StoreSalesForecasting / src /streamlit_app.py
handex's picture
Update src/streamlit_app.py
11741b6 verified
Raw
History Blame Contribute Delete
3.1 kB
import streamlit as st
import pandas as pd
import plotly.express as px
# 1. Page Configuration
st.set_page_config(page_title="Store Sales Forecast", page_icon="🛒", layout="wide")
# 2. Title and Description
st.title("🛒 Store Sales Forecasting")
st.markdown("""
This application visualizes the 16-day future sales predictions for retail stores in Ecuador.
The forecasts are generated using an **XGBoost** machine learning model trained on historical data,
incorporating features like seasonality, oil prices, and holidays.
""")
# 3. Load Data Function (Cached for performance)
@st.cache_data
def load_data():
# Load the CSV file we prepared earlier
df = pd.read_csv("src/dashboard_data.csv")
# Ensure date column is in datetime format
if 'date' in df.columns:
df['date'] = pd.to_datetime(df['date'])
return df
try:
df = load_data()
# --- SIDEBAR (FILTERS) ---
st.sidebar.header("Filter Options")
# Store Selector
if 'store_nbr' in df.columns:
store_list = sorted(df['store_nbr'].unique())
selected_store = st.sidebar.selectbox("Select Store Number", store_list)
else:
st.error("Column 'store_nbr' not found in dataset.")
st.stop()
# Family Selector
if 'family' in df.columns:
family_list = sorted(df['family'].unique())
selected_family = st.sidebar.selectbox("Select Product Category (Family)", family_list)
# --- MAIN CONTENT ---
# Filter data based on user selection
filtered_data = df[(df['store_nbr'] == selected_store) & (df['family'] == selected_family)]
# Sort by date for proper plotting
if 'date' in filtered_data.columns:
filtered_data = filtered_data.sort_values('date')
# Plot Chart using Plotly
st.subheader(f"📅 Forecast for Store #{selected_store} - Category: {selected_family}")
fig = px.line(filtered_data, x='date', y='Predicted_Sales',
title='16-Day Sales Prediction Trend',
labels={'Predicted_Sales': 'Predicted Sales Volume', 'date': 'Date'},
markers=True)
# --- DÜZELTİLEN KISIM (FIXED PART) ---
# Renk ayarını 'update_traces' içine aldık (Layout hatasını çözer)
fig.update_traces(line_color='#00CC96')
# Layout ayarları sadece hover (üzerine gelince çıkan bilgi) için kaldı
fig.update_layout(hovermode="x unified")
# -------------------------------------
st.plotly_chart(fig, use_container_width=True)
# Show Data Table
with st.expander("View Detailed Forecast Data"):
st.dataframe(filtered_data[['id', 'date', 'store_nbr', 'family', 'Predicted_Sales']])
else:
st.warning("Date column is missing. Cannot plot the graph.")
except FileNotFoundError:
st.error("Error: 'dashboard_data.csv' not found. Please upload the file to Hugging Face Files.")
except Exception as e:
st.error(f"An unexpected error occurred: {e}")