我希望在Django中使用Bokeh实现实时图表。 我尝试了许多方法,但没有任何方法适合我。 我有一段代码可以正常工作bokeh serve
,但在Django中无法正常运行。 有人能帮我吗? 我已经卡了一个星期了。 代码如下:
import numpy as np
from bokeh.client.session import push_session
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
import random
from bokeh.embed import components
# 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
# p.border_fill_color = 'black'
# p.background_fill_color = 'black'
# p.outline_line_color = None
# p.grid.grid_line_color = None
# add a text renderer to out plot (no data yet)
r = p.line(x = [], y = [], legend='close', color='navy')
s = p.triangle(x = [], y = [], legend='sell', color='red' ,size=10 )
t = p.triangle(x = [], y = [], legend='buy', color='green' ,size=10 )
# r = p.text(x=[], y=[], text=[], text_color=[], text_font_size="20pt",
# text_baseline="middle", text_align="center")
i = 0
dr = r.data_source
ds = s.data_source
dt = t.data_source
# create a callback that will add a number in a random location
def callback():
global i
a=fxdata()[0]
val = np.random.random()*70 + 15
dr.data['x'].append(i) # np.datetime64(str(a[1]))
dr.data['y'].append( val ) # np.float(a[2])
rm = random.randint(1,10)
if rm <= 2:
ds.data['x'].append(i) # np.datetime64(str(a[1]))
ds.data['y'].append(val)
elif rm >=9 :
dt.data['x'].append(i) # np.datetime64(str(a[1]))
dt.data['y'].append(val)
else:
pass
# ds.data['text_color'].append(RdYlBu3[i%3])
# ds.data['text'].append(str("MJ"))
dr.trigger('data', dr.data, dr.data)
ds.trigger('data', ds.data, ds.data)
dt.trigger('data', dt.data, dt.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)
我尝试了使用随机数进行简单实时绘图。我希望在Django中也能做到同样的事情。有人可以帮忙吗?感激不尽。
select/{option}
。 - Kirill Vladi