Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,75 +3,66 @@ import pandas as pd
|
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import seaborn as sns
|
|
|
|
| 6 |
|
| 7 |
st.set_page_config(page_title="Legislative Visualizations", layout="wide")
|
| 8 |
|
| 9 |
st.title("Legislative Bill Analysis Dashboard")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
use_sample = st.sidebar.checkbox("Use sample dataset")
|
| 13 |
|
| 14 |
-
if
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
'intent_standardized': ['Fund', 'Fund', 'Regulate', 'Promote', 'Regulate'],
|
| 18 |
-
'stance_standardized': ['Support', 'Neutral', 'Oppose', 'Support', 'Oppose'],
|
| 19 |
-
'intended_beneficiaries_standardized': ['Students', 'Schools', 'Teachers', 'Parents', 'Students'],
|
| 20 |
-
'category_&_subcategory_standardized': ['Education > Higher Ed'] * 5,
|
| 21 |
-
'policy_impact_areas_standardized': ['Access', 'Infrastructure', 'Curriculum', 'Affordability', 'Access']
|
| 22 |
-
})
|
| 23 |
-
else:
|
| 24 |
-
uploaded_file = st.file_uploader("Upload legislative dataset (.csv)", type=["csv"])
|
| 25 |
-
if uploaded_file:
|
| 26 |
-
df = pd.read_csv(uploaded_file)
|
| 27 |
-
else:
|
| 28 |
-
st.info("Please upload a dataset file to view the visualizations.")
|
| 29 |
-
st.stop()
|
| 30 |
|
| 31 |
-
# Sankey Diagram
|
| 32 |
-
st.header("π Sankey Diagram: Intent β Stance β Beneficiaries")
|
| 33 |
-
sankey_df = df[['intent_standardized', 'stance_standardized', 'intended_beneficiaries_standardized']].dropna()
|
| 34 |
|
| 35 |
-
if not sankey_df.empty:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
else:
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
plt.xlabel("Policy Impact Area")
|
| 72 |
-
plt.ylabel("Category")
|
| 73 |
-
plt.tight_layout()
|
| 74 |
|
| 75 |
-
st.pyplot(plt)
|
| 76 |
else:
|
| 77 |
-
st.
|
|
|
|
|
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import seaborn as sns
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
st.set_page_config(page_title="Legislative Visualizations", layout="wide")
|
| 9 |
|
| 10 |
st.title("Legislative Bill Analysis Dashboard")
|
| 11 |
|
| 12 |
+
DATA_PATH = "Illinois_Entire_Data_Insights_Final_v2.csv"
|
|
|
|
| 13 |
|
| 14 |
+
if os.path.exists(DATA_PATH):
|
| 15 |
+
df = pd.read_csv(DATA_PATH)
|
| 16 |
+
st.success(f"Loaded dataset from {DATA_PATH}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Sankey Diagram
|
| 19 |
+
st.header("π Sankey Diagram: Intent β Stance β Beneficiaries")
|
| 20 |
+
sankey_df = df[['intent_standardized', 'stance_standardized', 'intended_beneficiaries_standardized']].dropna()
|
| 21 |
|
| 22 |
+
if not sankey_df.empty:
|
| 23 |
+
labels = list(pd.unique(sankey_df['intent_standardized'].tolist() +
|
| 24 |
+
sankey_df['stance_standardized'].tolist() +
|
| 25 |
+
sankey_df['intended_beneficiaries_standardized'].tolist()))
|
| 26 |
+
label_map = {label: i for i, label in enumerate(labels)}
|
| 27 |
|
| 28 |
+
intent_stance = sankey_df.groupby(['intent_standardized', 'stance_standardized']).size().reset_index(name='count')
|
| 29 |
+
stance_beneficiary = sankey_df.groupby(['stance_standardized', 'intended_beneficiaries_standardized']).size().reset_index(name='count')
|
| 30 |
|
| 31 |
+
source = intent_stance['intent_standardized'].map(label_map).tolist() + stance_beneficiary['stance_standardized'].map(label_map).tolist()
|
| 32 |
+
target = intent_stance['stance_standardized'].map(label_map).tolist() + stance_beneficiary['intended_beneficiaries_standardized'].map(label_map).tolist()
|
| 33 |
+
value = intent_stance['count'].tolist() + stance_beneficiary['count'].tolist()
|
| 34 |
|
| 35 |
+
fig_sankey = go.Figure(data=[go.Sankey(
|
| 36 |
+
node=dict(pad=15, thickness=20, line=dict(color="black", width=0.5), label=labels),
|
| 37 |
+
link=dict(source=source, target=target, value=value)
|
| 38 |
+
)])
|
| 39 |
+
fig_sankey.update_layout(title_text="Sankey: Intent β Stance β Beneficiary", font_size=12)
|
| 40 |
|
| 41 |
+
st.plotly_chart(fig_sankey, use_container_width=True)
|
| 42 |
+
else:
|
| 43 |
+
st.warning("Sankey input columns contain only null values or are missing.")
|
| 44 |
+
|
| 45 |
+
# Heatmap
|
| 46 |
+
st.header("π§― Heatmap: Category vs Policy Impact Area")
|
| 47 |
+
heat_df = df[['category_&_subcategory_standardized', 'policy_impact_areas_standardized']].dropna()
|
| 48 |
|
| 49 |
+
if not heat_df.empty:
|
| 50 |
+
heat = heat_df.pivot_table(index='category_&_subcategory_standardized',
|
| 51 |
+
columns='policy_impact_areas_standardized',
|
| 52 |
+
aggfunc=len,
|
| 53 |
+
fill_value=0)
|
| 54 |
|
| 55 |
+
plt.figure(figsize=(14, 8))
|
| 56 |
+
sns.heatmap(heat, cmap='coolwarm', annot=False)
|
| 57 |
+
plt.title("Heatmap: Category vs Policy Impact Area")
|
| 58 |
+
plt.xlabel("Policy Impact Area")
|
| 59 |
+
plt.ylabel("Category")
|
| 60 |
+
plt.tight_layout()
|
| 61 |
|
| 62 |
+
st.pyplot(plt)
|
| 63 |
+
else:
|
| 64 |
+
st.warning("Heatmap input columns contain only null values or are missing.")
|
|
|
|
|
|
|
|
|
|
| 65 |
|
|
|
|
| 66 |
else:
|
| 67 |
+
st.error(f"Data file {DATA_PATH} not found in Space repo.")
|
| 68 |
+
st.stop()
|