import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import os
from utils import (
read_data_file,
load_and_process_data,
create_specific_chart,
AVAILABLE_CHARTS
)
# Page configuration
st.set_page_config(
page_title="داشبورد تحلیل فروش پیشرفته",
page_icon="📊",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for RTL support and styling
st.markdown("""
""", unsafe_allow_html=True)
# Header
st.markdown("""
""", unsafe_allow_html=True)
# Initialize session state
if 'data' not in st.session_state:
st.session_state.data = None
if 'kpis' not in st.session_state:
st.session_state.kpis = None
# Sidebar for file upload
with st.sidebar:
st.markdown("### 📁 بارگذاری فایل داده")
uploaded_file = st.file_uploader(
"فایل اکسل یا CSV خود را آپلود کنید",
type=['csv', 'xlsx', 'xls'],
help="فرمتهای پشتیبانی شده: CSV, XLSX, XLS"
)
if uploaded_file is not None:
with st.spinner("در حال پردازش دادهها..."):
result = load_and_process_data(uploaded_file)
if result['success']:
st.session_state.data = result['data']
st.session_state.kpis = result['kpis']
st.success("✅ فایل با موفقیت بارگذاری شد!")
else:
st.error(f"❌ خطا: {result['error']}")
st.markdown("---")
st.markdown("### 📈 راهنما")
st.markdown("""
1. فایل داده خود را آپلود کنید
2. شاخصهای کلیدی را مشاهده کنید
3. نمودارهای مختلف را انتخاب و تحلیل کنید
""")
# Main content
if st.session_state.data is not None and st.session_state.kpis is not None:
kpis = st.session_state.kpis
# KPI Cards
st.markdown("### 📊 شاخصهای کلیدی عملکرد (KPI)")
col1, col2, col3 = st.columns(3)
with col1:
st.markdown(f"""
💰 مجموع فروش خالص (بدون ارزش افزوده)
{kpis['net_sale_without_VAT']:,.0f} ریال
""", unsafe_allow_html=True)
with col2:
st.markdown(f"""
💰 مجموع فروش خالص (با ارزش افزوده)
{kpis['net_sale_with_VAT']:,.0f} ریال
""", unsafe_allow_html=True)
with col3:
st.markdown(f"""
💰 مجموع فروش ناخالص
{kpis['gross_sales']:,.0f} ریال
""", unsafe_allow_html=True)
col4, col5, col6 = st.columns(3)
with col4:
st.markdown(f"""
📦 تعداد کل محصولات
{kpis['total_product']} کالا
""", unsafe_allow_html=True)
with col5:
st.markdown(f"""
🧾 تعداد کل برندها
{kpis['total_brand']} برند
""", unsafe_allow_html=True)
with col6:
st.markdown(f"""
↩️ مجموع برگشتی خالص
{kpis['total_return']:,.0f} ریال
""", unsafe_allow_html=True)
st.markdown("---")
# Charts Section
st.markdown("### 📈 نمودارهای تحلیلی")
# Row 1
col1, col2 = st.columns(2)
with col1:
chart_1 = st.selectbox(
"نمودار ۱",
AVAILABLE_CHARTS,
index=0,
key="chart_1"
)
fig1 = create_specific_chart(chart_1, st.session_state.data)
st.plotly_chart(fig1, use_container_width=True)
with col2:
chart_2 = st.selectbox(
"نمودار ۲",
AVAILABLE_CHARTS,
index=4,
key="chart_2"
)
fig2 = create_specific_chart(chart_2, st.session_state.data)
st.plotly_chart(fig2, use_container_width=True)
# Row 2
col3, col4 = st.columns(2)
with col3:
chart_3 = st.selectbox(
"نمودار ۳",
AVAILABLE_CHARTS,
index=5,
key="chart_3"
)
fig3 = create_specific_chart(chart_3, st.session_state.data)
st.plotly_chart(fig3, use_container_width=True)
with col4:
chart_4 = st.selectbox(
"نمودار ۴",
AVAILABLE_CHARTS,
index=9,
key="chart_4"
)
fig4 = create_specific_chart(chart_4, st.session_state.data)
st.plotly_chart(fig4, use_container_width=True)
# Row 3
col5, col6 = st.columns(2)
with col5:
chart_5 = st.selectbox(
"نمودار ۵",
AVAILABLE_CHARTS,
index=8,
key="chart_5"
)
fig5 = create_specific_chart(chart_5, st.session_state.data)
st.plotly_chart(fig5, use_container_width=True)
with col6:
chart_6 = st.selectbox(
"نمودار ۶",
AVAILABLE_CHARTS,
index=12,
key="chart_6"
)
fig6 = create_specific_chart(chart_6, st.session_state.data)
st.plotly_chart(fig6, use_container_width=True)
# Data Preview
with st.expander("📋 پیشنمایش دادهها"):
st.dataframe(st.session_state.data.head(100), use_container_width=True)
else:
# Welcome message when no data is loaded
st.markdown("""
🎯 به داشبورد تحلیل فروش خوش آمدید
برای شروع، لطفاً فایل داده خود را از طریق پنل سمت چپ بارگذاری کنید
📋 ستونهای مورد نیاز در فایل:
- ماه
- کد کالا
- نام برند
- تولید کننده
- نام کالا
- مبلغ فروش ناخالص
- مبلغ تخفیف
- مبلغ فروش خالص
- مبلغ برگشتی خالص
""", unsafe_allow_html=True)
# Footer
st.markdown("---")
st.markdown("""
📊 داشبورد تحلیل فروش پیشرفته | ساخته شده با ❤️
""", unsafe_allow_html=True)