Advertisement
pasholnahuy

Untitled

Nov 5th, 2023
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. def get_df(sellers, items, products, orders, translation):
  2.     my_sellers = sellers[['seller_id', 'seller_state']].drop_duplicates()
  3.     merged_df = my_sellers.merge(items, on = 'seller_id')
  4.     df = products.merge(merged_df, on = 'product_id')
  5.     df = df[['seller_state', 'product_category_name', 'order_id']].merge(orders[['order_status', 'order_id', 'order_delivered_customer_date']], on = 'order_id')
  6.     df = df.merge(translation, on = "product_category_name")
  7.     df.drop(columns = ['product_category_name'], inplace = True)
  8.     df.rename(columns = {'product_category_name_english': 'category', 'seller_state': 'state', 'order_status': 'status', 'order_delivered_customer_date' : 'year'}, inplace = True)
  9.     df['year'] = df['year'].apply(lambda x: pd.to_datetime(x).year)
  10.     return df
  11.    
  12.  
  13.  
  14. from dash import Dash, dcc, html, Input, Output
  15. app = Dash(__name__)
  16.  
  17. df = get_df(sellers, items, products, orders, translation)
  18.  
  19. app.layout = html.Div([
  20.     dcc.Graph(id='sellers-graph'),
  21.     dcc.RangeSlider(
  22.         df['year'].min(),
  23.         df['year'].max(),
  24.         step=None,
  25.         value = [df['year'].min(), df['year'].max()],
  26.         marks={str(year): str(year) for year in df['year'].unique()},
  27.         id='year-slider'
  28.     ),
  29.     dcc.Checklist(
  30.         df['status'],
  31.         df['status'],
  32.         id = 'status-filter'
  33.     ),
  34.     dcc.Dropdown(
  35.         df['state'],
  36.         df['state'][0],
  37.         id = 'state-dropdown'
  38.     )
  39.    
  40. ])
  41.  
  42.  
  43.  
  44. @app.callback(
  45.     Output('sellers-graph', 'figure'),
  46.     [Input('year-slider', 'value'),
  47.      Input('state-dropdown', 'value'),
  48.      Input('status-filter', 'value')])
  49.  
  50. def update_figure(years_to_display, state_to_display, statuses_filter):
  51.     cur_df = get_df(sellers, items, products, orders, translation)
  52.     cur_df = cur_df.groupby(['state', 'category', 'status']).agg({'order_id' : 'count'}).reset_index()
  53.     df.rename(columns = {'order_id' : 'count'}, inplace = True)
  54.     cur_df = cur_df[cur_df['status'].isin(statuses_filter)]
  55.     if state_to_display != 'All':
  56.         cur_df = cur_df[cur_df.state == state_to_display]
  57.     cur_df = cur_df.loc[cur_df.year <= years_to_display[1]]
  58.     cur_df = cur_df.loc[cur_df.year >= years_to_display[0]]
  59.     fig = px.bar(cur_df, y="state", x="count", color="category",
  60.                  title="Distribution by sales categories", orientation='h', height=300)
  61.     fig.update_layout(transition_duration=500)
  62.     return fig
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement