pgzmnk commited on
Commit
77d952e
1 Parent(s): 7a95d7a

Project dropdown shows project names from table.

Browse files
Files changed (2) hide show
  1. app.py +32 -1
  2. utils/js.py +8 -0
app.py CHANGED
@@ -11,6 +11,9 @@ import plotly.graph_objects as go
11
  import yaml
12
  from google.oauth2 import service_account
13
 
 
 
 
14
  # Logging
15
  logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
16
 
@@ -317,6 +320,10 @@ def push_to_md():
317
  logging.info("upsert records into motherduck")
318
 
319
 
 
 
 
 
320
  with gr.Blocks() as demo:
321
  # Environment setup
322
  authenticate_gee(GEE_SERVICE_ACCOUNT, GEE_SERVICE_ACCOUNT_CREDENTIALS_FILE)
@@ -341,7 +348,8 @@ with gr.Blocks() as demo:
341
  with gr.Row():
342
  start_year = gr.Number(value=2017, label="Start Year", precision=0)
343
  end_year = gr.Number(value=2022, label="End Year", precision=0)
344
- project_name = gr.Textbox(label="Project Name")
 
345
  # boroughs = gr.CheckboxGroup(choices=["Queens", "Brooklyn", "Manhattan", "Bronx", "Staten Island"], value=["Queens", "Brooklyn"], label="Select Methodology:")
346
  # btn = gr.Button(value="Update Filter")
347
  with gr.Row():
@@ -363,4 +371,27 @@ with gr.Blocks() as demo:
363
  view_btn.click(view_all, outputs=results_df)
364
  save_btn.click(push_to_md)
365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
366
  demo.launch()
 
11
  import yaml
12
  from google.oauth2 import service_account
13
 
14
+
15
+ from utils.js import get_window_url_params
16
+
17
  # Logging
18
  logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
19
 
 
320
  logging.info("upsert records into motherduck")
321
 
322
 
323
+ def motherduck_list_projects():
324
+ return con.sql("SELECT DISTINCT name FROM project").df()
325
+
326
+
327
  with gr.Blocks() as demo:
328
  # Environment setup
329
  authenticate_gee(GEE_SERVICE_ACCOUNT, GEE_SERVICE_ACCOUNT_CREDENTIALS_FILE)
 
348
  with gr.Row():
349
  start_year = gr.Number(value=2017, label="Start Year", precision=0)
350
  end_year = gr.Number(value=2022, label="End Year", precision=0)
351
+ # project_name = gr.Textbox(label="Project Name")
352
+ project_name = gr.Dropdown([], label="Project", value="Select project")
353
  # boroughs = gr.CheckboxGroup(choices=["Queens", "Brooklyn", "Manhattan", "Bronx", "Staten Island"], value=["Queens", "Brooklyn"], label="Select Methodology:")
354
  # btn = gr.Button(value="Update Filter")
355
  with gr.Row():
 
371
  view_btn.click(view_all, outputs=results_df)
372
  save_btn.click(push_to_md)
373
 
374
+ def update_project_dropdown_list(x):
375
+ projects = motherduck_list_projects()
376
+ # to-do: filter projects based on user
377
+ return gr.Dropdown.update(choices=projects["name"].tolist())
378
+
379
+ # Get url params
380
+ url_params = gr.JSON({"username": "default"}, visible=False, label="URL Params")
381
+
382
+ # Gradio has a bug
383
+ # For dropdown to update by demo.load, dropdown value must be called downstream
384
+ b1 = gr.Button("Hidden button that fixes bug.", visible=False)
385
+ b1.click(lambda x: x, inputs=project_name, outputs=[])
386
+
387
+ # Update project dropdown list on page load
388
+ demo.load(
389
+ fn=update_project_dropdown_list,
390
+ inputs=[url_params],
391
+ outputs=[project_name],
392
+ _js=get_window_url_params,
393
+ queue=False,
394
+ )
395
+
396
+
397
  demo.launch()
utils/js.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ get_window_url_params = """
2
+ function() {
3
+ const params = new URLSearchParams(window.location.search);
4
+ const url_params = Object.fromEntries(params);
5
+ console.log('url_params', url_params)
6
+ return url_params;
7
+ }
8
+ """