avatarGuido Casiraghi

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1840

Abstract

3/tutorial/venv.html">virtual environments</a>. I am not covering this theme here, but I’d like to suggest to use <a href="https://pipenv.pypa.io/en/latest/">pipenv</a> to handle virtual environments and all related stuff.</p><p id="794e">For this example, I use the<a href="https://en.wikipedia.org/wiki/Iris_flower_data_set"> Iris data set</a>, already included in Plotly:</p><div id="32d6"><pre><span class="hljs-title">df</span> = px.<span class="hljs-class"><span class="hljs-keyword">data</span>.iris()</span></pre></div><p id="83bf">The returned object is a <a href="http://pandas.pydata.org/">Pandas</a> dataframe. The data set contains four features, width and length of the <b>petal</b>, and width and length of the <b>sepal</b>. For the purpose of this example, I build two separate <b>scatter plots</b>, one for the sepal and one for the petal.</p><figure id="f98d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*jBnEYfQVsWhG5_K7THdCxw.png"><figcaption>Scatter plot of sepal data</figcaption></figure><figure id="3cee"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*T2-FPanPLDS5fEbrIPlTMQ.png"><figcaption>Scatter plot of petal data</figcaption></figure><p id="15e0">The plots can be easily created in the following way:</p><div id="df0f"><pre><span class="hljs-keyword">import</span> plotly.express <span class="hljs-keyword">as</span> px</pre></div><div id="303e"><pre>plot = px.scatter(<span class="hljs-attribute">data_frame</span>=df, <span class="hljs-attribute">x</span>=x_name, <span class="hljs-attribute">y</span>=y_name, <span class="hljs-attribute">color</span>=<span class="hljs-string">'species'</span>, <span class="hljs-attribute">title</span>=title)</pre></div><p id="9754">Now you <b>export the plot to HTML</b> as a standalone <code>iframe</code>.</p><div id="7719"><pre>p

Options

lot.write_html(outpath, <span class="hljs-attribute">full_html</span>=<span class="hljs-literal">False</span>, <span class="hljs-attribute">include_plotlyjs</span>=<span class="hljs-string">'cdn'</span>)</pre></div><p id="01b7">The key takeaways here are:</p><ul><li>the <code>full_html</code> parameter set to false tells Plotly that we want a file containing only a <code>div</code></li><li><code>include_plotlyjs</code> set to <code>cdn</code> means that you don’t want to include the full Plotly Javascript into your HTML, but just link to the online version.</li></ul><p id="ba61">The last step is to create a main HTML file that includes the two iframes just created:</p><div id="2ed9"><pre><div class<span class="hljs-operator">=</span><span class="hljs-string">"container-fluid"</span> style<span class="hljs-operator">=</span><span class="hljs-string">"margin-top:40px"</span>> <iframe src<span class="hljs-operator">=</span><span class="hljs-string">"sepal_plot.html"</span> width<span class="hljs-operator">=</span><span class="hljs-string">"100%"</span> height<span class="hljs-operator">=</span><span class="hljs-string">"300"</span>></iframe> <iframe src<span class="hljs-operator">=</span><span class="hljs-string">"petal_plot.html"</span> width<span class="hljs-operator">=</span><span class="hljs-string">"100%"</span> height<span class="hljs-operator">=</span><span class="hljs-string">"300"</span>></iframe> </div></pre></div><p id="521a">Note that, to make the example a little bit more interesting, I have added some <a href="https://getbootstrap.com/">Bootstrap</a> code.</p><p id="31e3">That’s it. You can now share your HTML files or publish them online.</p></article></body>

How to export a Plotly chart as HTML

Photo by Lukas on Pexels

In this post, I will show how to create charts in Python using the Plotly library, by exporting them as plain HTML files.

See live demo here.

GitHub repo.

Plotly (among other things) offers a great Javascript open source library to build beautiful charts in your Web apps. Charts have many cool features and they are interactive (e.g., can be zoomed, panned, etc…). The same library can also be used from Python to embed charts, for example, in Jupyter notebooks.

In your Python workflow, suppose you have analyzed your data and built your awesome charts inside Jupyter. Now you want to publish your work and share it. You have of course many options. One of the easiest ways is nonetheless exporting everything as static HTML files (and maybe deploying them for free on Netlify).

Now let’s see how to do this. All the code for this blog post is published on GitHub.

First of all you need to install the Plotly package:

pip install plotly

Many expert Python users often prefer to install new packages in virtual environments. I am not covering this theme here, but I’d like to suggest to use pipenv to handle virtual environments and all related stuff.

For this example, I use the Iris data set, already included in Plotly:

df = px.data.iris()

The returned object is a Pandas dataframe. The data set contains four features, width and length of the petal, and width and length of the sepal. For the purpose of this example, I build two separate scatter plots, one for the sepal and one for the petal.

Scatter plot of sepal data
Scatter plot of petal data

The plots can be easily created in the following way:

import plotly.express as px
plot = px.scatter(data_frame=df, x=x_name, y=y_name, color='species', title=title)

Now you export the plot to HTML as a standalone iframe.

plot.write_html(outpath,
                full_html=False,
                include_plotlyjs='cdn')

The key takeaways here are:

  • the full_html parameter set to false tells Plotly that we want a file containing only a div
  • include_plotlyjs set to cdn means that you don’t want to include the full Plotly Javascript into your HTML, but just link to the online version.

The last step is to create a main HTML file that includes the two iframes just created:

<div class="container-fluid" style="margin-top:40px">                           <iframe src="sepal_plot.html" width="100%" height="300"></iframe>                           <iframe src="petal_plot.html" width="100%" height="300"></iframe>                       </div>

Note that, to make the example a little bit more interesting, I have added some Bootstrap code.

That’s it. You can now share your HTML files or publish them online.

Python
Plotly
Charts
HTML
Data Visualization
Recommended from ReadMedium