Spaces:
Sleeping
Sleeping
cassiebuhler
commited on
Commit
·
36961ed
1
Parent(s):
46725fa
pretty charts!
Browse files
app.py
CHANGED
@@ -80,7 +80,25 @@ party = (con
|
|
80 |
.cast({"geometry": "geometry"})
|
81 |
)
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
|
|
|
|
|
|
84 |
def get_summary(party, year):
|
85 |
total_measures = party.filter(_.year == year).count().execute()
|
86 |
|
@@ -88,25 +106,88 @@ def get_summary(party, year):
|
|
88 |
.filter(_.year == year)
|
89 |
.mutate(
|
90 |
# Convert 'amount' from string with '$' and ',' to numeric
|
91 |
-
amount_numeric=_.amount.replace('$', '').replace(',', '').cast('float64')
|
92 |
)
|
93 |
.group_by("party")
|
94 |
.aggregate(
|
95 |
-
|
96 |
-
|
97 |
-
.when(_.Status.isin(["Pass", "Pass*"]), _.amount_numeric)
|
98 |
-
.else_(ibis.literal(0))
|
99 |
-
.end()
|
100 |
-
.sum()
|
101 |
)
|
102 |
.mutate(color=ibis.case()
|
103 |
-
.when(_.party == "DEMOCRAT", ibis.literal("#
|
104 |
.else_(ibis.literal("#E81B23"))
|
105 |
.end())
|
106 |
)
|
107 |
|
108 |
df = df.to_pandas()
|
109 |
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
style_municipals = {
|
112 |
"layers": [
|
@@ -270,12 +351,21 @@ m.add_pmtiles(
|
|
270 |
|
271 |
m.add_layer_control()
|
272 |
m.to_streamlit()
|
273 |
-
|
|
|
|
|
|
|
|
|
274 |
|
275 |
df = get_summary(party, year)
|
276 |
-
st.bar_chart(df, x= "party",y = "percent_passed",color="color")
|
277 |
|
278 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
|
280 |
# st.divider()
|
281 |
# footer = st.container()
|
|
|
80 |
.cast({"geometry": "geometry"})
|
81 |
)
|
82 |
|
83 |
+
def get_passes(party):
|
84 |
+
df = (party
|
85 |
+
.group_by("year", "party")
|
86 |
+
.aggregate(total=_.count(),
|
87 |
+
passes = (_.Status.isin(["Pass", "Pass*"]).sum())
|
88 |
+
)
|
89 |
+
.mutate(
|
90 |
+
percent_passed=(_.passes / _.total).round(2),
|
91 |
+
color=ibis.case()
|
92 |
+
.when(_.party == "DEMOCRAT", ibis.literal("#2e4a93"))
|
93 |
+
.else_(ibis.literal("#E81B23"))
|
94 |
+
.end()
|
95 |
+
)
|
96 |
+
# # .select("year","party","percent_passed","color")
|
97 |
+
)
|
98 |
|
99 |
+
df = df.to_pandas()
|
100 |
+
return df
|
101 |
+
|
102 |
def get_summary(party, year):
|
103 |
total_measures = party.filter(_.year == year).count().execute()
|
104 |
|
|
|
106 |
.filter(_.year == year)
|
107 |
.mutate(
|
108 |
# Convert 'amount' from string with '$' and ',' to numeric
|
109 |
+
# amount_numeric=_.amount.replace('$', '').replace(',', '').cast('float64')
|
110 |
)
|
111 |
.group_by("party")
|
112 |
.aggregate(
|
113 |
+
total = _.count(),
|
114 |
+
percent_passed= (_.Status.isin(["Pass", "Pass*"]).sum() / _.count()).round(2),
|
|
|
|
|
|
|
|
|
115 |
)
|
116 |
.mutate(color=ibis.case()
|
117 |
+
.when(_.party == "DEMOCRAT", ibis.literal("#92c7f5"))
|
118 |
.else_(ibis.literal("#E81B23"))
|
119 |
.end())
|
120 |
)
|
121 |
|
122 |
df = df.to_pandas()
|
123 |
return df
|
124 |
+
|
125 |
+
# def get_cumulative(party):
|
126 |
+
|
127 |
+
# df = (party
|
128 |
+
# .select("year","amount","Status")
|
129 |
+
# # .mutate(
|
130 |
+
# # # Convert 'amount' from string with '$' and ',' to numeric
|
131 |
+
# # # amount_numeric=_.amount.replace('$', '').replace(',', '').cast('float64')
|
132 |
+
# # )
|
133 |
+
# # .group_by("party")
|
134 |
+
# # .aggregate(
|
135 |
+
# # total = _.count(),
|
136 |
+
# # percent_passed= (_.Status.isin(["Pass", "Pass*"]).sum() / _.count()).round(2),
|
137 |
+
# # )
|
138 |
+
# # .mutate(color=ibis.case()
|
139 |
+
# # .when(_.party == "DEMOCRAT", ibis.literal("#92c7f5"))
|
140 |
+
# # .else_(ibis.literal("#E81B23"))
|
141 |
+
# # .end())
|
142 |
+
# )
|
143 |
+
|
144 |
+
# df = df.to_pandas()
|
145 |
+
# return df
|
146 |
+
|
147 |
+
|
148 |
+
import altair as alt
|
149 |
+
import streamlit as st
|
150 |
+
|
151 |
+
def plot(df_passes):
|
152 |
+
chart = alt.Chart(df_passes).mark_line(strokeWidth=3).encode(
|
153 |
+
x=alt.X('year:N', title='Year'),
|
154 |
+
y=alt.Y('percent_passed:Q', title='Percent Passed'),
|
155 |
+
color=alt.Color('party:N', # Map 'party' to color
|
156 |
+
scale=alt.Scale(domain=["DEMOCRAT", "REPUBLICAN"],
|
157 |
+
range=["#1b46c2", "#E81B23"]),
|
158 |
+
legend=alt.Legend(title="Party")
|
159 |
+
)
|
160 |
+
).properties(
|
161 |
+
title='% of Measures Passed'
|
162 |
+
)
|
163 |
+
|
164 |
+
st.altair_chart(chart, use_container_width=True)
|
165 |
+
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
+
def funding_chart(party):
|
170 |
+
|
171 |
+
df = (party
|
172 |
+
.mutate(amount=_.amount.replace('$', '').replace(',', '').cast('float64'))
|
173 |
+
.filter(_.Status.isin(["Pass", "Pass*"]))
|
174 |
+
.group_by("year")
|
175 |
+
.aggregate(total_funding=_.amount.sum())
|
176 |
+
.order_by("year")
|
177 |
+
.mutate(cumulative_funding=_.total_funding.cumsum())
|
178 |
+
.execute()
|
179 |
+
)
|
180 |
+
|
181 |
+
chart = alt.Chart(df).mark_line(strokeWidth=3).encode(
|
182 |
+
x=alt.X('year:N', title='Year'),
|
183 |
+
y=alt.Y('cumulative_funding:Q', title='Cumulative Funding'),
|
184 |
+
).properties(
|
185 |
+
title='Cumulative Funding'
|
186 |
+
)
|
187 |
+
|
188 |
+
st.altair_chart(chart, use_container_width=True)
|
189 |
+
|
190 |
+
|
191 |
|
192 |
style_municipals = {
|
193 |
"layers": [
|
|
|
351 |
|
352 |
m.add_layer_control()
|
353 |
m.to_streamlit()
|
354 |
+
|
355 |
+
df_passes = get_passes(party)
|
356 |
+
# st.dataframe(df_passes)
|
357 |
+
plot(df_passes)
|
358 |
+
|
359 |
|
360 |
df = get_summary(party, year)
|
|
|
361 |
|
362 |
+
# st.line_chart(df_passes, x= "year",y = "percent_passed",color="color")
|
363 |
+
|
364 |
+
# st.bar_chart(df, x= "party",y = "percent_passed",color="color")
|
365 |
+
# Assuming df is your dataframe with 'year', 'amount', and 'Status' columns
|
366 |
+
# df_fund = get_cumulative(party)
|
367 |
+
funding_chart(party)
|
368 |
+
|
369 |
|
370 |
# st.divider()
|
371 |
# footer = st.container()
|