Hoo*_*voo 21 python django plotly
我试图在Django html模板中嵌入一个情节饼图.当图表以"在线模式"生成时(即html片段存储在绘图服务器上)但不在"离线模式"(即当html存储在本地时)时,这种方法正常工作.在后一种情况下,图表不会出现.我希望能够将html存储在我的本地服务器上并从那里嵌入图表.
这是有效的一点:
import plotly.plotly as py
import plotly.graph_objs as go
labels = [1,2,3,4]
values = [10,20,30,40]
ndata = 100
fig = {
'data': [{'labels': labels,
'values': values,
'type': 'pie',
'textposition':"none",
'textinfo':"percent",
'textfont':{'size':'12'},
'showlegend':'false'}],
'layout': {'title': 'Total:'+str(ndata),
'showlegend':'false',
'height':'200',
'width':'200',
'autosize':'false',
'margin':{'t':'50','l':'75','r':'0','b':'10'},
'separators':'.,'}
}
plotly_url = py.plot(fig, filename='myfile', auto_open=False)
pie_url = '<iframe width="200" height="200" frameborder="0" seamless="seamless" scrolling="no" src='+plotly_url+'.embed?width=200&height=200&link=false&showlegend=false></iframe>'
Run Code Online (Sandbox Code Playgroud)
请注意,pie_url在Django中的Http渲染请求中作为字符串传递.模板使用|将字符串解释为html 安全标签,即{{pie_url | safe}}.
这是不起作用的一点:
from plotly.offline import download_plotlyjs, plot
import plotly.graph_objs as go
labels = [1,2,3,4]
values = [10,20,30,40]
ndata = 100
fig = {
'data': [{'labels': labels,
'values': values,
'type': 'pie',
'textposition':"none",
'textinfo':"percent",
'textfont':{'size':'12'},
'showlegend':'false'}],
'layout': {'title': 'Total:'+str(ndata),
'showlegend':'false',
'height':'200',
'width':'200',
'autosize':'false',
'margin':{'t':'50','l':'75','r':'0','b':'10'},
'separators':'.,'}
}
plotly_url = plot(fig, filename='file:///home/website/pie.html', auto_open=False)
pie_url = '''<iframe width="200" height="200" frameborder="0" seamless="seamless" scrolling="no" src=\"'''+plotly_url+'''.embed?width=200&height=200&link=false&showlegend=false\"></iframe>'''
Run Code Online (Sandbox Code Playgroud)
任何意见,将不胜感激.
Chr*_* K. 37
不是将html写入文件,而是可以将图形的html部分作为字符串返回.例如,使用基于类的TemplateView:
import plotly.offline as opy
import plotly.graph_objs as go
class Graph(TemplateView):
template_name = 'graph.html'
def get_context_data(self, **kwargs):
context = super(Graph, self).get_context_data(**kwargs)
x = [-2,0,4,6,7]
y = [q**2-q+3 for q in x]
trace1 = go.Scatter(x=x, y=y, marker={'color': 'red', 'symbol': 104, 'size': "10"},
mode="lines", name='1st Trace')
data=go.Data([trace1])
layout=go.Layout(title="Meine Daten", xaxis={'title':'x1'}, yaxis={'title':'x2'})
figure=go.Figure(data=data,layout=layout)
div = opy.plot(figure, auto_open=False, output_type='div')
context['graph'] = div
return context
Run Code Online (Sandbox Code Playgroud)
和模板:
{% if graph %}
<div style="width:600;height:500">
{{ graph|safe }}
</div>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
小智 21
plotly 有一些更新,其中一些适用于plotly 4+:
注意:使用 plotly.py 不需要互联网连接、帐户或付款。在版本 4 之前,该库可以在“在线”或“离线”模式下运行。文档倾向于强调在线模式,其中图表发布到 Chart Studio Web 服务。在第 4 版中,所有“在线”功能都从 plotly 包中删除,现在可以作为单独的、可选的、图表工作室包使用(见下文)。plotly.py 版本 4 仅是“离线”的,不包括将图形或数据上传到云服务的任何功能。
to_html功能这允许您执行以下操作以离线嵌入图形:
graph = fig.to_html(full_html=False, default_height=500, default_width=700)
context = {'graph': graph}
response = render(request, 'graph.html', context)
Run Code Online (Sandbox Code Playgroud)
而在graph.html你可以这样做:
graph = fig.to_html(full_html=False, default_height=500, default_width=700)
context = {'graph': graph}
response = render(request, 'graph.html', context)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15094 次 |
| 最近记录: |