Hello,
I am trying to run Bokeh visualizations inside a Django framework.
The goal is for the user to import a csv file, which, when uploaded, renders an html with bokeh plots that I have specified.
I have successfully built the views and connected the urls properly, and my plot renders after the csv is uploaded. The problem is, my python code includes Select Tool and a Slider callbacks, which do not modify my plots as the plots render on standalone HTML/JS.
Bokeh documentation recommends using the CustomJS model and gives a few examples: JavaScript callbacks — Bokeh 2.4.2 Documentation
but I am fairly new to Bokeh and Django and am unsure how my specific callback code needs to be modified with CustomJS. Here is the relevant code snippet:
def csv(request):
if request.method == ‘POST’:
excel_file = request.FILES[‘excel_file’]
excel = pd.read_csv(excel_file)
excel_df = pd.DataFrame(excel)
excel_df_avg = excel_df.groupby(‘Year’).agg(‘mean’)
excel_df_avg = excel_df_avg.reset_index()
source1 = ColumnDataSource(data = {
'x' : excel_df_avg['Year'],
'top' : excel_df_avg['Residential_Rate'],
})
ymin1,ymax1 = min(excel_df_avg.Residential_Rate), max(excel_df_avg.Residential_Rate)
xmin1,xmax1 = min(excel_df_avg.Year),max(excel_df_avg.Year)
plot1 = figure(title='Galena Rate Data',plot_height=400, plot_width=700)
plot1.vbar(x='x',top='top',width=0.5,source=source1)
plot1.xaxis.axis_label = 'Year'
plot1.yaxis.axis_label = 'Residential Rate ($/kWh)'
def update_plot1(attr,old,new):
y1 = y1_select.value
new1_data = {
'x' : excel_df_avg['Year'],
'top' : excel_df_avg[y1]
}
source1.data = new1_data
plot1.yaxis.axis_label = y1
y1_select = Select(
options=['Residential_Rate','PCE Rate ($/kWh)','Effective Rate ($/kWh)','Fuel Price ($/gal)','Diesel Efficiency (kWh/gal)'],
value='Residential_Rate',
title='Y-Axis Options'
)
y1_select.on_change('value', update_plot1)