Python 如何在Django中使用bokeh add_periodic_回调函数

Python 如何在Django中使用bokeh add_periodic_回调函数,python,django,bokeh,Python,Django,Bokeh,我试图在django实现bokeh live图表。我在一个网站上找到了一些参考资料。他们使用curdoc()中的add\u periodic\u callback函数刷新图表。通过运行bokeh-serve-filename.py,这可以正常工作。我在django中尝试了这一点,在我的view.py中使用了这段代码 def bokeh(request): import numpy as np from bokeh.layouts import column from bo

我试图在django实现bokeh live图表。我在一个网站上找到了一些参考资料。他们使用
curdoc()
中的
add\u periodic\u callback
函数刷新图表。通过运行
bokeh-serve-filename.py
,这可以正常工作。我在django中尝试了这一点,在我的view.py中使用了这段代码

def bokeh(request):
    import numpy as np
    from bokeh.layouts import column
    from bokeh.models import Button
    from bokeh.palettes import RdYlBu3
    from bokeh.plotting import figure, curdoc
    import pandas as pd        

    # create a plot and style its properties
    p = figure(x_axis_type="datetime", title="EUR USD", plot_width=1000)
    p.grid.grid_line_alpha = 0
    p.xaxis.axis_label = 'Date'
    p.yaxis.axis_label = 'Price'
    p.ygrid.band_fill_color = "olive"
    p.ygrid.band_fill_alpha = 0.1



    # add a text renderer to out plot (no data yet)
    r = p.line(x = [], y = [], legend='close', color='navy')

    i = 0

    ds = r.data_source

    # create a callback that will add a number in a random location
    def callback():
        global i
        a = fxdata()[0] ## this script will return EURUSD forex data as list

        ds.data['x'].append(np.datetime64(str(a[1]))) 
        ds.data['y'].append(np.float(a[2]))    

        ds.trigger('data', ds.data, ds.data)
        i = i + 1

    # add a button widget and configure with the call back
    button = Button(label="Press Me")
    # button.on_click(callback)

    # put the button and plot in a layout and add to the document
    curdoc().add_root(column(button, p))
    curdoc().add_periodic_callback(callback, 1000)
    script, div = components(curdoc())

    return render_to_response( 'bokeh/index.html', {'script' : script , 'div' : div} )  
当我尝试这段代码时,我得到了空的bokeh图表框架作为输出。我们可以在Django中使用
add\u periodic\u callback
函数来实现这一点吗?或以任何其他类似方式刷新图表

谁能帮帮我吗。如果你觉得这不可理解,请在这里评论。
谢谢你的帮助。提前感谢。

不要使用
curdoc()
而是使用
bokeh中的
components()
函数。嵌入
,然后使用ajax使其生效

`def bokeh(request):


      window_size = 100
        window = np.ones(window_size) / float(window_size)
        #data_avg = np.convolve(data, window, 'same')
        TOOLS = "resize,pan,wheel_zoom,box_zoom,reset,save"
        p2 = figure(x_axis_type="datetime", title="abc", plot_width=1000)
        p2.grid.grid_line_alpha = 0
        p2.xaxis.axis_label = 'Date'
        p2.yaxis.axis_label = 'Price'
        p2.ygrid.band_fill_color = "olive"
        p2.ygrid.band_fill_alpha = 0.1

        p2.line(data_dates, data,  legend='close', color='navy')
        # p2.line(data_dates, data2, legend='SMA-15', color='green')


        #p2.line(data_dates, data_avg, legend='avg', color='navy')
        p2.legend.location = "top_left"
        script, div = components(p2)

        return render_to_response( 'bindex.html', {'script' : script , 'div' : div} )`
阿贾克斯:


函数刷新_bokeh(){
$.ajax({
url:“{%url”bokeh“%}”,
成功:功能(数据){
$('#bo').html(数据);
}
});
}
setTimeout(函数(){
刷新_bokeh();
}, 2000);
Html:

{%load staticfiles%}
{{script | safe}}
{{div | safe}}
{%csrf_令牌%}

我认为您可以使用嵌入式服务器文档:

from bokeh.embed import server_document
script = server_document("https://demo.bokeh.org/slider")
这将输出bokeh服务器页面的
,您可以将其嵌入到文档中

资料来源:

请检查您是否认为这有帮助,我觉得在这个示例中,他运行bokeh服务器并连接到Django端口有些复杂。在我的例子中,它是一个大的应用程序和博克图表的一部分。谢谢老兄!!!!,但是你能告诉我如何使用curdoc或添加定期回调()这是我主要关注的。无论如何,谢谢你的回答。。
from bokeh.embed import server_document
script = server_document("https://demo.bokeh.org/slider")