Plinio Guzman commited on
Commit
265b33b
2 Parent(s): 7a95d7a 7c17d65

Merge pull request #4 from openbiodiversity/feat/project-dropdown

Browse files
Files changed (2) hide show
  1. app.py +37 -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,14 @@ 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 +352,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 +375,28 @@ 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(author_id):
324
+ return con.sql(
325
+ f"""
326
+ SELECT DISTINCT name FROM project WHERE authorId = '{author_id}'
327
+ """
328
+ ).df()
329
+
330
+
331
  with gr.Blocks() as demo:
332
  # Environment setup
333
  authenticate_gee(GEE_SERVICE_ACCOUNT, GEE_SERVICE_ACCOUNT_CREDENTIALS_FILE)
 
352
  with gr.Row():
353
  start_year = gr.Number(value=2017, label="Start Year", precision=0)
354
  end_year = gr.Number(value=2022, label="End Year", precision=0)
355
+ # project_name = gr.Textbox(label="Project Name")
356
+ project_name = gr.Dropdown([], label="Project", value="Select project")
357
  # boroughs = gr.CheckboxGroup(choices=["Queens", "Brooklyn", "Manhattan", "Bronx", "Staten Island"], value=["Queens", "Brooklyn"], label="Select Methodology:")
358
  # btn = gr.Button(value="Update Filter")
359
  with gr.Row():
 
375
  view_btn.click(view_all, outputs=results_df)
376
  save_btn.click(push_to_md)
377
 
378
+ def update_project_dropdown_list(url_params):
379
+ username = url_params.get("username", "default")
380
+ projects = motherduck_list_projects(author_id=username)
381
+ # to-do: filter projects based on user
382
+ return gr.Dropdown.update(choices=projects["name"].tolist())
383
+
384
+ # Get url params
385
+ url_params = gr.JSON({"username": "default"}, visible=False, label="URL Params")
386
+
387
+ # Gradio has a bug
388
+ # For dropdown to update by demo.load, dropdown value must be called downstream
389
+ b1 = gr.Button("Hidden button that fixes bug.", visible=False)
390
+ b1.click(lambda x: x, inputs=project_name, outputs=[])
391
+
392
+ # Update project dropdown list on page load
393
+ demo.load(
394
+ fn=update_project_dropdown_list,
395
+ inputs=[url_params],
396
+ outputs=[project_name],
397
+ _js=get_window_url_params,
398
+ queue=False,
399
+ )
400
+
401
+
402
  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
+ """