Right way to plot live data with django and bokeh

后端 未结 3 1675
醉酒成梦
醉酒成梦 2021-02-19 08:20

i have a bokeh plot embedded in a django app. I create the plot in the django view and push the plot to the bokeh server to show the plot in my webpage.

#view.p         


        
3条回答
  •  梦如初夏
    2021-02-19 08:57

    Something like this worked for me:

    #views.py
    from bokeh.plotting import figure, curdoc
    from bokeh.client import pull_session
    
    def my_line_chart(request):
        session = pull_session(url = "http://localhost:5006/myapp")
        bokeh_script=autoload_server(None,url = "http://localhost:5006/myapp", session_id= session.id)
        return render(request, u'line_charts.html', {u'the_script': bokeh_script})
    

    Then on your bokeh server use source.stream()

        #myapp
        '''
        everything else here
        '''
        def update():
            new_data = qu() #qu is the newdata to be updated
            source.stream(new_data, rollover = 60)
            print(source.data) #if you want to see new data
    
        curdoc().add_root(p)
        curdoc().add_periodic_callback(update,10000)
    

    then start your bokeh server and allow connection from django like

    bokeh serve --allow-websocket-origin=127.0.0.1:8000 myapp.py #you can add app2.py too
    

    I used port 8000 because that is my django port, and port 5006 on views.py because it is my tornado port.

    Check class columndatasource for more

    Hope it helps.

提交回复
热议问题