I am very new to dash and have been toiling around this problem for while, could anyone help?
ive got a daterangepicker which then finds values in a df, does some caluclations and finds pecentages in integers.
The calculations work find because if i ask the dash to return the percentage split (e.g 60,40) it shows me the integers perfectly, its only when i return the data and layout does an error message come up as
navy_trouser_culm=int(((len(ref_df[ref_df['product_title'].str.contains('Navy Blue')]))/(len(ref_df)))*100)
ZeroDivisionError: division by zero
I have no idea how this could be possible as ive checked the numbers and there is no 0, and as ive said before when i return navy_trousers_culm to be printed in the dashboard it says gives a number.
Below is my code:
app = dash.Dash()
app.layout = html.Div([
dcc.DatePickerRange(
id='my-date-picker-range',
min_date_allowed=dt(2019, 4, 1),
max_date_allowed=dt(2019, 6, 30),
#initial_visible_month=dt(2019, 4, 1),
end_date=dt(2019, 4, 2),
start_date=dt(2019, 4, 1)
),
html.Div(id='output-container-date-picker-range')
])
@app.callback(
dash.dependencies.Output('output-container-date-picker-range', 'children'),
[dash.dependencies.Input('my-date-picker-range', 'start_date'),
dash.dependencies.Input('my-date-picker-range', 'end_date')])
def update_output(start_date, end_date):
start_day=str(start_date.split('-')[2]).split('T')[0]
start_month=str(start_date.split('-')[1])
start_year=str(start_date.split('-')[0])
end_day=str(end_date.split('-')[2]).split('T')[0]
end_month=str(end_date.split('-')[1])
end_year=str(end_date.split('-')[0])
start_date_ref=start_day+'/'+start_month+'/'+start_year
end_date_ref=end_day+'/'+end_month+'/'+end_year
#finds earliest and latest dates for start_date_ref and end_date_ref
#if the dates arent available in the data
if start_date_ref not in days:
start_date_ref=find_earlier_start_date(start_date_ref)
if end_date_ref not in days:
end_date_ref=find_later_end_date(end_date_ref)
start_index=list(df[df['day']==end_date_ref].index.values)[0]
end_index=list(df[df['day']==start_date_ref].index.values)[-1]
#as df is ordered earliest at the top the end date is the start index
ref_df=df[start_index:end_index+1]
#return(str(len(ref_df)))
navy_trouser_culm=int(((len(ref_df[ref_df['product_title'].str.contains('Navy Blue')]))/(len(ref_df)))*100)
sand_trouser_culm=int(((len(ref_df[ref_df['product_title'].str.contains('Sand')]))/(len(ref_df)))*100)
return{
'data': [go.Pie(labels=['Navy','Sand'], values=[50,50],
marker={'colors': ['#EF963B','C93277']},textinfo='label')
],
'layout': go.Layout(
title='distribution',
margin={'l': 100, 'r': 10},
legend={'x': 0, 'y': 1.5},
)
}
if __name__ == '__main__':
app.run_server()