Skip to content

Instantly share code, notes, and snippets.

@macleginn
Created October 23, 2019 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macleginn/5ef6dce81f3fd6a8b74a5ee655e2393b to your computer and use it in GitHub Desktop.
Save macleginn/5ef6dce81f3fd6a8b74a5ee655e2393b to your computer and use it in GitHub Desktop.
An example of an offline Plotly plot created using Python with Plotly.js included once in the <head> section of the page.
import plotly.express as px
import plotly.offline
template = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<script>{plotly}</script>
</head>
<body>
{plot}
</body>
</html>
"""
fig = px.histogram(px.data.tips(),x="total_bill")
fig_div = fig.to_html(include_plotlyjs=False)
## Works in the same way:
# fig_div = plotly.offline.plot(fig,
# output_type='div',
# include_plotlyjs=False)
result = template.format(
plotly=plotly.offline.get_plotlyjs(),
plot=fig_div
)
with open('test.html', 'w') as out:
out.write(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment