I am trying to implement bokeh live chart in django. I got some reference in one website. They used add_periodic_callback
function in curdoc()
to refresh the chart. This is working fine by running bokeh serve filename.py
. I tried this in django by using this code in my 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} )
when I try this code i'm getting empty bokeh chart frame as output. can we do this by using add_periodic_callback
function in Django? or any other similar way to refresh the chart.
can anyone please help me. if you feel its not understandable please comment here. your help will be appreciated . Thanks in advance.