2017-08-16 31 views
1

我使用plotly庫創建圖像並嘗試以HTML顯示。我以HTML格式格式化圖像。但display(HTML(report_html))代碼不顯示HTML頁面。Plotly Ipython - 顯示(HTML)函數不顯示html文件

我正在使用python 3.5和pycharm IDE。我沒有使用iPython筆記本。

的源代碼:

from IPython.display import display, HTML, Image 
import plotly.plotly as py 
from plotly.offline import init_notebook_mode 
init_notebook_mode() 

data = go.scatter(x=df['date'], y=i, name = long_name['value']) 
figures = [data] 
images = [base64.b64encode(py.image.get(figure, width=width,height=height)).decode('utf-8') for figure in figures] 

    report_html = '' 
    for image in images: 
     _ = template 
     _ = _.format(image=image, caption='', width=width, height=height) 
     report_html += _ 

    display(HTML(report_html)) 

我沒有得到任何錯誤。我剛開以下輸出

IPython.core.display.HTML對象

+0

您使用plotl y離線繪製圖像,還是使用情節性在線模式,您能澄清一下嗎? –

+0

我正在使用情節式在線模式。在公共雲中保存密謀圖表。 –

+1

我不是專業的用戶,我可以在網上積極使用嗎? –

回答

1

很抱歉這麼晚纔回復,該代碼完全適用於我,讓我分享我的樣品,基本的區別是我使用figure object under plotly.graph_objs代替figures = [data]

代碼:

from IPython.display import display, HTML, Image 
import plotly.plotly as py 
import base64 
import plotly.graph_objs as go 
py.sign_in('<<username here>>', '<<api key here>>') 

# required variables 
width=500 
height=300 

# template not provided so created my own 
template = """ 
<div class="row"> 
    <div class="col-xs-12" style="text-align:center"> 
     {caption} 
    </div> 
    <div class="col-xs-12"> 
     <img src="data:image/png;base64, {image}" alt="Red dot"/> 
    </div> 
</div> 
""" 

# data = go.scatter(x=df['date'], y=i, name = long_name['value']) 

# using my sample data instead 
trace = go.Bar(x=[2, 4, 6], y= [10, 12, 15]) 

# layout can also be provided, I am giving as blank 
layout = dict() 

# figures = [data] 
# changing the above commented line to plotly figure object 
fig = go.Figure(data=[trace], layout=layout) 

# defining list which will contain all the plot objects 
figures = [] 
figures.append(fig) 

images = [base64.b64encode(py.image.get(figure, width=width,height=height)).decode('utf-8') for figure in figures] 

report_html = '' 
for image in images: 
    _ = template 
    _ = _.format(image=image, caption='', width=width, height=height) 
    report_html += _ 

display(HTML(report_html)) 
+0

非常感謝!它像一個魅力 –

+0

@ArvinthKumar不客氣 –