将Plotly图形嵌入到带有Bottle的网页中

use*_*165 9 python bottle plotly

嗨,我正在使用Python,瓶子使用plotly生成图表.但是,这会给我一个网址.喜欢:

https://plot.ly/~abhishek.mitra.963/1
Run Code Online (Sandbox Code Playgroud)

我想将整个图表粘贴到我的网页中,而不是提供链接.这可能吗?

我的代码是:

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)
Run Code Online (Sandbox Code Playgroud)

Mat*_*hez 10

是的,嵌入是可能的.这是您可以使用的iframe代码段(使用任何Plotly URL):

<iframe width="800" height="600" frameborder="0" seamless="seamless" scrolling="no" src="https://plot.ly/~abhishek.mitra.963/1/.embed?width=800&height=600"></iframe>

该图嵌入在一个专门用于嵌入图的URL中.所以在这种情况下你的情节是https://plot.ly/~abhishek.mitra.963/1/.嵌入它的URL是通过将.embed添加到URL:https://plot.ly/~abhishek.mitra.963/1.embed .

您可以更改该代码段中的宽度/高度尺寸.要获取iframe代码并查看不同的大小,可以单击绘图上的嵌入图标,或者在共享时生成代码.以下是嵌入选项的位置:

在此输入图像描述

以下是华盛顿邮报中嵌入式图表的外观.而这里是一个有用的教程有人用Plotly和瓶做开发.

如果这不起作用,请告诉我,我很乐意帮忙.

披露:我在Plotly团队.

  • 情节URL来自API,对吧?有没有办法用最近发布的离线库做这样的事情? (2认同)