Update app.py
Browse files
app.py
CHANGED
|
@@ -197,13 +197,34 @@ def on_table_select(choice: str):
|
|
| 197 |
# =========================================================
|
| 198 |
|
| 199 |
def load_kpis() -> Dict[str, Any]:
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
|
| 209 |
# =========================================================
|
|
@@ -465,10 +486,10 @@ def render_kpi_cards() -> str:
|
|
| 465 |
</div>"""
|
| 466 |
|
| 467 |
kpi_config = [
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
]
|
| 473 |
|
| 474 |
html = (
|
|
@@ -532,74 +553,100 @@ def _empty_chart(title: str) -> go.Figure:
|
|
| 532 |
|
| 533 |
|
| 534 |
def build_sales_chart() -> go.Figure:
|
| 535 |
-
path =
|
| 536 |
if not path.exists():
|
| 537 |
return _empty_chart("Sales Trends β run the pipeline first")
|
|
|
|
| 538 |
df = pd.read_csv(path)
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
|
|
|
| 544 |
fig = go.Figure()
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
)
|
| 552 |
-
|
| 553 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 554 |
fig.update_xaxes(gridcolor="rgba(124,92,191,0.15)", showgrid=True)
|
| 555 |
-
fig.update_yaxes(gridcolor="rgba(124,92,191,0.15)", showgrid=True)
|
| 556 |
return fig
|
| 557 |
|
| 558 |
|
| 559 |
def build_sentiment_chart() -> go.Figure:
|
| 560 |
-
path =
|
| 561 |
if not path.exists():
|
| 562 |
return _empty_chart("Sentiment Distribution β run the pipeline first")
|
|
|
|
| 563 |
df = pd.read_csv(path)
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 576 |
fig.update_layout(**_styled_layout(
|
| 577 |
-
height=
|
| 578 |
-
title=dict(text="Sentiment Distribution
|
|
|
|
| 579 |
))
|
| 580 |
-
fig.update_xaxes(title="
|
| 581 |
-
fig.update_yaxes(
|
| 582 |
return fig
|
| 583 |
|
| 584 |
|
| 585 |
def build_top_sellers_chart() -> go.Figure:
|
| 586 |
-
path =
|
| 587 |
if not path.exists():
|
| 588 |
-
return _empty_chart("Top
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 592 |
fig = go.Figure(go.Bar(
|
| 593 |
-
y=df[
|
| 594 |
-
|
| 595 |
-
|
|
|
|
|
|
|
| 596 |
))
|
|
|
|
| 597 |
fig.update_layout(**_styled_layout(
|
| 598 |
height=max(400, len(df) * 30),
|
| 599 |
-
title=dict(text="Top
|
|
|
|
| 600 |
))
|
| 601 |
fig.update_yaxes(autorange="reversed")
|
| 602 |
-
fig.update_xaxes(title="Total
|
| 603 |
return fig
|
| 604 |
|
| 605 |
|
|
|
|
| 197 |
# =========================================================
|
| 198 |
|
| 199 |
def load_kpis() -> Dict[str, Any]:
|
| 200 |
+
try:
|
| 201 |
+
summary_path = BASE_DIR / "menu_item_summary.csv"
|
| 202 |
+
sales_path = BASE_DIR / "monthly_sales.csv"
|
| 203 |
+
final_path = BASE_DIR / "final_menu_recommendations.csv"
|
| 204 |
+
|
| 205 |
+
if not summary_path.exists() or not sales_path.exists():
|
| 206 |
+
return {}
|
| 207 |
+
|
| 208 |
+
summary_df = pd.read_csv(summary_path)
|
| 209 |
+
sales_df = pd.read_csv(sales_path)
|
| 210 |
+
|
| 211 |
+
kpis = {
|
| 212 |
+
"n_menu_items": int(summary_df["menu_item"].nunique()) if "menu_item" in summary_df.columns else 0,
|
| 213 |
+
"n_months": int(sales_df["month"].nunique()) if "month" in sales_df.columns else 0,
|
| 214 |
+
"total_orders": float(sales_df["orders"].sum()) if "orders" in sales_df.columns else 0,
|
| 215 |
+
"total_revenue": float(sales_df["revenue"].sum()) if "revenue" in sales_df.columns else 0,
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
if final_path.exists():
|
| 219 |
+
final_df = pd.read_csv(final_path)
|
| 220 |
+
if "recommended_action" in final_df.columns:
|
| 221 |
+
counts = final_df["recommended_action"].value_counts().to_dict()
|
| 222 |
+
for k, v in counts.items():
|
| 223 |
+
kpis[f"action_{str(k).lower()}"] = int(v)
|
| 224 |
+
|
| 225 |
+
return kpis
|
| 226 |
+
except Exception:
|
| 227 |
+
return {}
|
| 228 |
|
| 229 |
|
| 230 |
# =========================================================
|
|
|
|
| 486 |
</div>"""
|
| 487 |
|
| 488 |
kpi_config = [
|
| 489 |
+
("n_menu_items", "π", "Menu Items", "#a48de8"),
|
| 490 |
+
("n_months", "π
", "Months", "#7aa6f8"),
|
| 491 |
+
("total_orders", "π§Ύ", "Total Orders", "#6ee7c7"),
|
| 492 |
+
("total_revenue", "π°", "Revenue", "#3dcba8"),
|
| 493 |
]
|
| 494 |
|
| 495 |
html = (
|
|
|
|
| 553 |
|
| 554 |
|
| 555 |
def build_sales_chart() -> go.Figure:
|
| 556 |
+
path = BASE_DIR / "monthly_sales.csv"
|
| 557 |
if not path.exists():
|
| 558 |
return _empty_chart("Sales Trends β run the pipeline first")
|
| 559 |
+
|
| 560 |
df = pd.read_csv(path)
|
| 561 |
+
if "month" not in df.columns or "revenue" not in df.columns:
|
| 562 |
+
return _empty_chart("monthly_sales.csv is missing required columns")
|
| 563 |
+
|
| 564 |
+
monthly = df.groupby("month", as_index=False)["revenue"].sum()
|
| 565 |
+
monthly["month"] = pd.to_datetime(monthly["month"], errors="coerce")
|
| 566 |
+
|
| 567 |
fig = go.Figure()
|
| 568 |
+
fig.add_trace(go.Scatter(
|
| 569 |
+
x=monthly["month"],
|
| 570 |
+
y=monthly["revenue"],
|
| 571 |
+
mode="lines+markers",
|
| 572 |
+
name="Revenue",
|
| 573 |
+
line=dict(color=CHART_PALETTE[0], width=3),
|
| 574 |
+
marker=dict(size=6),
|
| 575 |
+
hovertemplate="<b>Revenue</b><br>%{x|%b %Y}: %{y:,.2f}<extra></extra>",
|
| 576 |
+
))
|
| 577 |
+
|
| 578 |
+
fig.update_layout(**_styled_layout(
|
| 579 |
+
height=450,
|
| 580 |
+
hovermode="x unified",
|
| 581 |
+
title=dict(text="Monthly Revenue Trend"),
|
| 582 |
+
))
|
| 583 |
fig.update_xaxes(gridcolor="rgba(124,92,191,0.15)", showgrid=True)
|
| 584 |
+
fig.update_yaxes(title="Revenue", gridcolor="rgba(124,92,191,0.15)", showgrid=True)
|
| 585 |
return fig
|
| 586 |
|
| 587 |
|
| 588 |
def build_sentiment_chart() -> go.Figure:
|
| 589 |
+
path = BASE_DIR / "reviews.csv"
|
| 590 |
if not path.exists():
|
| 591 |
return _empty_chart("Sentiment Distribution β run the pipeline first")
|
| 592 |
+
|
| 593 |
df = pd.read_csv(path)
|
| 594 |
+
if "rating" not in df.columns:
|
| 595 |
+
return _empty_chart("reviews.csv is missing the rating column")
|
| 596 |
+
|
| 597 |
+
def label_sentiment(x):
|
| 598 |
+
if x >= 4:
|
| 599 |
+
return "Positive"
|
| 600 |
+
elif x == 3:
|
| 601 |
+
return "Neutral"
|
| 602 |
+
return "Negative"
|
| 603 |
+
|
| 604 |
+
df["sentiment"] = df["rating"].apply(label_sentiment)
|
| 605 |
+
counts = df["sentiment"].value_counts().reindex(["Negative", "Neutral", "Positive"], fill_value=0)
|
| 606 |
+
|
| 607 |
+
fig = go.Figure(go.Bar(
|
| 608 |
+
x=counts.index,
|
| 609 |
+
y=counts.values,
|
| 610 |
+
marker_color=["#e8537a", "#5e8fef", "#2ec4a0"],
|
| 611 |
+
hovertemplate="<b>%{x}</b><br>Count: %{y}<extra></extra>",
|
| 612 |
+
))
|
| 613 |
+
|
| 614 |
fig.update_layout(**_styled_layout(
|
| 615 |
+
height=420,
|
| 616 |
+
title=dict(text="Review Sentiment Distribution"),
|
| 617 |
+
showlegend=False,
|
| 618 |
))
|
| 619 |
+
fig.update_xaxes(title="Sentiment")
|
| 620 |
+
fig.update_yaxes(title="Number of Reviews")
|
| 621 |
return fig
|
| 622 |
|
| 623 |
|
| 624 |
def build_top_sellers_chart() -> go.Figure:
|
| 625 |
+
path = BASE_DIR / "final_menu_recommendations.csv"
|
| 626 |
if not path.exists():
|
| 627 |
+
return _empty_chart("Top Menu Items β run the pipeline first")
|
| 628 |
+
|
| 629 |
+
df = pd.read_csv(path)
|
| 630 |
+
if "menu_item" not in df.columns or "total_orders" not in df.columns:
|
| 631 |
+
return _empty_chart("final_menu_recommendations.csv is missing required columns")
|
| 632 |
+
|
| 633 |
+
df = df.sort_values("total_orders", ascending=False).head(10)
|
| 634 |
+
|
| 635 |
fig = go.Figure(go.Bar(
|
| 636 |
+
y=df["menu_item"],
|
| 637 |
+
x=df["total_orders"],
|
| 638 |
+
orientation="h",
|
| 639 |
+
marker=dict(color=df["total_orders"], colorscale=[[0, "#c5b4f0"], [1, "#7c5cbf"]]),
|
| 640 |
+
hovertemplate="<b>%{y}</b><br>Total Orders: %{x}<extra></extra>",
|
| 641 |
))
|
| 642 |
+
|
| 643 |
fig.update_layout(**_styled_layout(
|
| 644 |
height=max(400, len(df) * 30),
|
| 645 |
+
title=dict(text="Top Menu Items by Orders"),
|
| 646 |
+
showlegend=False,
|
| 647 |
))
|
| 648 |
fig.update_yaxes(autorange="reversed")
|
| 649 |
+
fig.update_xaxes(title="Total Orders")
|
| 650 |
return fig
|
| 651 |
|
| 652 |
|