Advertisement
pasholnahuy

Untitled

Nov 5th, 2023
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. from dash import Dash, dcc, html, Input, Output
  2. app = Dash(__name__)
  3.  
  4. app.layout = html.Div([
  5. html.Label('Order statuses to display:'),
  6. dcc.Checklist(
  7. all_statuses,
  8. [df['status'][0]],
  9. id = 'status-filter'
  10. ),
  11. html.Label('State:', style={'font-size': '120%'}),
  12. html.Div(dcc.Dropdown(
  13. all_states,
  14. 'All',
  15. id = 'state-dropdown'
  16. )),
  17. dcc.Graph(id='sellers-graph'),
  18. dcc.Graph(id='customers-graph'),
  19. dcc.RangeSlider(
  20. min=0,
  21. max=all_dates.shape[0] - 1,
  22. step=1,
  23. dots=False,
  24. value=[0, all_dates.shape[0] - 1],
  25. marks = marks,
  26. id='date-slider',
  27. ),
  28. ])
  29.  
  30.  
  31.  
  32. @app.callback(
  33. Output('sellers-graph', 'figure'),
  34. [Input('date-slider', 'value'),
  35. Input('state-dropdown', 'value'),
  36. Input('status-filter', 'value')])
  37.  
  38. def update_figure(time_gap, state_to_display, statuses_filter):
  39. cur_df = get_df_sellers(sellers, items, products, orders, translation)
  40. cur_df = cur_df[cur_df['status'].isin(statuses_filter)]
  41. if state_to_display == 'All':
  42. cur_df['state'] = 'All'
  43. else:
  44. cur_df = cur_df[cur_df.state == state_to_display]
  45. cur_df = cur_df[pd.to_datetime(sorted(all_dates['date'])[time_gap[0]]) <= pd.to_datetime(cur_df.date)]
  46. cur_df = cur_df[pd.to_datetime(cur_df.date) <= pd.to_datetime(sorted(all_dates['date'])[time_gap[1]])]
  47. cur_df = cur_df.groupby(['category']).agg({'count' : 'count', 'state':'first'}).reset_index()
  48. all_count = np.einsum('i->', cur_df['count'])
  49. cur_df['count'] = cur_df['count'] * 100/all_count
  50. cur_df.rename(columns = {"count":"propotion"})
  51. fig = px.bar(cur_df, y="state", x="count", color="category",
  52. title="Distribution by sales categories", orientation='h', height=300)
  53. fig.update_layout(transition_duration=500)
  54. return fig
  55.  
  56. @app.callback(
  57. Output('customers-graph', 'figure'),
  58. [Input('date-slider', 'value'),
  59. Input('state-dropdown', 'value'),
  60. Input('status-filter', 'value')])
  61. def update_figure(time_gap, state_to_display, statuses_filter):
  62. cur_df = get_df_customers(customers, items, products, orders, translation)
  63. cur_df = cur_df[cur_df['status'].isin(statuses_filter)]
  64. if state_to_display == 'All':
  65. cur_df['state'] = 'All'
  66. else:
  67. cur_df = cur_df[cur_df.state == state_to_display]
  68. cur_df = cur_df[pd.to_datetime(sorted(all_dates['date'])[time_gap[0]]) <= pd.to_datetime(cur_df.date)]
  69. cur_df = cur_df[pd.to_datetime(cur_df.date) <= pd.to_datetime(sorted(all_dates['date'])[time_gap[1]])]
  70. cur_df = cur_df.groupby(['category']).agg({'count' : 'count', 'state':'first'}).reset_index()
  71. all_count = np.einsum('i->', cur_df['count'])
  72. cur_df['count'] = cur_df['count'] * 100/all_count
  73. cur_df.rename(columns = {"count":"propotion"})
  74. fig = px.bar(cur_df, y="state", x="count", color="category",
  75. title="Distribution by customers categories", orientation='h', height=300)
  76. fig.update_layout(transition_duration=500)
  77. return fig
  78. if __name__ == '__main__':
  79. app.run_server(debug=True)
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement