0

I have been working on COVID19 analysis for a dashboard and am using a JSON data source. I have converted the json to dataframe. I am working on plotting bar chart for "Days to reach deaths" over a "States" x-axis (categorical values). I am trying to use a function to update the slider.value. Upon running the bokeh serve with --log-level=DEBUG, I am getting a following error:

enter image description here

Can someone provide me with any direction or help with what might be causing the issue as I am new to Python and any help is appreciated? Or if there's any other alternative.

Please find the code below:

cases_summary = requests.get('https://api.rootnet.in/covid19-in/stats/history')

json_data = cases_summary.json()

#Data Cleaning
cases_summary=pd.json_normalize(json_data['data'], record_path='regional', meta='day')

cases_summary['loc']=np.where(cases_summary['loc']=='Nagaland#', 'Nagaland', cases_summary['loc'])
cases_summary['loc']=np.where(cases_summary['loc']=='Madhya Pradesh#', 'Madhya Pradesh', cases_summary['loc'])
cases_summary['loc']=np.where(cases_summary['loc']=='Jharkhand#', 'Jharkhand', cases_summary['loc'])

#Calculate cumulative days since 1st case for each state
cases_summary['day_count']=(cases_summary['day'].groupby(cases_summary['loc']).cumcount())+1

#Initial plot for default slider value=35 
days_reach_death_count=cases_summary.loc[(cases_summary['deaths']>=35)].groupby(cases_summary['loc']).head(1).reset_index()

slider = Slider(start=10, end=max(cases_summary['deaths']), value=35, step=10, title="Total Deaths")

source = ColumnDataSource(data=dict(days_reach_death_count[['loc','day_count', 'deaths']]))

q = figure(x_range=days_reach_death_count['loc'], plot_width=1200, plot_height=600, sizing_mode="scale_both")


q.title.align = 'center'
q.title.text_font_size = '17px'
q.xaxis.axis_label = 'State'
q.yaxis.axis_label = 'Days since 1st Case'
q.xaxis.major_label_orientation = math.pi/2

q.vbar('loc', top='day_count', width=0.9, source=source)

deaths = slider.value
q.title.text = 'Days to reach %d Deaths' % deaths

hover = HoverTool(line_policy='next')
hover.tooltips = [('State', '@loc'),
                  ('Days since 1st Case', '@day_count'),  # @$name gives the value corresponding to the legend
                  ('Deaths', '@deaths')
                  ]
q.add_tools(hover)

def update(attr, old, new):
    days_death_count = cases_summary.loc[(cases_summary['deaths'] >= slider.value)].groupby(cases_summary['loc']).head(1).reindex()
    source.data = [ColumnDataSource().from_df(days_death_count)]

slider.on_change('value', update)

layout = row(q, slider)
tab = Panel(child=layout, title="New Confirmed Cases since Day 1")
tabs= Tabs(tabs=[tab])

curdoc().add_root(tabs)

1 Answers1

0

Your code has 2 issues

  • (critical) source.data must be a dictionary, but you're assigning it an array
  • (minor) from_df is a class method, you don't have to construct an object of it

Try using source.data = ColumnDataSource.from_df(days_death_count) instead.

Eugene Pakhomov
  • 9,309
  • 3
  • 27
  • 53