- How to insert a Python graph into an HTML Web Site
- A short tutorial to integrate graphs built in Altair or Plot.ly with an HTML Web site
- Plot.ly
- 23 CSS Charts And Graphs
- Related Articles
- Author
- Links
- Made with
- About a code
- CSS-only Pie Charts
- Author
- Links
- Made with
- About a code
- Skills Chart Animation with a Bit of Houdini Magic
- Author
- Links
- Made with
- About a code
- Pure CSS Area Chart
- Author
- Links
- Made with
- About a code
- Graph
- Author
- Links
- Made with
- About a code
- Pure CSS Donut Charts
- Author
- Links
- Made with
- About a code
- Pure CSS Background Depending on Height
- Author
- Links
- Made with
- About a code
- HTML Chart
- Author
- Links
- Made with
- About a code
- This Pen is 19% HTML & 81% CSS
- Author
- Links
- Made with
- About a code
- Purple Pie Chart
- Author
- Links
- Made with
- About a code
- Statistics Card
- Author
- Links
- Made with
- About a code
- CSS Animation: A Line Graph
- Author
- Links
- Made with
- About a code
- Interactive, Responsive Pie Chart
- Author
- Links
- Made with
- About a code
- Simple and Responsive Organizational Chart
- Author
- Links
- Made with
- About a code
- Bar Graph
- Author
- Links
- Made with
- About a code
- Charles Hayter’s Colour Diagrams
- Author
- Links
- Made with
- About a code
- Diagram
- Author
- Links
- Made with
- About a code
- Interactive SVG & CSS Graph
- Author
- Links
- Made with
- About a code
- Animated CSS Graph
- Author
- Links
- Made with
- About a code
- chart.css
- Author
- Links
- Made with
- About a code
- Pure CSS Bars
- Author
- Links
- Made with
- About a code
- CSS Only 3D Bar Graph
- Author
- Links
- Made with
- About a code
- Circle Chart With Three Bars
- Author
- Embed interactive plots in static web pages with Plotly
- Generate HTML with Plotly
- Embed the graph into a web page
How to insert a Python graph into an HTML Web Site
A short tutorial to integrate graphs built in Altair or Plot.ly with an HTML Web site
As many people know, Python is a very powerful language for data manipulation, including data collection, data analysis and data visualisation. Regarding this last aspect, many powerful libraries and frameworks exist, such as the Plot.ly graph library and Altair.
The Plot.ly graph library is a very powerful library, which permits to build graphs and maps in Python, R and Javascript. Altair is another declarative statistical visualisation library for Python, based on Vega and Vega-lite.
In this tutorial, I suppose that you already know how to build a graph using either Plot.ly or Altair. I will illustrate only how to integrate an existing graph with an HTML Web Site.
Three steps are required to integrate a Python graph into an HTML Web site:
- generate the graph either in Plot.ly or Altair
- save the graph as an HTML page
- manipulate the generated HTML
Plot.ly
Firstly, I build a generic graph in Plot.ly. In this specific example, I consider as input data the time series regarding the number of positive cases to COVID-19 in Italy. I plot a line, exploiting the following Python code:
import plotly
import plotly.graph_objs as go
import pandas as pddf = pd.read_csv('https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-andamento-nazionale/dpc-covid19-ita-andamento-nazionale.csv')# Create a trace
data = [go.Scatter(
x = df['data'],
y = df['totale_positivi'],
)]layout = go.Layout(
xaxis=dict(
title='Data',
),
yaxis=dict(
title='Totale positivi',
)
)
fig = go.Figure(data=data, layout=layout)
The following figure shows the produced output:
23 CSS Charts And Graphs
Collection of free HTML and pure CSS chart and graph code examples from Codepen and other resources.
Related Articles
- jQuery Charts And Graphs
- Tailwind Charts And Graphs
Author
Links
Made with
About a code
CSS-only Pie Charts
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Skills Chart Animation with a Bit of Houdini Magic
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Pure CSS Area Chart
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Graph
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Pure CSS Donut Charts
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Pure CSS Background Depending on Height
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
HTML Chart
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
This Pen is 19% HTML & 81% CSS
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Purple Pie Chart
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Statistics Card
Financial chart UI design, animated SVG line. Tooltips on hover. No jQuery, no JavaScript involved.
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
CSS Animation: A Line Graph
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Dependencies: simple-line-icons.css, jquery.js
Author
Links
Made with
About a code
Interactive, Responsive Pie Chart
Interactive, responsive pie chart with conic-gradient() , CSS variables & Houdini magic.
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Simple and Responsive Organizational Chart
HTML5 and CSS3 only simple and responsive organizational chart.
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Bar Graph
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Charles Hayter’s Colour Diagrams
An attempt to recreate the colour diagrams in Charles Hayter’s “A New Practical Treatise on the Three Primitive Colours Assumed as a Perfect System of Rudimentary Information”.
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Diagram
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Interactive SVG & CSS Graph
Interactive SVG & CSS graph with segments, legend and hover effect.
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Animated CSS Graph
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
chart.css
A simple CSS chart system.
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Pure CSS Bars
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
CSS Only 3D Bar Graph
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Links
Made with
About a code
Circle Chart With Three Bars
Compatible browsers: Chrome, Edge, Firefox, Opera, Safari
Author
Embed interactive plots in static web pages with Plotly
Inserting images into articles is something I have been battling with since my university days, when getting it right with LaTex was the ultimate challenge.
These days things have gotten much better, thanks to the use of formats like MarkDown or RestructuredText. These make it quite straightforward to include a JPG or PNG image in a document for the web (and can be used to produce PDF documents as well).
But these are still just static images. Instead, interactive images — where one can zoom, pan, and select different parts of the graph — allow the user to explore and better understand the visualization and the data. This goes up a notch from static plots and it is quite useful when the amount of information displayed is considerable.
Until a few years ago interactive visualizations were relegated to the wizardry of some Javascript gurus and only for the web. Luckily, several new Python libraries (e.g. Bokeh, Plotly ) came out that produce interactive graphs for e.g. Jupyter notebooks or dashboards.
When it comes to interactive visualizations, people would often go for the dashboard route, i.e. a bespoke web page/application that is created for the purpose of visualizing a specific set of data. But what if we just wanted to include a more user-friendly graph in a regular blog post? This is a much simpler solution than programming a full web application or dashboard. It comes down to the ability to export graphs in HTML format.
Generate HTML with Plotly
Plotly is a Javascript-based declarative data-visualization library. The library has APIs in many popular languages, most notably R and Python. It can be used to build powerful data visualizations and dashboards. Graphs created with Plotly are fully interactive and can be embedded in a standalone static web page. Let’s see how to do it using the Python API.
The library comes with some handy demo data that we can use. In this case, we’ll use the Gapminder dataset.
Plotly 4.0 offers a renewed interface through the plotly.express submodule. A function call creates a Pandas DataFrame gapminder which can be directly fed into the plotting function px.scatter() which creates a figure object. Using a subset of the original dataframe ( year==2007 ), we specify which columns should be used on what axes/dimensions of the graph ( x, y, size, color ). Lastly we specify the size of the plot in pixels.
from plotly import express as px gapminder = px.data.gapminder() fig = px.scatter( gapminder.query("year==2007"), x="gdpPercap", y="lifeExp", size="pop", color="continent", hover_name="country", log_x=True, size_max=60, height=400, width=650, )
The last step is displaying the figure object. This can be done directly to the screen or to a variety of other formats for further consumption using a set of bespoke methods. In this case, the .to_html() method will do the job. The HTML output can be written to a file:
with open('plotly_graph.html', 'w') as f: f.write(fig.to_html(include_plotlyjs='cdn'))
The include_plotlyjs=’cdn’ keyword argument includes the plotly.js library as a link to the official CDN. The figure can be made offline-viewable by setting include_plotlyjs=True but that adds about 3MB of extra code to the HTML document.
The document we just produced is a standalone HTML page, viewable by most modern browsers. However, it’s only containing the graph. There is a small extra step needed in order to include the graph in another page, e.g. this blog post.
Embed the graph into a web page
The HTML document we just produced should be a familiar one, composed by a header and a body, enclosed within their respective and tags.
We are interested in the content of the body. The body should contain a single which contains everything that is needed to visualize the graph. We can just take that div and copy it to the point in the page where we want to display the graph. Then we should see the figure, displayed exactly as it was created within plotly.py . Voila: