✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Selecting country by dropdown menu

Hiya

I’m just getting to grips with dash and trying to recreate some graphs I made with plotly.

I have a master.csv stored in a dataframe with info for many countries. I want to use a dropdown to select country and graph variables.

Here is my code:

import requests
import requests_cache
import time

import json

from functools import reduce

import pandas as pd
import numpy as np
from pandas import Series, DataFrame, json_normalize

from datetime import datetime


import plotly.offline as pyo
import plotly.graph_objs as go

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

# Launch the application:
app = dash.Dash()

# Read csv to DataFrame call and store the response.
df = pd.read_csv('data\master.csv')

df.fillna(0,inplace=True)
#print(df)

country_options = []
for country in df['Country'].unique():
    country_options.append({'label':str(country),'value':country})

#Create a Div to hold graph
app.layout = html.Div([
    dcc.Graph(id='graph'),
    dcc.Dropdown(id='country-picker',options=country_options,value=df['Country'].min())
])

@app.callback(Output('graph','figure'),
              [Input('country-picker', 'value')])

def update_country(selected_country):
    filtered_df = df[df['Country'] == selected_country]

    traces = []
    #for country in filtered_df.unique():
    traces.append(go.Bar(
        x=filtered_df['datetime'],
        y=filtered_df['New Cases']
    ))

    return {
        'data': traces,
        'layout': go.Layout(
            xaxis={ 'title': 'Date'},
            yaxis={'title': 'New Cases'},
            hovermode='closest'
        )
    }





if __name__ == '__main__':
    app.run_server(debug=True)

I’m not quite sure how to go about selecting info in the function to select county. I am summing all of the values instead of creating a unique value for each date. Is there another way to go about this?

Here is a readout of my df headers:
Country
Active
datetime
New Cases
SMA_7 New
Pct Ch New