I have just started learning Dash and I have been facing a problem for a few days and my learning has stopped.
I want to display the number of cases and deaths in dataframe on “#new-cases-display” and “#deaths-display” ID based on the selected country, month and day
but this does not happen, I tried a lot but did not understand where the problem is.
dataset available here
import dash
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import plotly.express as plotly
import pandas
import numpy
df = pandas.read_csv('covid19_dataset.csv')
country_list = list(numpy.unique(df['country']))
country_options = [{'label': x, 'value': x} for x in country_list]
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.CYBORG])
app.layout = html.Div(
[
html.Div(
[
dbc.Row(
[
dbc.Col(
[
html.Div(
dbc.Col(
dbc.FormGroup(
[
dbc.Label("Select Your Country", className = "font-weight-bold font-italic"),
html.Br(),
dbc.Select(
id = 'country',
options = country_options,
value = 'Afghanistan',
)
]
)
)
),
html.Br(),
html.Div(
[
dbc.Row(
[
dbc.Col(
[
dbc.FormGroup(
[
dbc.Label('Select Month', className = "font-weight-bold font-italic"),
html.Br(),
dbc.Select(
id = 'month',
options =
[
{'label': 'January', 'value':1},
{'label': 'February', 'value':2},
{'label': 'March', 'value':3},
{'label': 'April', 'value':4},
{'label': 'May', 'value':5},
{'label': 'June', 'value':6},
{'label': 'July', 'value':7},
{'label': 'August', 'value':8},
{'label': 'September', 'value':9},
{'label': 'October', 'value':10},
{'label': 'November', 'value':11},
{'label': 'December', 'value':12},
],
value = 1
)
]
)
],
lg = 6,
md = 6,
sm = 6,
xs = 12,
),
dbc.Col(
[
dbc.FormGroup(
[
dbc.Label('Select Day', className = 'font-weight-bold font-italic'),
html.Br(),
dbc.Select(
id = 'day',
options =
[
{'label': '1', 'value':1},
{'label': '2', 'value':2},
{'label': '3', 'value':3},
{'label': '4', 'value':4},
{'label': '5', 'value':5},
{'label': '6', 'value':6},
{'label': '7', 'value':7},
{'label': '8', 'value':8},
{'label': '9', 'value':9},
{'label': '10', 'value':10},
{'label': '11', 'value':11},
{'label': '12', 'value':12},
{'label': '13', 'value':1},
{'label': '14', 'value':14},
{'label': '15', 'value':15},
{'label': '16', 'value':16},
{'label': '17', 'value':17},
{'label': '18', 'value':18},
{'label': '19', 'value':19},
{'label': '20', 'value':20},
{'label': '21', 'value':21},
{'label': '22', 'value':22},
{'label': '23', 'value':23},
{'label': '24', 'value':24},
{'label': '25', 'value':25},
{'label': '26', 'value':26},
{'label': '27', 'value':27},
{'label': '28', 'value':28},
{'label': '29', 'value':29},
{'label': '30', 'value':30},
{'label': '31', 'value':31},
],
value = 1
)
]
)
],
lg = 6,
md = 6,
sm = 6,
xs = 12)
]
)
]
),
],
lg = 5,
md = 5,
sm = 5,
xs = 12,
),
dbc.Col(
[
dbc.Row(
[
dbc.Card(
[
dbc.CardHeader("New Cases", style = {'color':'black'},
className = 'font-weight-bold font-italic'),
dbc.CardBody(
[
html.Div(
html.H5(
id = 'new-case-display',
)
),
]
)
],
color ="warning",
style={"width": 600}
),
],
),
html.Hr(),
dbc.Row(
[
dbc.Card(
[
dbc.CardHeader("Deaths", style = {'color':'black'},
className = 'font-weight-bold font-italic'),
dbc.CardBody(
[
html.Div(
html.H5(
id = 'death-display',
)
),
]
)
],
color ="danger",
style={"width": 600}
),
],
),
],
id = 'card-col',
className = 'offset-lg-2 offset-md-2 offset-sm-2 offset-xs-0',
lg = 5,
md = 5,
sm = 5,
xs = 11,
)
]
),
],
style = {'margin':40},
id = 'my_page',
)
]
)
@app.callback(Output('new-case-display','children'),
[Input('country','value'),
Input('month','value'),
Input('day','value')])
def cases_display_text(countryy, monthh, dayy):
dff = df[(df['country'] == countryy) & (df['month'] == monthh) & (df['day'] == dayy)]
case = dff['cases']
case = case.to_string(index=False)
return case
@app.callback(Output('death-display','children'),
[Input('country','value'),
Input('month','value'),
Input('day','value')])
def deaths_display_text(countryyy, monthhh, dayyy):
dff = df[(df['country'] == countryyy) & (df['month'] == monthhh) & (df['day'] == dayyy)]
death = dff['deaths']
death = death.to_string(index=False)
return death
if __name__ == '__main__':
app.run_server(debug=True, port= 8080)