2017-11-28 93 views
14

私はHTMLファイルに埋め込みたいグラフを作成しました。オンラインでplotlyを使用すると、意図したとおりに動作します。ただし、オフラインを使用すると、オフラインチャートが動作します(つまり、別のHTMLチャートが表示されます)が、HTML(nick.html)に埋め込まれていない、つまりiframeが空です。Python Plotly - HTMLに埋め込まれたオフラインチャート

これは私のコードです:それは埋め込み、どのようにそれを修正しないようにしている理由

fig = dict(data=data, layout=layout) 
plotly.tools.set_credentials_file(username='*****', api_key='*****') 
aPlot = plotly.offline.plot(fig, config={"displayModeBar": False}, show_link=False, 
          filename='pandas-continuous-error-bars.html') 

html_string = ''' 
<html> 
    <head> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> 
     <style>body{ margin:0 100; background:whitesmoke; }</style> 
    </head> 
    <body> 
     <h1>Monthly Report</h1> 

     <!-- *** Section 1 *** ---> 
     <h2></h2> 
     <iframe width="1000" height="550" frameborder="0" seamless="seamless" scrolling="no" \ 
src="''' + aPlot + '''.embed?width=800&height=550"></iframe> 
     <p> (Insights).</p> 


    </body> 
</html>''' 

f = open("C:/Users/nicholas\Desktop/nick.html",'w') 
f.write(html_string) 
f.close() 

誰もが知っていますか?

答えて

9

aPlotは、Plotlyファイルのファイル名です。

iframeには、ファイル名に.embed?width=800&height=550を追加すると、存在しないファイル名になります。

この文字列を削除すると、つまりsrc="''' + aPlot + '''"が機能するはずです。代わりに、あなたはまた、アプローチを使用することができます全体のHTMLファイルを埋め込む

は、関連するすべての情報を持つ小さなHTMLファイルを生成しhere、すなわち生成divを提案し、あなたのヘッダーにplotly.jsが含まれます。

import plotly 

fig = {'data': [{'x': [1,2,3], 
        'y': [2,5,3], 
        'type': 'bar'}], 
     'layout': {'width': 800, 
       'height': 550}} 

aPlot = plotly.offline.plot(fig, 
          config={"displayModeBar": False}, 
          show_link=False, 
          include_plotlyjs=False, 
          output_type='div') 

html_string = ''' 
<html> 
    <head> 
     <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> 
     <style>body{ margin:0 100; background:whitesmoke; }</style> 
    </head> 
    <body> 
     <h1>Monthly Report</h1> 
     ''' + aPlot + ''' 
    </body> 
</html>''' 

with open("nick.html", 'w') as f: 
    f.write(html_string) 
+1

ありがとう、ありがとう。私はちょうど飛行機に乗っているので、それが動作するかどうかをチェックすることはできませんが、ロジックがよく見えると私は理解していますので、非常にありがとう:) – ScoutEU

関連する問題