如何将Python散景图添加到我的网站?

时间:2018-08-22 17:48:27

标签: python html plot widget bokeh

我已经在Jupyter笔记本中创建了一个简单的Python Bokeh交互式图形,并且希望将该图形添加到我的一个网站中,以便用户可以与该图形进行交互。另外,我使用了ipywidgets的interact_manual小部件。我尝试使用file_html模块,然后使用print(html)来生成html文件,但是没有显示用户可以输入股票行情自动收录器和日期的interact_manual小部件。

此外,如果我将HTML文件上传到公共网站,那么公众可以查看图表吗?

我的代码:

def update(Ticker, syear=2016, smonth=(1), sday=(1), eyear=(current_year), emonth=(current_month), eday=(current_day)):

    fstocksymbol=str(Ticker.upper())

    starts=dt.datetime(syear,smonth,sday)
    end=dt.datetime(eyear,emonth,eday)

    if starts >= end:
        print('Please ensure the starting date does not exceed the end date')
        return False

    p.title.text = (symbolsdictionary[fstocksymbol]).upper()+' ('+fstocksymbol+')'
    tickerstring=fstocksymbol
    firstfunction=stockname(tickerstring, starts, end)
    secondfunction=stockdata(firstfunction)
    stockdates=[]
    stockcloseprices=[]
    for value in secondfunction:
        stockdates.append(value[0])
        stockcloseprices.append(float(value[4]))
    finaldate=np.array(stockdates, dtype=np.datetime64)

    r.data_source.data['x'] = finaldate
    r.data_source.data['y'] = stockcloseprices
    plot.y_range.start = float(min(stockcloseprices))
    plot.y_range.end = float(max(stockcloseprices))
    change=priceincrease(secondfunction)
    divgenerator=input_div(stockcloseprices)
    div=Div(text='<b> Stock Statistics </b><br>'+change+'<br><br>'+divgenerator, width=350, height=300)
    crawler=webcrawler(fstocksymbol)
    divc=Div(width=100,height=300)
    div2=Div(text='<b> News Insights </b><br>'+crawler[0]+'<br>'+crawler[1]+'<br>'+crawler[2]+'<br>'+crawler[3]+'<br>'+crawler[4], width=550)
    show(p)
    series2 = row(div, divc, div2)
    show(series2)

    push_notebook()



p=figure(

    height=400,
    x_axis_type='datetime',
    title=("SPDR S&P 500 ETF TRUST (SPY)"),
    tools='pan, box_zoom, wheel_zoom, reset',
)


x = np.array(sandpdates, dtype=np.datetime64)
y=sandpclose
r=p.line(x, y)

p.grid.grid_line_color="white"
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Price'
plot.y_range.start = float(min(sandpclose))
plot.y_range.end = float(max(sandpclose))
p.add_tools(HoverTool(
    tooltips=[
        ("Date", "@x{%F}"),
        ('Close',"@y")
    ],
    formatters={
        'x':'datetime', # use 'datetime' formatter for 'date' field
    },
    mode='vline'
))



startyear = widgets.IntSlider(description="Starting Year", max=current_year, min=2013, value=2015)
startmonth = widgets.IntSlider(description='Starting Month', max=12, min=1, value=1)
startday = widgets.IntSlider(description='Starting Day Of Month', max=31, min=1, value=1)
endyear = widgets.IntSlider(description='End Year', max=current_year, min=2013, value=current_year)
endmonth = widgets.IntSlider(description='End Month', max=12, min=1, value=current_month)
endday = widgets.IntSlider(description='End Day Of Month', max=31, min=1, value=current_day)


interact_manual(update, Ticker=stocksymbols, syear=startyear, smonth=startmonth, sday=startday, eyear=endyear, emonth=endmonth, eday=endday)


from bokeh.resources import CDN
from bokeh.embed import file_html


html = file_html(p, CDN, "my plot")
#print(html) to generate a long html file

0 个答案:

没有答案