【发布时间】:2016-08-19 04:30:22
【问题描述】:
我正在尝试在 Django html 模板中嵌入一个绘图饼图。当图表以“在线模式”(即 html sn-p 存储在 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>'
注意 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>'''
任何建议将不胜感激。
【问题讨论】:
-
你能把它输出到一个.html文件吗?
-
嗨,是的,html 文件已生成。但是当 Django 渲染它时它并没有显示出来(这是原始帖子中的 pie_url 行。)
-
将保存到 pie_url 中的字符串包裹在三个 ' 而不是单个 ' 之间是否正确?