Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
|
| 7 |
+
# Set Streamlit page config
|
| 8 |
+
st.set_page_config(page_title="Legislative Visualizations", layout="wide")
|
| 9 |
+
|
| 10 |
+
st.title("Legislative Bill Analysis Dashboard")
|
| 11 |
+
|
| 12 |
+
# Upload dataset
|
| 13 |
+
uploaded_file = st.file_uploader("Illinois_Entire_Data_Insights_Final_v2.csv", type=["csv", "xlsx"])
|
| 14 |
+
|
| 15 |
+
if uploaded_file:
|
| 16 |
+
# Load dataset
|
| 17 |
+
if uploaded_file.name.endswith('.csv'):
|
| 18 |
+
df = pd.read_csv(uploaded_file)
|
| 19 |
+
else:
|
| 20 |
+
df = pd.read_excel(uploaded_file)
|
| 21 |
+
|
| 22 |
+
st.success("File uploaded and read successfully!")
|
| 23 |
+
|
| 24 |
+
# Sankey Diagram
|
| 25 |
+
st.header("🔗 Sankey Diagram: Intent → Stance → Beneficiaries")
|
| 26 |
+
sankey_df = df[['intent_standardized', 'stance_standardized', 'intended_beneficiaries_standardized']].dropna()
|
| 27 |
+
|
| 28 |
+
if not sankey_df.empty:
|
| 29 |
+
labels = list(pd.unique(sankey_df['intent_standardized'].tolist() +
|
| 30 |
+
sankey_df['stance_standardized'].tolist() +
|
| 31 |
+
sankey_df['intended_beneficiaries_standardized'].tolist()))
|
| 32 |
+
label_map = {label: i for i, label in enumerate(labels)}
|
| 33 |
+
|
| 34 |
+
intent_stance = sankey_df.groupby(['intent_standardized', 'stance_standardized']).size().reset_index(name='count')
|
| 35 |
+
stance_beneficiary = sankey_df.groupby(['stance_standardized', 'intended_beneficiaries_standardized']).size().reset_index(name='count')
|
| 36 |
+
|
| 37 |
+
source = intent_stance['intent_standardized'].map(label_map).tolist() + stance_beneficiary['stance_standardized'].map(label_map).tolist()
|
| 38 |
+
target = intent_stance['stance_standardized'].map(label_map).tolist() + stance_beneficiary['intended_beneficiaries_standardized'].map(label_map).tolist()
|
| 39 |
+
value = intent_stance['count'].tolist() + stance_beneficiary['count'].tolist()
|
| 40 |
+
|
| 41 |
+
fig_sankey = go.Figure(data=[go.Sankey(
|
| 42 |
+
node=dict(pad=15, thickness=20, line=dict(color="black", width=0.5), label=labels),
|
| 43 |
+
link=dict(source=source, target=target, value=value)
|
| 44 |
+
)])
|
| 45 |
+
fig_sankey.update_layout(title_text="Sankey: Intent → Stance → Beneficiary", font_size=12)
|
| 46 |
+
|
| 47 |
+
st.plotly_chart(fig_sankey, use_container_width=True)
|
| 48 |
+
else:
|
| 49 |
+
st.warning("Sankey input columns contain only null values or are missing.")
|
| 50 |
+
|
| 51 |
+
# Heatmap
|
| 52 |
+
st.header("🧯 Heatmap: Category vs Policy Impact Area")
|
| 53 |
+
heat_df = df[['category_&_subcategory_standardized', 'policy_impact_areas_standardized']].dropna()
|
| 54 |
+
|
| 55 |
+
if not heat_df.empty:
|
| 56 |
+
heat = heat_df.pivot_table(index='category_&_subcategory_standardized',
|
| 57 |
+
columns='policy_impact_areas_standardized',
|
| 58 |
+
aggfunc=len,
|
| 59 |
+
fill_value=0)
|
| 60 |
+
|
| 61 |
+
plt.figure(figsize=(14, 8))
|
| 62 |
+
sns.heatmap(heat, cmap='coolwarm', annot=False)
|
| 63 |
+
plt.title("Heatmap: Category vs Policy Impact Area")
|
| 64 |
+
plt.xlabel("Policy Impact Area")
|
| 65 |
+
plt.ylabel("Category")
|
| 66 |
+
plt.tight_layout()
|
| 67 |
+
|
| 68 |
+
st.pyplot(plt)
|
| 69 |
+
else:
|
| 70 |
+
st.warning("Heatmap input columns contain only null values or are missing.")
|
| 71 |
+
else:
|
| 72 |
+
st.info("Please upload a dataset file to view the visualizations.")
|