# Vizro is an open-source toolkit for creating modular data visualization applications.
# check out https://github.com/mckinsey/vizro for more info about Vizro
# and checkout https://vizro.readthedocs.io/en/stable/ for documentation.
"""Dev app to try things out."""
import random
from vizro import Vizro
import vizro.models as vm
import vizro.actions as va
import pandas as pd
from custom_charts import custom_line, create_kpi_card, create_monthly_sales_by_segment, bar_chart_by_category, orders_ag_grid
def make_superstore_df():
"""Load and preprocess the Superstore dataset.
Returns:
pd.DataFrame: Processed dataframe with state codes, filtered to latest 2 years,
and enriched with Year, Month, and Order Status columns.
"""
df = pd.read_csv("superstore.csv", encoding="latin1", parse_dates=["Order Date", "Ship Date"])
# Filter dataframe for only the latest 2 years and add a month column.
df["Year"] = df["Order Date"].dt.year
df["Month"] = df["Order Date"].dt.month
df["Month Name"] = df["Order Date"].dt.strftime("%b")
df = df[df["Year"].isin([2015, 2016])]
# Create order status - randomly assign with weights: 60% Delivered, 10% In Transit, 30% Processing
df["Order Status"] = random.choices(["Delivered", "In Transit", "Processing"], weights=[0.6, 0.1, 0.3], k=len(df))
return df
superstore_df = make_superstore_df()
page_one = vm.Page(
title="Superstore Sales Dashboard",
components=[
vm.Container(
title="",
components=[
create_kpi_card(superstore_df, column="Sales", icon="Bar Chart", prefix="$"),
vm.Graph(figure=custom_line(superstore_df)),
],
variant="filled",
layout=vm.Grid(grid=[[0], [0], [1], [1], [1]]),
),
vm.Container(
title="",
components=[
create_kpi_card(superstore_df, column="Profit", icon="Money Bag", prefix="$"),
vm.Graph(figure=custom_line(superstore_df, value_col="Profit")),
],
variant="filled",
layout=vm.Grid(grid=[[0], [0], [1], [1], [1]]),
),
vm.Container(
title="",
components=[
create_kpi_card(superstore_df, column="Customer ID", icon="Group"),
vm.Graph(figure=custom_line(superstore_df, value_col="Customer ID")),
],
variant="filled",
layout=vm.Grid(grid=[[0], [0], [1], [1], [1]]),
),
vm.Container(
title="Monthly Sales by Segment",
components=[
vm.Graph(figure=create_monthly_sales_by_segment(superstore_df), header="Consumer"),
vm.Graph(
figure=create_monthly_sales_by_segment(superstore_df, segment="Corporate"), header="Corporate"
),
vm.Graph(
figure=create_monthly_sales_by_segment(superstore_df, segment="Home Office"), header="Home Office"
),
],
layout=vm.Grid(grid=[[0, 1, 2]], col_gap="12px"),
variant="filled",
),
vm.Container(
title="",
components=[
vm.Graph(
figure=bar_chart_by_category(superstore_df, custom_data=["Category"]),
actions=va.set_control(control="product_category_filter", value="Category"),
)
],
variant="filled",
),
],
layout=vm.Grid(grid=[[0, 1, 2], [3, 3, 4]]),
controls=[
vm.Filter(id="product_category_filter", column="Category", visible=False),
vm.Filter(column="Region"),
vm.Filter(column="Category"),
vm.Filter(column="Segment"),
],
)
page_two = vm.Page(
title="Orders",
layout=vm.Flex(),
components=[
vm.AgGrid(figure=orders_ag_grid(superstore_df)),
],
)
dashboard = vm.Dashboard(
pages=[page_one, page_two],
navigation=vm.Navigation(
pages=["Superstore Sales Dashboard", "Orders"],
nav_selector=vm.NavBar(
items=[
vm.NavLink(pages=["Superstore Sales Dashboard"], icon="Bar Chart", label="Overview"),
vm.NavLink(pages=["Orders"], icon="Shopping Cart", label="Orders"),
]
),
)
)
Vizro().build(dashboard).run()