我正试图在Django html模板中嵌入一个绘图饼图。当图表以“联机模式”(即html代码片段存储在plotly服务器上)生成,而不是以“脱机模式”(即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>'
请注意,在Django的Http呈现请求中,pie_url是作为字符串传递的。模板使用|安全标记将字符串解释为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>'''
任何建议都将不胜感激。
不必将html写入文件,您可以将图形的html部分以字符串的形式绘出返回。例如,使用基于类的TemplateView:
以及模板:
相关问题 更多 >
编程相关推荐