使用Bottle将Plotly图嵌入网页 [英] Embed Plotly graph into a webpage with Bottle

查看:132
本文介绍了使用Bottle将Plotly图嵌入网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用Python,Bottle使用plotly生成图形.但是,这给我返回了一个URL. 喜欢:

Hi i am using plotly to generate graphs using Python, Bottle. However, this returns me a url. Like:

https://plot.ly/~abhishek.mitra.963/1

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

I want to paste the entire graph into my webpage instead of providing a link. Is this possible?

我的代码是:

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)

推荐答案

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

Yes, embedding is possible. Here's an iframe snippet you can use (with any 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 .

The plot gets embedded at a URL that is made especially for embedding the plot. So in this case your plot is https://plot.ly/~abhishek.mitra.963/1/. The URL to embed it is made by adding .embed to the URL: https://plot.ly/~abhishek.mitra.963/1.embed.

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

You can change the width/height dimensions in that snippet. To get the iframe code and see different sizes, you can click the embed icon on a plot, or when you share it generate the code. Here's where the embed options are:

此处是有帮助的本教程是有关使用Plotly和Bottle进行开发的.

Here's how an embedded graph looks in the Washington Post. And here is a helpful tutorial someone made on developing with Plotly and Bottle.

让我知道是否行不通,我很乐意提供帮助.

Let me know if that doesn't work, and I'm happy to help out.

披露:我在Plotly团队中.

Disclosure: I'm on the Plotly team.

这篇关于使用Bottle将Plotly图嵌入网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆