Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -61,7 +61,6 @@ def load_csv_data(folder_path):
|
|
| 61 |
pass
|
| 62 |
return pd.concat(all_data, ignore_index=True) if all_data else None
|
| 63 |
|
| 64 |
-
|
| 65 |
def dashboard_overview():
|
| 66 |
st.title("π Vehicle Registration Summary")
|
| 67 |
|
|
@@ -99,6 +98,14 @@ def dashboard_overview():
|
|
| 99 |
df_total_filtered = df_total[(df_total["year"] >= selected_years_total[0]) & (df_total["year"] <= selected_years_total[1])]
|
| 100 |
fig1 = px.line(df_total_filtered, x="year", y="count", color="State", title=f"Year-wise Registration by State ({selected_years_total[0]} - {selected_years_total[1]})")
|
| 101 |
st.plotly_chart(fig1, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
else:
|
| 103 |
st.warning("No data available for year-wise registration.")
|
| 104 |
|
|
@@ -114,6 +121,14 @@ def dashboard_overview():
|
|
| 114 |
fig2 = px.area(df_fuel_grouped, x="year", y="count", color="row_value", title="National Fuel Type Trend Over Years")
|
| 115 |
st.plotly_chart(fig2, use_container_width=True)
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
# 3. Vehicle class trend
|
| 118 |
vehicle_classes = sorted(df_class['row_value'].dropna().unique())
|
| 119 |
class_options = ["Select All"] + vehicle_classes
|
|
@@ -126,6 +141,14 @@ def dashboard_overview():
|
|
| 126 |
fig3 = px.area(df_class_grouped, x="year", y="count", color="row_value", title="Vehicle Class Trend Across India")
|
| 127 |
st.plotly_chart(fig3, use_container_width=True)
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
# 4. EV adoption graph
|
| 130 |
ev_states = sorted(df_ev['State'].dropna().unique())
|
| 131 |
ev_options = ["Select All"] + ev_states
|
|
@@ -138,12 +161,28 @@ def dashboard_overview():
|
|
| 138 |
fig5 = px.bar(df_ev_statewise, x="State", y="count", title="Top 10 States by EV Registration")
|
| 139 |
st.plotly_chart(fig5, use_container_width=True)
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
# 5. EV vs. Total Vehicle Registrations Over Time
|
| 142 |
df_ev_vs_total = df_ev_filtered.groupby("year")["count"].sum().reset_index()
|
| 143 |
df_total_vs_ev = pd.merge(df_total.groupby("year")['count'].sum().reset_index(), df_ev_vs_total, on="year", suffixes=('_total', '_ev'))
|
| 144 |
fig6 = px.line(df_total_vs_ev, x="year", y=["count_total", "count_ev"], title="EV vs Total Vehicle Registrations Over Time")
|
| 145 |
st.plotly_chart(fig6, use_container_width=True)
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
def ev_insights():
|
| 149 |
st.title("β‘ Electric Vehicle (EV) Analysis")
|
|
@@ -176,6 +215,15 @@ def ev_insights():
|
|
| 176 |
)
|
| 177 |
st.plotly_chart(fig1, use_container_width=True)
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
# 2οΈβ£ EV Growth Over Years (National) with Year Range Filter
|
| 180 |
st.subheader("π National EV Growth Over Years")
|
| 181 |
ev_growth = df_ev_state.groupby("year")["count"].sum().reset_index()
|
|
@@ -190,6 +238,14 @@ def ev_insights():
|
|
| 190 |
fig2 = px.line(ev_growth, x="year", y="count", title="Total EVs Registered per Year in India")
|
| 191 |
st.plotly_chart(fig2, use_container_width=True)
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
def fuel_norm_distribution_dashboard(root_dir, state_mapping_file, start_year=2009, end_year=2025):
|
| 195 |
|
|
@@ -246,38 +302,63 @@ def fuel_norm_distribution_dashboard(root_dir, state_mapping_file, start_year=20
|
|
| 246 |
if not statewise_df.empty:
|
| 247 |
melted_df = pd.melt(statewise_df, id_vars=["Fuel", "Year"], var_name="State", value_name="Fuel_Amount")
|
| 248 |
melted_df["State"] = melted_df["State"].apply(lambda x: state_dict.get(x.upper(), x))
|
| 249 |
-
|
| 250 |
selected_states = st.multiselect("Select States", ["Select All"] + sorted(melted_df["State"].unique()), default=[], key="state_selection")
|
| 251 |
if "Select All" in selected_states:
|
| 252 |
selected_states = list(melted_df["State"].unique())
|
| 253 |
-
|
| 254 |
year_range = st.slider("Select Year Range", min_value=start_year, max_value=end_year, value=(start_year, end_year), key="fuel_state_year")
|
| 255 |
filtered_df = melted_df[(melted_df["State"].isin(selected_states)) &
|
| 256 |
(melted_df["Year"].between(year_range[0], year_range[1]))]
|
| 257 |
-
|
| 258 |
fig1 = px.bar(filtered_df, x="State", y="Fuel_Amount", color="Fuel",
|
| 259 |
title=f"Fuel Distribution from {year_range[0]} to {year_range[1]}",
|
| 260 |
labels={"Fuel_Amount": "Amount (in units)"}, barmode="group")
|
| 261 |
st.plotly_chart(fig1, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
with tab2:
|
| 264 |
st.subheader("Emission Norm Distribution by State")
|
| 265 |
if not norms_df.empty:
|
| 266 |
melted_norms = pd.melt(norms_df, id_vars=["Norms", "Year"], var_name="State", value_name="Count")
|
| 267 |
melted_norms["State"] = melted_norms["State"].apply(lambda x: state_dict.get(x.upper(), x))
|
| 268 |
-
|
| 269 |
selected_states = st.multiselect("Select States", ["Select All"] + sorted(melted_norms["State"].unique()), default=[], key="norm_state_selection")
|
| 270 |
if "Select All" in selected_states:
|
| 271 |
selected_states = list(melted_norms["State"].unique())
|
| 272 |
-
|
| 273 |
year_range = st.slider("Select Year Range", min_value=start_year, max_value=end_year, value=(start_year, end_year), key="norm_state_year")
|
| 274 |
filtered_norms = melted_norms[(melted_norms["State"].isin(selected_states)) &
|
| 275 |
(melted_norms["Year"].between(year_range[0], year_range[1]))]
|
| 276 |
-
|
| 277 |
fig2 = px.bar(filtered_norms, x="State", y="Count", color="Norms",
|
| 278 |
title=f"Emission Norms Distribution from {year_range[0]} to {year_range[1]}",
|
| 279 |
labels={"Count": "Count of Norms"}, barmode="group")
|
| 280 |
st.plotly_chart(fig2, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
with tab3:
|
| 283 |
st.subheader("Norm Emissions by Fuel Type and Year")
|
|
|
|
| 61 |
pass
|
| 62 |
return pd.concat(all_data, ignore_index=True) if all_data else None
|
| 63 |
|
|
|
|
| 64 |
def dashboard_overview():
|
| 65 |
st.title("π Vehicle Registration Summary")
|
| 66 |
|
|
|
|
| 98 |
df_total_filtered = df_total[(df_total["year"] >= selected_years_total[0]) & (df_total["year"] <= selected_years_total[1])]
|
| 99 |
fig1 = px.line(df_total_filtered, x="year", y="count", color="State", title=f"Year-wise Registration by State ({selected_years_total[0]} - {selected_years_total[1]})")
|
| 100 |
st.plotly_chart(fig1, use_container_width=True)
|
| 101 |
+
|
| 102 |
+
# Download buttons
|
| 103 |
+
csv = df_total_filtered.to_csv(index=False).encode('utf-8')
|
| 104 |
+
st.download_button("π₯ Download Registration Data (CSV)", csv, "registration_data.csv", "text/csv")
|
| 105 |
+
excel_buffer = io.BytesIO()
|
| 106 |
+
with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
|
| 107 |
+
df_total_filtered.to_excel(writer, index=False)
|
| 108 |
+
st.download_button("π₯ Download Registration Data (Excel)", excel_buffer.getvalue(), "registration_data.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
| 109 |
else:
|
| 110 |
st.warning("No data available for year-wise registration.")
|
| 111 |
|
|
|
|
| 121 |
fig2 = px.area(df_fuel_grouped, x="year", y="count", color="row_value", title="National Fuel Type Trend Over Years")
|
| 122 |
st.plotly_chart(fig2, use_container_width=True)
|
| 123 |
|
| 124 |
+
# Download buttons
|
| 125 |
+
csv = df_fuel_grouped.to_csv(index=False).encode('utf-8')
|
| 126 |
+
st.download_button("π₯ Download Fuel Trend (CSV)", csv, "fuel_trend.csv", "text/csv")
|
| 127 |
+
excel_buffer = io.BytesIO()
|
| 128 |
+
with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
|
| 129 |
+
df_fuel_grouped.to_excel(writer, index=False)
|
| 130 |
+
st.download_button("π₯ Download Fuel Trend (Excel)", excel_buffer.getvalue(), "fuel_trend.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
| 131 |
+
|
| 132 |
# 3. Vehicle class trend
|
| 133 |
vehicle_classes = sorted(df_class['row_value'].dropna().unique())
|
| 134 |
class_options = ["Select All"] + vehicle_classes
|
|
|
|
| 141 |
fig3 = px.area(df_class_grouped, x="year", y="count", color="row_value", title="Vehicle Class Trend Across India")
|
| 142 |
st.plotly_chart(fig3, use_container_width=True)
|
| 143 |
|
| 144 |
+
# Download buttons
|
| 145 |
+
csv = df_class_grouped.to_csv(index=False).encode('utf-8')
|
| 146 |
+
st.download_button("π₯ Download Class Trend (CSV)", csv, "vehicle_class_trend.csv", "text/csv")
|
| 147 |
+
excel_buffer = io.BytesIO()
|
| 148 |
+
with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
|
| 149 |
+
df_class_grouped.to_excel(writer, index=False)
|
| 150 |
+
st.download_button("π₯ Download Class Trend (Excel)", excel_buffer.getvalue(), "vehicle_class_trend.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
| 151 |
+
|
| 152 |
# 4. EV adoption graph
|
| 153 |
ev_states = sorted(df_ev['State'].dropna().unique())
|
| 154 |
ev_options = ["Select All"] + ev_states
|
|
|
|
| 161 |
fig5 = px.bar(df_ev_statewise, x="State", y="count", title="Top 10 States by EV Registration")
|
| 162 |
st.plotly_chart(fig5, use_container_width=True)
|
| 163 |
|
| 164 |
+
# Download buttons
|
| 165 |
+
csv = df_ev_statewise.to_csv(index=False).encode('utf-8')
|
| 166 |
+
st.download_button("π₯ Download Top 10 EV States (CSV)", csv, "top_ev_states.csv", "text/csv")
|
| 167 |
+
excel_buffer = io.BytesIO()
|
| 168 |
+
with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
|
| 169 |
+
df_ev_statewise.to_excel(writer, index=False)
|
| 170 |
+
st.download_button("π₯ Download Top 10 EV States (Excel)", excel_buffer.getvalue(), "top_ev_states.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
| 171 |
+
|
| 172 |
# 5. EV vs. Total Vehicle Registrations Over Time
|
| 173 |
df_ev_vs_total = df_ev_filtered.groupby("year")["count"].sum().reset_index()
|
| 174 |
df_total_vs_ev = pd.merge(df_total.groupby("year")['count'].sum().reset_index(), df_ev_vs_total, on="year", suffixes=('_total', '_ev'))
|
| 175 |
fig6 = px.line(df_total_vs_ev, x="year", y=["count_total", "count_ev"], title="EV vs Total Vehicle Registrations Over Time")
|
| 176 |
st.plotly_chart(fig6, use_container_width=True)
|
| 177 |
|
| 178 |
+
# Download buttons
|
| 179 |
+
csv = df_total_vs_ev.to_csv(index=False).encode('utf-8')
|
| 180 |
+
st.download_button("π₯ Download EV vs Total Over Time (CSV)", csv, "ev_vs_total.csv", "text/csv")
|
| 181 |
+
excel_buffer = io.BytesIO()
|
| 182 |
+
with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
|
| 183 |
+
df_total_vs_ev.to_excel(writer, index=False)
|
| 184 |
+
st.download_button("π₯ Download EV vs Total Over Time (Excel)", excel_buffer.getvalue(), "ev_vs_total.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
| 185 |
+
|
| 186 |
|
| 187 |
def ev_insights():
|
| 188 |
st.title("β‘ Electric Vehicle (EV) Analysis")
|
|
|
|
| 215 |
)
|
| 216 |
st.plotly_chart(fig1, use_container_width=True)
|
| 217 |
|
| 218 |
+
# π₯ Download button for state-wise EV fuel type data
|
| 219 |
+
csv1 = filtered.to_csv(index=False).encode("utf-8")
|
| 220 |
+
st.download_button(
|
| 221 |
+
label=f"Download State-wise EV Data for {state_selection}",
|
| 222 |
+
data=csv1,
|
| 223 |
+
file_name=f"{state_selection.replace(' ', '_').lower()}_ev_fuel_data.csv",
|
| 224 |
+
mime="text/csv"
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
# 2οΈβ£ EV Growth Over Years (National) with Year Range Filter
|
| 228 |
st.subheader("π National EV Growth Over Years")
|
| 229 |
ev_growth = df_ev_state.groupby("year")["count"].sum().reset_index()
|
|
|
|
| 238 |
fig2 = px.line(ev_growth, x="year", y="count", title="Total EVs Registered per Year in India")
|
| 239 |
st.plotly_chart(fig2, use_container_width=True)
|
| 240 |
|
| 241 |
+
# π₯ Download button for national EV growth data
|
| 242 |
+
csv2 = ev_growth.to_csv(index=False).encode("utf-8")
|
| 243 |
+
st.download_button(
|
| 244 |
+
label="Download National EV Growth Data",
|
| 245 |
+
data=csv2,
|
| 246 |
+
file_name="national_ev_growth.csv",
|
| 247 |
+
mime="text/csv"
|
| 248 |
+
)
|
| 249 |
|
| 250 |
def fuel_norm_distribution_dashboard(root_dir, state_mapping_file, start_year=2009, end_year=2025):
|
| 251 |
|
|
|
|
| 302 |
if not statewise_df.empty:
|
| 303 |
melted_df = pd.melt(statewise_df, id_vars=["Fuel", "Year"], var_name="State", value_name="Fuel_Amount")
|
| 304 |
melted_df["State"] = melted_df["State"].apply(lambda x: state_dict.get(x.upper(), x))
|
| 305 |
+
|
| 306 |
selected_states = st.multiselect("Select States", ["Select All"] + sorted(melted_df["State"].unique()), default=[], key="state_selection")
|
| 307 |
if "Select All" in selected_states:
|
| 308 |
selected_states = list(melted_df["State"].unique())
|
| 309 |
+
|
| 310 |
year_range = st.slider("Select Year Range", min_value=start_year, max_value=end_year, value=(start_year, end_year), key="fuel_state_year")
|
| 311 |
filtered_df = melted_df[(melted_df["State"].isin(selected_states)) &
|
| 312 |
(melted_df["Year"].between(year_range[0], year_range[1]))]
|
| 313 |
+
|
| 314 |
fig1 = px.bar(filtered_df, x="State", y="Fuel_Amount", color="Fuel",
|
| 315 |
title=f"Fuel Distribution from {year_range[0]} to {year_range[1]}",
|
| 316 |
labels={"Fuel_Amount": "Amount (in units)"}, barmode="group")
|
| 317 |
st.plotly_chart(fig1, use_container_width=True)
|
| 318 |
+
|
| 319 |
+
# Download button
|
| 320 |
+
st.markdown("### π₯ Download Filtered Data")
|
| 321 |
+
download_format = st.radio("Select format", ["CSV", "Excel"], horizontal=True)
|
| 322 |
+
if download_format == "CSV":
|
| 323 |
+
csv_data = filtered_df.to_csv(index=False).encode("utf-8")
|
| 324 |
+
st.download_button("Download CSV", csv_data, file_name="fuel_distribution_filtered.csv", mime="text/csv")
|
| 325 |
+
else:
|
| 326 |
+
output = BytesIO()
|
| 327 |
+
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
|
| 328 |
+
filtered_df.to_excel(writer, index=False, sheet_name="Filtered Data")
|
| 329 |
+
st.download_button("Download Excel", output.getvalue(), file_name="fuel_distribution_filtered.xlsx", mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
| 330 |
+
|
| 331 |
|
| 332 |
with tab2:
|
| 333 |
st.subheader("Emission Norm Distribution by State")
|
| 334 |
if not norms_df.empty:
|
| 335 |
melted_norms = pd.melt(norms_df, id_vars=["Norms", "Year"], var_name="State", value_name="Count")
|
| 336 |
melted_norms["State"] = melted_norms["State"].apply(lambda x: state_dict.get(x.upper(), x))
|
| 337 |
+
|
| 338 |
selected_states = st.multiselect("Select States", ["Select All"] + sorted(melted_norms["State"].unique()), default=[], key="norm_state_selection")
|
| 339 |
if "Select All" in selected_states:
|
| 340 |
selected_states = list(melted_norms["State"].unique())
|
| 341 |
+
|
| 342 |
year_range = st.slider("Select Year Range", min_value=start_year, max_value=end_year, value=(start_year, end_year), key="norm_state_year")
|
| 343 |
filtered_norms = melted_norms[(melted_norms["State"].isin(selected_states)) &
|
| 344 |
(melted_norms["Year"].between(year_range[0], year_range[1]))]
|
| 345 |
+
|
| 346 |
fig2 = px.bar(filtered_norms, x="State", y="Count", color="Norms",
|
| 347 |
title=f"Emission Norms Distribution from {year_range[0]} to {year_range[1]}",
|
| 348 |
labels={"Count": "Count of Norms"}, barmode="group")
|
| 349 |
st.plotly_chart(fig2, use_container_width=True)
|
| 350 |
+
|
| 351 |
+
# Download button
|
| 352 |
+
st.markdown("### π₯ Download Filtered Norms Data")
|
| 353 |
+
download_format_norms = st.radio("Select format", ["CSV", "Excel"], horizontal=True, key="norm_download_format")
|
| 354 |
+
if download_format_norms == "CSV":
|
| 355 |
+
csv_norm_data = filtered_norms.to_csv(index=False).encode("utf-8")
|
| 356 |
+
st.download_button("Download CSV", csv_norm_data, file_name="norm_distribution_filtered.csv", mime="text/csv")
|
| 357 |
+
else:
|
| 358 |
+
norm_output = BytesIO()
|
| 359 |
+
with pd.ExcelWriter(norm_output, engine='xlsxwriter') as writer:
|
| 360 |
+
filtered_norms.to_excel(writer, index=False, sheet_name="Filtered Norms")
|
| 361 |
+
st.download_button("Download Excel", norm_output.getvalue(), file_name="norm_distribution_filtered.xlsx", mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
| 362 |
|
| 363 |
with tab3:
|
| 364 |
st.subheader("Norm Emissions by Fuel Type and Year")
|