1+ import streamlit as st
2+ import pandas as pd
3+ import plotly .express as px
4+ from core .database import SessionLocal , InvoiceDB
5+ from sqlalchemy .orm import Session
6+
7+ # --- CONFIGURATION ---
8+ st .set_page_config (page_title = "DocuFlow Dashboard" , page_icon = "💰" , layout = "wide" )
9+
10+ # --- HELPER FUNCTIONS ---
11+ def get_data ():
12+ """Fetches all invoices from the database."""
13+ db : Session = SessionLocal ()
14+ try :
15+ invoices = db .query (InvoiceDB ).all ()
16+ # Convert to a format Pandas understands (list of dicts)
17+ data = [
18+ {
19+ "ID" : inv .id ,
20+ "Date" : inv .invoice_date ,
21+ "Vendor" : inv .vendor_name ,
22+ "Total ($)" : inv .total_amount ,
23+ "Filename" : inv .filename ,
24+ "Created At" : inv .created_at
25+ }
26+ for inv in invoices
27+ ]
28+ return pd .DataFrame (data )
29+ finally :
30+ db .close ()
31+
32+ # --- SIDEBAR ---
33+ st .sidebar .title ("🚀 DocuFlow" )
34+ st .sidebar .markdown ("Automated Invoice Processing Pipeline" )
35+ if st .sidebar .button ("🔄 Refresh Data" ):
36+ st .rerun ()
37+
38+ # --- MAIN DASHBOARD ---
39+ st .title ("💸 Financial Overview" )
40+
41+ # 1. Fetch Data
42+ df = get_data ()
43+
44+ if not df .empty :
45+ # 2. KPI Metrics (Top Row)
46+ total_spend = df ["Total ($)" ].sum ()
47+ total_invoices = len (df )
48+ unique_vendors = df ["Vendor" ].nunique ()
49+
50+ col1 , col2 , col3 = st .columns (3 )
51+ col1 .metric ("Total Spending" , f"${ total_spend :,.2f} " )
52+ col2 .metric ("Invoices Processed" , total_invoices )
53+ col3 .metric ("Active Vendors" , unique_vendors )
54+
55+ # 3. Charts (Middle Row)
56+ st .markdown ("### 📈 Spending Trends" )
57+
58+ # Clean up date column for charting
59+ # (Some dates might be None or messy strings, so we handle errors)
60+ df ["Date" ] = pd .to_datetime (df ["Date" ], errors = 'coerce' )
61+
62+ # Simple Bar Chart: Spend by Vendor
63+ fig_vendor = px .bar (
64+ df , x = "Vendor" , y = "Total ($)" ,
65+ title = "Spending by Vendor" ,
66+ color = "Total ($)" ,
67+ template = "plotly_dark"
68+ )
69+ st .plotly_chart (fig_vendor , use_container_width = True )
70+
71+ # 4. Data Table (Bottom Row)
72+ st .markdown ("### 📄 Recent Invoices" )
73+ st .dataframe (df , use_container_width = True )
74+
75+ else :
76+ st .info ("No data found in the database. Drag some PDFs into the folder!" )
77+
78+ # --- FOOTER ---
79+ st .markdown ("---" )
80+ st .caption ("DocuFlow v1.0 | Built with Python, Celery, & Streamlit" )
0 commit comments