2013-10-14 42 views
9

こんにちは私は、plotlyを使ってPython、Bottleを使ってグラフを生成しています。しかし、これは私にURLを返します。 などは:ボトル付きWebページに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

はい、埋め込みが可能です。ここで(任意のPlotlyのURLを持つ)は、使用できるのiframe抜粋です:

<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

プロットURLはAPIのものです。最近公開されたオフラインライブラリでこれを行う方法はありますか? –

関連する問題