Hi Everyone, I am developing my first app on the dash. Below is my code and I am getting an error in a callback. -
File “”, line 183, in update_cards
** dff_1 = dff[(dff[‘District’]==dist_val) & ((dff[‘TP date’]>=date(start_date)) & (dff[‘TP date’]<=date(end_date)))]**
TypeError: an integer is required (got type str)
I tried different things but still don’t know how to fix them. Help me, please.
app.layout = html.Div([
dbc.Container([
#---------- Row 1
dbc.Row([
dbc.Col([
dbc.Card([
html.H2(‘Hi Dash’,style={‘textAlign’:‘center’},
className=‘font-weight-bold’)
],color=‘info’,inverse=True)
],width=12)
],className=‘my-4’),
#---------- Row 2
dbc.Row([
dbc.Col([
dbc.Card([
dbc.CardBody([
dcc.Dropdown(id = ‘dist_dropdown’,
multi= True,
options=[{‘label’:x, ‘value’:x} for x in sorted(df[‘District’].unique())],
placeholder= ‘District’,
style={‘color’:‘black’},
searchable=True)
])
],color=‘secondary’,inverse=True)
],width=4),
dbc.Col([
dbc.Card([
dbc.CardBody([
dcc.DatePickerRange(id=‘choosen_date’,
min_date_allowed=date(2021, 1, 1),
initial_visible_month=date.today(),
number_of_months_shown=2,
updatemode=‘bothdates’,
with_portal=True,
className=‘ml-5’)
])
],color=‘secondary’,inverse=True)
],width=4),
dbc.Col([
dbc.Card([
dbc.CardBody([
dcc.RadioItems(id = ‘select_opt’,
options=[{‘label’:‘First Option’,‘value’:‘FO’},
{‘label’:‘Second Option’,‘value’:‘SO’,‘disabled’:‘True’},
{‘label’:‘Third Option’,‘value’:‘TO’,‘disabled’:‘True’}],
value=‘FO’,
labelStyle={‘fontSize’:‘20px’,‘margin-right’:‘20px’,
‘display’:‘inline-block’,‘align’:‘center’},
inputStyle={“margin-right”: “10px”})
])
],color=‘secondary’,inverse=True)
],width=4),
]),
],fluid=True),
#---------- Row 3
dbc.Container([
dbc.Row([
dbc.Col([
dbc.CardDeck([
dbc.Card([
dbc.CardHeader(Lottie(options=options, width=“50%”, height=“100%”,url=url_trips)),
dbc.CardBody([
html.H5(‘Total Count’),
html.H3(id = ‘card1’ , children=‘000000’)
], style={‘textAlign’:‘center’})
]),
dbc.Card([
dbc.CardHeader(Lottie(options=options, width=“50%”, height=“100%”,url=url_lifting)),
dbc.CardBody([
html.H5(‘cumulative_sum’),
html.H3(id = ‘card2’ , children=‘000000’)
], style={‘textAlign’:‘center’})
]),
dbc.Card([
dbc.CardHeader(Lottie(options=options, width=“50%”, height=“100%”,url=url_tp)),
dbc.CardBody([
html.H5(id = ‘card3_head’),
html.H3(id = ‘card3’ , children=‘000000’)
], style={‘textAlign’:‘center’})
]),
dbc.Card([
dbc.CardHeader(Lottie(options=options, width=“50%”, height=“100%”,url=url_vehicle)),
dbc.CardBody([
html.H5(‘Total’),
html.H3(id = ‘card4’ , children=‘000000’)
], style={‘textAlign’:‘center’})
])
],className=‘h-50 w-40’)
])
],className=‘mb-2’),
dbc.Row([
dbc.Col([
dbc.Card([
dbc.CardBody([
dcc.Graph(id = ‘graph1’,figure={}, config={‘displayModeBar’: False})
])
])
],width=3),
dbc.Col([
dbc.Card([
dbc.CardBody([
dcc.Graph(id = ‘graph2’,figure={}, config={‘displayModeBar’: False})
])
])
],width=6),
dbc.Col([
dbc.Card([
dbc.CardBody([
dcc.Graph(id = ‘graph3’,figure={}, config={‘displayModeBar’: False})
])
])
],width=3)
],className='mb-2')
], fluid=True ,className='my-4')
])
####################################
#-------- callback
#------------- Updating the 4 number cards
@app.callback(
Output(‘card1’,‘chidren’),
Output(‘card2’,‘chidren’),
Output(‘card3’,‘chidren’),
Output(‘card4’,‘chidren’),
Output(‘card3_head’,‘chidren’),
[Input(‘choosen_date’,‘start_date’),
Input(‘choosen_date’,‘end_date’)],
[State(‘select_opt’,‘value’),
State(‘dist_dropdown’,‘value’)]
)
def update_cards(start_date,end_date,opt_val,dist_val):
if opt_val=='FO':
dff = df.copy()
dff_1 = dff[(dff['District']==dist_val) & ((dff['TP date']>=date(start_date)) & (dff['TP date']<=date(end_date)))]
count1 = len(dff_1['var1'].unique())
sum1 = pd.to_numeric(dff_1['var2']).sum()
count2 = dff_1['var3'].nunique()
total1= dff_1['var'].nunique()
header_name = "TP Created"
else:
raise dash.exceptions.PreventUpdate
return count1 , sum1 , total1, count2 , header_name
####################################
if name == ‘main’:
app.run_server(debug=False,port = 8010)