Skip to content

Instantly share code, notes, and snippets.

@Benoss
Last active July 11, 2018 01:54
Show Gist options
  • Save Benoss/feda0c2fdb5ba1d5f5e66e83b2c49ec1 to your computer and use it in GitHub Desktop.
Save Benoss/feda0c2fdb5ba1d5f5e66e83b2c49ec1 to your computer and use it in GitHub Desktop.
Python Standalone plotly template
<html>
<head>
<title>test</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div style="display: flex; flex-flow: row wrap;">
<div style="width: 50%; height:400px;"><div id="dd4338a2-db39-4984-9ad0-911ebb98ff11" style="height: 100%; width: 100%;" class="plotly-graph-div"></div><script type="text/javascript">window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL="https://plot.ly";Plotly.newPlot("dd4338a2-db39-4984-9ad0-911ebb98ff11", [{"y": [5, 16, 18, 5, 7, 2, 17, 18, 1, 6, 2, 4], "x": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], "type": "scatter"}], {}, {"linkText": "", "showLink": true})</script></div>
<div style="width: 50%; height:400px;"><div id="5569d3f7-1687-44fc-9c46-1bb70ef6309e" style="height: 100%; width: 100%;" class="plotly-graph-div"></div><script type="text/javascript">window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL="https://plot.ly";Plotly.newPlot("5569d3f7-1687-44fc-9c46-1bb70ef6309e", [{"marker": {"color": "rgb(49,130,189)"}, "y": [20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17], "type": "bar", "name": "Primary Product", "x": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]}, {"marker": {"color": "rgb(204,204,204)"}, "y": [19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16], "type": "bar", "name": "Secondary Product", "x": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]}], {"barmode": "group", "xaxis": {"tickangle": -45}}, {"linkText": "", "showLink": true})</script></div>
<div style="width: 50%; height:400px;"><div id="bb5a03dc-4d53-4ed9-8ecf-a6de90bd0b2c" style="height: 100%; width: 100%;" class="plotly-graph-div"></div><script type="text/javascript">window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL="https://plot.ly";Plotly.newPlot("bb5a03dc-4d53-4ed9-8ecf-a6de90bd0b2c", [{"y": [1, 3, 6], "x": ["2013-10-04 22:23:00", "2013-11-04 22:23:00", "2013-12-04 22:23:00"], "type": "scatter"}], {}, {"linkText": "", "showLink": true})</script></div>
<div style="width: 50%; height:400px;"><div id="928f4f50-a037-4d3a-b6b1-e70e096dd075" style="height: 100%; width: 100%;" class="plotly-graph-div"></div><script type="text/javascript">window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL="https://plot.ly";Plotly.newPlot("928f4f50-a037-4d3a-b6b1-e70e096dd075", [{"values": [46, 70, 12, 23], "labels": ["Residential", "Non-Residential", "Utility", "other"], "type": "pie"}], {"title": "Forcasted 2014 U.S. PV Installations by Market Segment"}, {"linkText": "", "showLink": true})</script></div>
<div>
</body>
</html>
# -*- coding: utf-8 -*-
# pip install plotly
import random
import plotly
from plotly.offline import plot
from plotly.offline.offline import get_plotlyjs
from plotly.graph_objs import Bar, Scatter, Figure, Layout
def generate_rand_serie(nb, low=1, high=20):
return [random.randint(low, high) for x in range(nb)]
OUTPUT_FILE = "export.html"
div = plot([Scatter(x=[x for x in range(12)], y=generate_rand_serie(12))], output_type='div', include_plotlyjs=False, link_text='')
div2 = plot(
Figure(data=[
Bar(
x=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
y=[20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17],
name='Primary Product',
marker=dict(
color='rgb(49,130,189)'
)
),
Bar(
x=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
y=[19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16],
name='Secondary Product',
marker=dict(
color='rgb(204,204,204)',
)
)
], layout=Layout(xaxis=dict(tickangle=-45), barmode='group'))
, output_type='div', include_plotlyjs=False, link_text='')
div3 = plot([Scatter(
x=['2013-10-04 22:23:00', '2013-11-04 22:23:00', '2013-12-04 22:23:00'],
y=[1, 3, 6])], output_type='div', include_plotlyjs=False, link_text='')
div4 = plot(
{
'data': [{'labels': ['Residential', 'Non-Residential', 'Utility', 'other'],
'values': generate_rand_serie(4, 1, 80),
'type': 'pie'}],
'layout': {'title': 'Forcasted 2014 U.S. PV Installations by Market Segment'}
}
, output_type='div', include_plotlyjs=False, link_text='')
def get_plotlyjs_script(offline=False):
if offline:
script_tag = """
<script type="text/javascript">
{plotly_library}
</script>
""".format(plotly_library=get_plotlyjs())
else:
script_tag = """<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>"""
return script_tag
template = """
<html>
<head>
<title>test</title>
{plotly_library}
</head>
<body>
<div style="display: flex; flex-flow: row wrap;">
<div style="width: 50%; height:400px;">{plot_div1}</div>
<div style="width: 50%; height:400px;">{plot_div2}</div>
<div style="width: 50%; height:400px;">{plot_div3}</div>
<div style="width: 50%; height:400px;">{plot_div4}</div>
<div>
</body>
</html>
"""
with open(OUTPUT_FILE, 'wb') as f:
f.write(template.format(
plotly_library=get_plotlyjs_script(),
plot_div1=div, plot_div2=div2,
plot_div3=div3, plot_div4=div4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment