GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing plotly.com — Interactive html export in Python Interactive vs Static Export¶Plotly figures are interactive when viewed in a web browser: you can hover over data points, pan and zoom axes, and show and hide traces by clicking or double-clicking on the legend. You can export figures either to static image file formats like PNG, JPEG, SVG or PDF or you can export them to HTML files which can be opened in a browser. This page explains how to do the latter. Saving to an HTML file¶Any figure can be saved as an HTML file using the import plotly.express as px fig = px.scatter(x=range(10), y=range(10)) fig.write_html("path/to/file.html") Controlling the size of the HTML file¶By default, the resulting HTML file is a fully self-contained HTML file which can be uploaded to a web server or shared via email or other file-sharing mechanisms. The downside to this approach is that the file is very large (5Mb+) because it contains an inlined copy of the Plotly.js library required to make the figure interactive. This can be controlled via the Inserting Plotly Output into HTML using a Jinja2 Template¶You can insert Plotly output and text related to your data into HTML templates using Jinja2. Use
Then use the following Python to replace import plotly.express as px from jinja2 import Template data_canada = px.data.gapminder().query("country == 'Canada'") fig = px.bar(data_canada, x='year', y='pop') output_html_path=r"/path/to/output.html" input_template_path = r"/path/to/template.html" plotly_jinja_data = {"fig":fig.to_html(full_html=False)} #consider also defining the include_plotlyjs parameter to point to an external Plotly.js as described above with open(output_html_path, "w", encoding="utf-8") as output_file: with open(input_template_path) as template_file: j2_template = Template(template_file.read()) output_file.write(j2_template.render(plotly_jinja_data)) The height of the figure can be made responsive by setting import plotly.express as px from jinja2 import Template data_canada = px.data.gapminder().query("country == 'Canada'") fig = px.bar(data_canada, x='year', y='pop') output_html_path=r"/path/to/output.html" input_template_path = r"/path/to/template.html" fig.update_layout(autosize=True, height=None) fig_html = fig.to_html( config={"responsive": True}, default_height="80vh", full_html=False, ) plotly_jinja_data = {"fig": fig_html} #consider also defining the include_plotlyjs parameter to point to an external Plotly.js as described above with open(output_html_path, "w", encoding="utf-8") as output_file: with open(input_template_path) as template_file: j2_template = Template(template_file.read()) output_file.write(j2_template.render(plotly_jinja_data)) HTML export in Dash¶Dash is the best way to build analytical apps in Python using Plotly figures. To run the app below, run Get started with the official Dash docs and learn how to effortlessly style & publish apps like this with Dash Enterprise or Plotly Cloud. Out[1]:
Sign up for Dash Club → Free cheat sheets plus updates from Chris Parmer and Adam Schroeder delivered to your inbox every two months. Includes tips and tricks, community apps, and deep dives into the Dash architecture. Join now. Reference¶See the following links for parameter information and HTML figure export options: What About Dash?¶Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library. Learn about how to install Dash at https://dash.plot.ly/installation. Everywhere in this page that you see import plotly.graph_objects as go # or plotly.express as px fig = go.Figure() # or any Plotly Express function e.g. px.bar(...) # fig.add_trace( ... ) # fig.update_layout( ... ) from dash import Dash, dcc, html app = Dash() app.layout = html.Div([ dcc.Graph(figure=fig) ]) app.run(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |