Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import plotly.express as px | |
| import gradio as gr | |
| # Load data | |
| df = pd.read_excel("report.xlsx", engine='openpyxl') | |
| df['date'] = pd.to_datetime(df['date'], dayfirst=True, errors='coerce') | |
| df['weekday'] = df['date'].dt.day_name() | |
| df['hour'] = df['date'].dt.hour | |
| # Define time blocks | |
| bins = [-1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 24] | |
| labels = [ | |
| 'Before Starting', '11:00-11:59', '12:00-12:59', '13:00-13:59', | |
| '14:00-14:59', '15:00-15:59', '16:00-16:59', '17:00-17:59', | |
| '18:00-18:59', '19:00-19:59', 'After closing' | |
| ] | |
| df['time_block'] = pd.cut(df['hour'], bins=bins, labels=labels, right=False) | |
| # Weekday options in temporal order | |
| ordered_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] | |
| available_days = [d for d in ordered_days if d in df['weekday'].unique()] | |
| # Function to generate plots | |
| def plot_day(day): | |
| df_day = df[df['weekday'] == day] | |
| avg = ( | |
| df_day.groupby('time_block')['total'] | |
| .mean() | |
| .reindex(labels) | |
| .reset_index(name='Average') | |
| ) | |
| count = ( | |
| df_day.groupby('time_block') | |
| .size() | |
| .reindex(labels) | |
| .reset_index(name='Count') | |
| ) | |
| total = ( | |
| df_day.groupby('time_block')['total'] | |
| .sum() | |
| .reindex(labels) | |
| .reset_index(name='Total') | |
| ) | |
| fig1 = px.bar(avg, x='time_block', y='Average', title=f"Average total per sale ({day})") | |
| fig2 = px.bar(count, x='time_block', y='Count', title=f"Number of sales ({day})") | |
| fig3 = px.bar(total, x='time_block', y='Total', title=f"Total revenue ({day})") | |
| return fig1, fig2, fig3 | |
| # Gradio app with custom layout | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🛍️ Retail Demand Dashboard") | |
| gr.Markdown("Visualize sales metrics by time block, filtered by weekday") | |
| with gr.Row(): | |
| dropdown = gr.Dropdown(choices=available_days, label="Select Day") | |
| with gr.Row(): | |
| avg_plot = gr.Plot(label="Average Total") | |
| with gr.Row(): | |
| count_plot = gr.Plot(label="Number of Sales") | |
| with gr.Row(): | |
| revenue_plot = gr.Plot(label="Total Revenue") | |
| dropdown.change( | |
| fn=plot_day, | |
| inputs=dropdown, | |
| outputs=[avg_plot, count_plot, revenue_plot] | |
| ) | |
| # Launch app | |
| if __name__ == "__main__": | |
| demo.launch() | |