-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdashboard_app.py
More file actions
137 lines (120 loc) Β· 4.91 KB
/
dashboard_app.py
File metadata and controls
137 lines (120 loc) Β· 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Dashboard application for Car Price Analytics using Streamlit.
Includes page navigation and sidebar filters.
"""
import streamlit as st
import pandas as pd
# Configure the Streamlit page
st.set_page_config(
page_title="Car Price Analytics Dashboard",
page_icon="π",
layout="wide",
initial_sidebar_state="expanded",
)
# Load data and store in session state
@st.cache_data
def load_data():
return pd.read_csv('data/final/car_prices.csv')
if "df" not in st.session_state:
st.session_state['df'] = load_data()
df = st.session_state['df']
# Define pages for navigation
overview = st.Page("pages/overview.py",
title="Overview",
icon="π ")
hypothesis1 = st.Page("pages/hypothesis1.py",
title="Hypothesis 1",
icon="β")
hypothesis2 = st.Page("pages/hypothesis2.py",
title="Hypothesis 2",
icon="β")
hypothesis3 = st.Page("pages/hypothesis3.py",
title="Hypothesis 3",
icon="π‘")
hypothesis4 = st.Page("pages/hypothesis4.py",
title="Hypothesis 4",
icon="π‘")
hypothesis5 = st.Page("pages/hypothesis5.py",
title="Hypothesis 5",
icon="β")
insights_model = st.Page("pages/insights_model.py",
title="Model Insights",
icon="π€")
nav = st.navigation([overview, hypothesis1, hypothesis2, hypothesis3, hypothesis4, hypothesis5, insights_model])
current_page = nav.title
# ---------------- Sidebar (filters) ----------------
with st.sidebar:
st.header("Filters")
# price filter
price_range = st.slider("Price Range",
min_value=float(df['price'].min()),
max_value=float(df['price'].max()),
value=(float(df['price'].min()),
float(df['price'].max())))
# engine size
enginesizes = st.slider("Engine Size",
min_value=float(df['enginesize'].min()),
max_value=float(df['enginesize'].max()),
value=(float(df['enginesize'].min()),
float(df['enginesize'].max())))
# horsepower
horsepower = st.slider("Horsepower",
min_value=float(df['horsepower'].min()),
max_value=float(df['horsepower'].max()),
value=(float(df['horsepower'].min()),
float(df['horsepower'].max())))
# fuel type
# Only show fuel filter if not on Hypothesis 1
if current_page != "Hypothesis 1":
fuel_types = st.sidebar.multiselect(
"Fuel Type(s):",
options=df["fueltype"].unique().tolist(),
default=df["fueltype"].unique().tolist()
)
else:
fuel_types = df["fueltype"].unique().tolist() # include all by default
# car body type
carbody_types = st.multiselect("Car Body Type",
options=df['carbody'].unique().tolist(),
default=df['carbody'].unique().tolist())
# drive wheel type
drivewheel_types = st.multiselect("Drive Wheel Type",
options=df['drivewheel'].unique()
.tolist(),
default=df['drivewheel'].unique()
.tolist())
# apply filters
def apply_filters(original_df: pd.DataFrame) -> pd.DataFrame:
"""
Apply filters to the original dataframe
Args:
original_df (pd.DataFrame): The input DataFrame before filtering
Returns:
pd.DataFrame: The filtered DataFrame
"""
filtered_df = original_df[original_df['price'].between(*price_range)]
if fuel_types:
filtered_df = filtered_df[filtered_df['fueltype'].isin(fuel_types)]
if carbody_types:
filtered_df = filtered_df[filtered_df['carbody'].isin(carbody_types)]
if drivewheel_types:
filtered_df = filtered_df[filtered_df['drivewheel']
.isin(drivewheel_types)]
filtered_df = filtered_df[filtered_df['enginesize'].between(*enginesizes)]
filtered_df = filtered_df[filtered_df['horsepower'].between(*horsepower)]
return filtered_df
st.session_state['filtered_df'] = apply_filters(df)
# display total cars after filtering
st.sidebar.info("**Cars after filtering: "
f"{st.session_state['filtered_df'].shape[0]}**")
# ---------------- Save global filters to session_state ----------------
st.session_state['global_filters'] = {
'price_range': price_range,
'enginesizes': enginesizes,
'horsepower': horsepower,
'fuel_types': fuel_types,
'carbody_types': carbody_types,
'drivewheel_types': drivewheel_types
}
# Run the navigation
nav.run()