1

I have a script to plot a line graph of covid-19 deaths using bokeh in python. I've added a dropdown menu which lets the user choose a country to plot the new_deaths_per_million based on a datetime axis (date). My problem is that the dropdown menu is not linked/working.

I have already tried to rewrite the callback function in different ways based on similair cases on the internet but stil not managed to fix the problem.

My code looks the following:

import pandas as pd
import numpy as np
from bokeh.models import ColumnDataSource, Select
from bokeh.plotting import figure
from bokeh.layouts import row
from bokeh.io import curdoc
#Loading in Dataframe
kolommen=['iso_code', 'new_deaths','continent', 'location', 'date', 'total_deaths_per_million', 'new_deaths_per_million','people_vaccinated_per_hundred', 'stringency_index']
df=pd.read_csv('owid-covid-data.csv', usecols=kolommen)
# Create ColumnDataSource: source
source = ColumnDataSource(data={
    'date' : df.date,
    'new_deaths_per_million' : df.new_deaths_per_million,
    'new_deaths' : df.new_deaths,
    'location': df.location
})
# Create a new plot: plot
plot = figure(plot_height=400, plot_width=700, x_axis_type='datetime')
# Add circles to the plot
plot.line('date', 'new_deaths_per_million', source=source)
# Define a callback function: update_plot
def update_plot(attr, old, new):
    # Set the yr name to slider.value and new_data to source.data
    land = select.value
    new_data = {
        'date'    : df[df['location']==land].date,
        'new_deaths_per_million': df[df['location']==land].new_deaths_per_million,
        'new_deaths' : df[df['location']==land].new_deaths_per_million,
        'locations': df[df['location']==land].location
    }
    source= new_data
# Create a dropdown Select widget: select    
select = Select(title="Landkeuze", options=df.location.unique().tolist(), value='Netherlands')
# Attach the update_plot callback to the 'value' property of select
select.on_change('value', update_plot)
# Create layout and add to current document
layout = row(select, plot)
curdoc().add_root(layout)

My dataframe looks like the following: iso_code continent location date new_deaths total_deaths_per_million new_deaths_per_million people_vaccinated_per_hundred stringency_index AFG Asia Afghanistan 2021-01-29 2.0 61.626 0.051 NaN 12.04 AFG Asia Afghanistan 2021-01-30 1.0 61.652 0.026 NaN 12.04

enter image description here

ravenb
  • 13
  • 3

0 Answers0