2013-10-14 30 views
9

您好我正在使用Python,Bottle來繪製圖表。不過,這給我一個url。 像:將Plotly圖嵌入到帶有瓶子的網頁中

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

我想整個圖形粘貼到我的網頁上,而不是提供一個鏈接。這可能嗎?

我的代碼是:

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) 
+0

有沒有考慮在燒瓶中使用模板? –

+0

我有模板。但它會如何幫助? – user2834165

回答

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代碼並看到不同的大小,您可以單擊圖上的嵌入圖標,或者在分享時生成代碼。這裏就是嵌入選項有:

enter image description here

Here's how an embedded graph looks in the Washington Posthere是一個有用的教程,有人用Plotly和Bottle進行開發。

讓我知道如果這不起作用,我很樂意幫忙。

披露:我在Plotly團隊。

+0

我正在尋找類似的東西。我的情況是在每隔5分鐘的間隔後運行一個python代碼並更新同一個圖。相反,我每次都會獲得新的窗口。我試圖嵌入網頁的iframe,所以需要更新相同的情節。我有20個這樣的情節。 –

+2

劇情網址來自API,對吧?有沒有辦法像最近發佈的離線庫那樣做這樣的事情? –