-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifesto_streamlit.py
90 lines (75 loc) · 3.43 KB
/
manifesto_streamlit.py
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
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
# Load dataframes
url_bjp = 'https://raw.githubusercontent.com/jyoti-sn/India_Election_Manifesto/main/FinalOutput_BJP.csv'
url_inc = 'https://raw.githubusercontent.com/jyoti-sn/India_Election_Manifesto/main/FinalOutput_INC.csv'
bjp_df = pd.read_csv(url_bjp)
inc_df = pd.read_csv(url_inc)
# App title and description
st.title("Election Manifesto Dashboard")
st.subheader("Exploring the Evolution of Key Issues in Indian Election Manifestos")
# Sidebar for year and party selection
years = st.sidebar.slider("Select years", min_value=2004, max_value=2024, value=(2004, 2024), step=5)
compare_parties = st.sidebar.checkbox("Compare Political Parties")
if compare_parties:
parties = st.sidebar.multiselect("Select parties to compare", ["BJP", "INC"], default=["BJP", "INC"])
else:
party = st.sidebar.selectbox("Select a party", ["BJP", "INC"])
# Function to generate radar chart with party-specific colors
def generate_radar_chart(party_name, df):
domains = [x.strip() for domain in df['Domains'].tolist() for x in domain.split(',')]
domain_counts = pd.Series(domains).value_counts()
color = 'orange' if party_name == 'BJP' else 'blue'
fig = go.Figure(go.Scatterpolar(
r=domain_counts.nlargest(10),
theta=domain_counts.nlargest(10).index,
fill='toself',
name=party_name,
line_color=color
))
fig.update_layout(
polar=dict(
radialaxis=dict(
visible=True,
range=[0, max(domain_counts)],
tickfont=dict(size=10)
)
),
showlegend=True,
margin=dict(t=30, b=30, l=30, r=30)
)
return fig
# Function to generate bar chart with party-specific colors
def generate_subcategory_barchart(party_name, df):
subcategories = [x.strip().replace("'", "").replace('"', "") for subcategory in df['Topic_Subcategories'].tolist() for x in subcategory.split(',')]
subcategory_counts = pd.Series(subcategories).value_counts()
color = 'darkorange' if party_name == 'BJP' else 'royalblue'
st.subheader(f"{party_name} Most Common Issues")
# Cleaning labels and rotating for better visibility
subcategory_counts.index = [label.replace("'", "").replace('"', "") for label in subcategory_counts.index]
st.bar_chart(subcategory_counts.nlargest(10), use_container_width=True)
# Logic for comparing parties
if compare_parties:
st.subheader("Most Common Domains")
col1, col2 = st.columns(2)
with col1:
bjp_filtered = bjp_df[bjp_df['Year'].between(years[0], years[1])]
st.plotly_chart(generate_radar_chart("BJP", bjp_filtered), use_container_width=True)
with col2:
inc_filtered = inc_df[inc_df['Year'].between(years[0], years[1])]
st.plotly_chart(generate_radar_chart("INC", inc_filtered), use_container_width=True)
st.subheader("Most Common Issues")
col3, col4 = st.columns(2)
with col3:
generate_subcategory_barchart("BJP", bjp_filtered)
with col4:
generate_subcategory_barchart("INC", inc_filtered)
# Logic for single party selection
else:
df = bjp_df if party == 'BJP' else inc_df
filtered_df = df[df['Year'].between(years[0], years[1])]
st.subheader("Most Common Domains")
st.plotly_chart(generate_radar_chart(party, filtered_df), use_container_width=True)
st.subheader("Most Common Issues")
generate_subcategory_barchart(party, filtered_df)