cassiebuhler commited on
Commit
9d9a528
·
1 Parent(s): 9d71920

adding summary table

Browse files
Files changed (1) hide show
  1. app.py +29 -12
app.py CHANGED
@@ -73,9 +73,30 @@ nonprofit_color = "#D77031" #orange
73
 
74
  from functools import reduce
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  def summary_table(column, colors, filter_cols, filter_vals):
77
- filters = [_.reGAP < 3] #only compute with gap codes 1 and 2
78
 
 
 
79
  if filter_cols and filter_vals: #if a filter is selected, add to list of filters
80
  for filter_col, filter_val in zip(filter_cols, filter_vals):
81
  if len(filter_val) > 1:
@@ -85,16 +106,11 @@ def summary_table(column, colors, filter_cols, filter_vals):
85
 
86
  combined_filter = reduce(lambda x, y: x & y, filters)
87
 
88
- df = (ca
89
- .filter(combined_filter)
90
- .group_by(column)
91
- .aggregate(percent_protected=100 * _.Acres.sum() / ca_area_acres)
92
- .mutate(percent_protected=_.percent_protected.round(1))
93
- .inner_join(colors, column)
94
- )
95
- df = df.to_pandas()
96
- df[column] = df[column].astype(str)
97
- return df
98
 
99
 
100
 
@@ -316,12 +332,13 @@ with main:
316
  map_col, stats_col = st.columns([2,1])
317
  with map_col:
318
  to_streamlit(m, height=700)
319
- df = summary_table(column, colors, filter_cols, filter_vals)
320
  total_percent = df.percent_protected.sum().round(1)
321
  with stats_col:
322
  with st.container():
323
  f"{total_percent}% CA Covered"
324
  st.altair_chart(area_plot(df, column), use_container_width=True)
 
325
 
326
 
327
 
 
73
 
74
  from functools import reduce
75
 
76
+ def get_summary(ca, combined_filter, column, colors=None):
77
+ df = (ca
78
+ .filter(combined_filter)
79
+ .group_by(*column) # unpack the list for grouping
80
+ .aggregate(percent_protected=100 * _.Acres.sum() / ca_area_acres)
81
+ .mutate(percent_protected=_.percent_protected.round(1))
82
+ )
83
+
84
+ if colors is not None and not colors.empty: # df used for chart.
85
+ df = (df #add color column + only gaps 1 and 2.
86
+ .filter(_.reGAP <3)
87
+ .inner_join(colors, column)
88
+ )
89
+ # df = df.inner_join(colors, column)
90
+
91
+ df = df.cast({col: "string" for col in column})
92
+ df = df.to_pandas()
93
+ return df
94
+
95
+
96
  def summary_table(column, colors, filter_cols, filter_vals):
 
97
 
98
+ filters = []
99
+
100
  if filter_cols and filter_vals: #if a filter is selected, add to list of filters
101
  for filter_col, filter_val in zip(filter_cols, filter_vals):
102
  if len(filter_val) > 1:
 
106
 
107
  combined_filter = reduce(lambda x, y: x & y, filters)
108
 
109
+ df = get_summary(ca, combined_filter, [column], colors) # df used for charts
110
+ df_tab = get_summary(ca, combined_filter, filter_cols, colors = None) #df used for printed table
111
+
112
+ return df, df_tab
113
+
 
 
 
 
 
114
 
115
 
116
 
 
332
  map_col, stats_col = st.columns([2,1])
333
  with map_col:
334
  to_streamlit(m, height=700)
335
+ df,df_tab = summary_table(column, colors, filter_cols, filter_vals)
336
  total_percent = df.percent_protected.sum().round(1)
337
  with stats_col:
338
  with st.container():
339
  f"{total_percent}% CA Covered"
340
  st.altair_chart(area_plot(df, column), use_container_width=True)
341
+ st.dataframe(df_tab)
342
 
343
 
344