-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit-app.py
223 lines (186 loc) · 7.5 KB
/
streamlit-app.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import simplejson as json
import streamlit as st
from kafka import KafkaConsumer
from streamlit_autorefresh import st_autorefresh
import psycopg2
# Function to create a Kafka consumer
def create_kafka_consumer(topic_name):
# Set up a Kafka consumer with specified topic and configurations
consumer = KafkaConsumer(
topic_name,
bootstrap_servers='localhost:9092',
auto_offset_reset='earliest',
value_deserializer=lambda x: json.loads(x.decode('utf-8')))
return consumer
# Function to fetch voting statistics from PostgreSQL database
@st.cache_data
def fetch_voting_stats():
# Connect to PostgreSQL database
conn = psycopg2.connect("host=localhost dbname=voting user=postgres password=postgres")
cur = conn.cursor()
# Fetch total number of voters
cur.execute("""
SELECT count(*) voters_count FROM voters
""")
voters_count = cur.fetchone()[0]
# Fetch total number of candidates
cur.execute("""
SELECT count(*) candidates_count FROM candidates
""")
candidates_count = cur.fetchone()[0]
return voters_count, candidates_count
# Function to fetch data from Kafka
def fetch_data_from_kafka(consumer):
# Poll Kafka consumer for messages within a timeout period
messages = consumer.poll(timeout_ms=1000)
data = []
# Extract data from received messages
for message in messages.values():
for sub_message in message:
data.append(sub_message.value)
return data
# Function to plot a colored bar chart for vote counts per candidate
def plot_colored_bar_chart(results):
data_type = results['candidate_name']
colors = plt.cm.viridis(np.linspace(0, 1, len(data_type)))
plt.bar(data_type, results['total_votes'], color=colors)
plt.xlabel('Candidate')
plt.ylabel('Total Votes')
plt.title('Vote Counts per Candidate')
plt.xticks(rotation=90)
return plt
# Function to plot a donut chart for vote distribution
def plot_donut_chart(data: pd.DataFrame, title='Donut Chart', type='candidate'):
if type == 'candidate':
labels = list(data['candidate_name'])
elif type == 'gender':
labels = list(data['gender'])
sizes = list(data['total_votes'])
fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
ax.axis('equal')
plt.title(title)
return fig
# Function to plot a pie chart for vote distribution
def plot_pie_chart(data, title='Gender Distribution of Voters', labels=None):
sizes = list(data.values())
if labels is None:
labels = list(data.keys())
fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
ax.axis('equal')
plt.title(title)
return fig
# Function to split a dataframe into chunks for pagination
@st.cache_data(show_spinner=False)
def split_frame(input_df, rows):
df = [input_df.loc[i: i + rows - 1, :] for i in range(0, len(input_df), rows)]
return df
# Function to paginate a table
def paginate_table(table_data):
top_menu = st.columns(3)
with top_menu[0]:
sort = st.radio("Sort Data", options=["Yes", "No"], horizontal=1, index=1)
if sort == "Yes":
with top_menu[1]:
sort_field = st.selectbox("Sort By", options=table_data.columns)
with top_menu[2]:
sort_direction = st.radio(
"Direction", options=["⬆️", "⬇️"], horizontal=True
)
table_data = table_data.sort_values(
by=sort_field, ascending=sort_direction == "⬆️", ignore_index=True
)
pagination = st.container()
bottom_menu = st.columns((4, 1, 1))
with bottom_menu[2]:
batch_size = st.selectbox("Page Size", options=[10, 25, 50, 100])
with bottom_menu[1]:
total_pages = (
int(len(table_data) / batch_size) if int(len(table_data) / batch_size) > 0 else 1
)
current_page = st.number_input(
"Page", min_value=1, max_value=total_pages, step=1
)
with bottom_menu[0]:
st.markdown(f"Page **{current_page}** of **{total_pages}** ")
pages = split_frame(table_data, batch_size)
pagination.dataframe(data=pages[current_page - 1], use_container_width=True)
# Function to update data displayed on the dashboard
def update_data():
# Placeholder to display last refresh time
last_refresh = st.empty()
last_refresh.text(f"Last refreshed at: {time.strftime('%Y-%m-%d %H:%M:%S')}")
# Fetch voting statistics
voters_count, candidates_count = fetch_voting_stats()
# Display total voters and candidates metrics
st.markdown("""---""")
col1, col2 = st.columns(2)
col1.metric("Total Voters", voters_count)
col2.metric("Total Candidates", candidates_count)
# Fetch data from Kafka on aggregated votes per candidate
consumer = create_kafka_consumer("aggregated_votes_per_candidate")
data = fetch_data_from_kafka(consumer)
results = pd.DataFrame(data)
# Identify the leading candidate
results = results.loc[results.groupby('candidate_id')['total_votes'].idxmax()]
leading_candidate = results.loc[results['total_votes'].idxmax()]
# Display leading candidate information
st.markdown("""---""")
st.header('Leading Candidate')
col1, col2 = st.columns(2)
with col1:
st.image(leading_candidate['photo_url'], width=200)
with col2:
st.header(leading_candidate['candidate_name'])
st.subheader(leading_candidate['party_affiliation'])
st.subheader("Total Vote: {}".format(leading_candidate['total_votes']))
# Display statistics and visualizations
st.markdown("""---""")
st.header('Statistics')
results = results[['candidate_id', 'candidate_name', 'party_affiliation', 'total_votes']]
results = results.reset_index(drop=True)
col1, col2 = st.columns(2)
# Display bar chart and donut chart
with col1:
bar_fig = plot_colored_bar_chart(results)
st.pyplot(bar_fig)
with col2:
donut_fig = plot_donut_chart(results, title='Vote Distribution')
st.pyplot(donut_fig)
# Display table with candidate statistics
st.table(results)
# Fetch data from Kafka on aggregated turnout by location
location_consumer = create_kafka_consumer("aggregated_turnout_by_location")
location_data = fetch_data_from_kafka(location_consumer)
location_result = pd.DataFrame(location_data)
# Identify locations with maximum turnout
location_result = location_result.loc[location_result.groupby('state')['count'].idxmax()]
location_result = location_result.reset_index(drop=True)
# Display location-based voter information with pagination
st.header("Location of Voters")
paginate_table(location_result)
# Update the last refresh time
st.session_state['last_update'] = time.time()
# Sidebar layout
def sidebar():
# Initialize last update time if not present in session state
if st.session_state.get('last_update') is None:
st.session_state['last_update'] = time.time()
# Slider to control refresh interval
refresh_interval = st.sidebar.slider("Refresh interval (seconds)", 5, 60, 10)
st_autorefresh(interval=refresh_interval * 1000, key="auto")
# Button to manually refresh data
if st.sidebar.button('Refresh Data'):
update_data()
# Title of the Streamlit dashboard
st.title('Real-time Election Dashboard')
topic_name = 'aggregated_votes_per_candidate'
# Display sidebar
sidebar()
# Update and display data on the dashboard
update_data()