Python 用瓶子将绘图嵌入网页
嗨,我正在使用plotly使用Python生成图形,瓶子。但是,这会返回一个url。 比如: 我想将整个图形粘贴到我的网页中,而不是提供链接。这可能吗 我的代码是:Python 用瓶子将绘图嵌入网页,python,bottle,plotly,Python,Bottle,Plotly,嗨,我正在使用plotly使用Python生成图形,瓶子。但是,这会返回一个url。 比如: 我想将整个图形粘贴到我的网页中,而不是提供链接。这可能吗 我的代码是: import os from bottle import run, template, get, post, request from plotly import plotly py = plotly(username='user', key='key') @get('/plot') def form(): return
import os
from bottle import run, template, get, post, request
from plotly import plotly
py = plotly(username='user', key='key')
@get('/plot')
def form():
return '''<h2>Graph via Plot.ly</h2>
<form method="POST" action="/plot">
Name: <input name="name1" type="text" />
Age: <input name="age1" type="text" /><br/>
Name: <input name="name2" type="text" />
Age: <input name="age2" type="text" /><br/>
Name: <input name="name3" type="text" />
Age: <input name="age3" type="text" /><br/>
<input type="submit" />
</form>'''
@post('/plot')
def submit():
name1 = request.forms.get('name1')
age1 = request.forms.get('age1')
name2 = request.forms.get('name2')
age2 = request.forms.get('age2')
name3 = request.forms.get('name3')
age3 = request.forms.get('age3')
x0 = [name1, name2, name3];
y0 = [age1, age2, age3];
data = {'x': x0, 'y': y0, 'type': 'bar'}
response = py.plot([data])
url = response['url']
filename = response['filename']
return ('''Congrats! View your chart here <a href="https://plot.ly/~abhishek.mitra.963/1">View Graph</a>!''')
if __name__ == '__main__':
port = int(os.environ.get('PORT', 8080))
run(host='0.0.0.0', port=port, debug=True)
导入操作系统
从瓶子导入运行、模板、获取、发布、请求
从plotly导入plotly
py=plotly(username='user',key='key')
@获取(“/plot”)
def form():
通过Plot.ly返回''''图形
姓名:
年龄:
姓名:
年龄:
姓名:
年龄:
'''
@post(“/plot”)
def submit():
name1=request.forms.get('name1')
age1=request.forms.get('age1')
name2=request.forms.get('name2')
age2=request.forms.get('age2')
name3=request.forms.get('name3')
age3=request.forms.get('age3')
x0=[name1,name2,name3];
y0=[1岁,2岁,3岁];
数据={'x':x0,'y':y0,'type':'bar'}
响应=py.plot([数据])
url=响应['url']
filename=响应['filename']
return(“”“恭喜!在此处查看图表!”“”)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
port=int(os.environ.get('port',8080))
运行(主机=0.0.0.0',端口=port,调试=True)
是的,可以嵌入。以下是一个iframe片段,您可以使用它(与任何Plotly URL一起使用):
绘图嵌入在专门为嵌入绘图而创建的URL处。所以在这种情况下,你的情节是。要嵌入它的URL是通过添加来创建的。嵌入到URL:
您可以更改该代码段中的宽度/高度标注。要获取iframe代码并查看不同的大小,可以单击绘图上的嵌入图标,或者在共享时生成代码。以下是嵌入选项的位置:
。这是一个有用的教程,有人用Plotly和Battle开发
如果这不起作用,请告诉我,我很乐意帮忙
披露:我是Plotly团队的成员。有没有考虑过在flask中使用模板?我有模板。但是它会有什么帮助呢?我正在寻找类似的东西。我的情况是每隔5分钟运行一段python代码,并更新相同的绘图。相反,我每次都会得到新的窗口。我试图嵌入网页的iframe,因此需要更新相同的绘图。我有20个这样的绘图。绘图URL来自API,对吗?对于最近发布的脱机库,有没有这样的方法?
import os
from bottle import run, template, get, post, request
from plotly import plotly
py = plotly(username='user', key='key')
@get('/plot')
def form():
return '''<h2>Graph via Plot.ly</h2>
<form method="POST" action="/plot">
Name: <input name="name1" type="text" />
Age: <input name="age1" type="text" /><br/>
Name: <input name="name2" type="text" />
Age: <input name="age2" type="text" /><br/>
Name: <input name="name3" type="text" />
Age: <input name="age3" type="text" /><br/>
<input type="submit" />
</form>'''
@post('/plot')
def submit():
name1 = request.forms.get('name1')
age1 = request.forms.get('age1')
name2 = request.forms.get('name2')
age2 = request.forms.get('age2')
name3 = request.forms.get('name3')
age3 = request.forms.get('age3')
x0 = [name1, name2, name3];
y0 = [age1, age2, age3];
data = {'x': x0, 'y': y0, 'type': 'bar'}
response = py.plot([data])
url = response['url']
filename = response['filename']
return ('''Congrats! View your chart here <a href="https://plot.ly/~abhishek.mitra.963/1">View Graph</a>!''')
if __name__ == '__main__':
port = int(os.environ.get('PORT', 8080))
run(host='0.0.0.0', port=port, debug=True)