nataliegref commited on
Commit
0bbb49e
·
1 Parent(s): 77a04d8

Add download csv

Browse files
Files changed (1) hide show
  1. app.py +24 -1
app.py CHANGED
@@ -1,6 +1,7 @@
1
  # Example taken from here - https://shinylive.io/py/examples/#map
2
 
3
  import os
 
4
  from shiny import reactive, render, ui, App
5
 
6
  from plot_utils import show_one_plot, show_two_plots
@@ -21,7 +22,8 @@ app_ui = ui.page_fluid(
21
  ui.output_ui("tab_slider"),
22
  ui.input_radio_buttons("split_filter", "Apply filter?",
23
  choices=["No filter", "Filter"], selected="No filter"),
24
- ui.output_ui("page_ui")
 
25
  )
26
 
27
  def server(input, output, session):
@@ -139,5 +141,26 @@ def server(input, output, session):
139
  "Tree canopy coverage increase in %pt")
140
  return fig
141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  app = App(app_ui, server)
143
 
 
1
  # Example taken from here - https://shinylive.io/py/examples/#map
2
 
3
  import os
4
+ import io
5
  from shiny import reactive, render, ui, App
6
 
7
  from plot_utils import show_one_plot, show_two_plots
 
22
  ui.output_ui("tab_slider"),
23
  ui.input_radio_buttons("split_filter", "Apply filter?",
24
  choices=["No filter", "Filter"], selected="No filter"),
25
+ ui.output_ui("page_ui"),
26
+ ui.download_button("download_csv", "Download simulation as CSV")
27
  )
28
 
29
  def server(input, output, session):
 
141
  "Tree canopy coverage increase in %pt")
142
  return fig
143
 
144
+ @output
145
+ @render.download(filename=lambda: (
146
+ f"gdf_trees_{input.tree_pct()}pct.csv" if input.tab_choice() == "Effect of trees on temperature"
147
+ else f"gdf_temp_{input.temp_goal()}C.csv"
148
+ ))
149
+ def download_csv():
150
+ if input.tab_choice() == "Effect of trees on temperature":
151
+ df = get_gdf_trees()
152
+ else:
153
+ df = get_gdf_temp()
154
+
155
+ if not hasattr(df, "to_csv"):
156
+ raise ValueError("Expected a DataFrame or GeoDataFrame")
157
+
158
+ df = df.drop(columns="geometry", errors="ignore")
159
+
160
+ buffer = io.StringIO()
161
+ df.to_csv(buffer, index=False)
162
+ buffer.seek(0)
163
+ return buffer
164
+
165
  app = App(app_ui, server)
166