File size: 2,091 Bytes
83c7368
12ca36b
35153c2
84d8075
6a0f6c2
12ca36b
6a0f6c2
 
12ca36b
640dc7a
fdda1da
b1ee3a4
12ca36b
ea36e3b
12ca36b
 
fdda1da
ea36e3b
12ca36b
ea36e3b
12ca36b
 
 
 
 
d03f5ec
640dc7a
d03f5ec
 
 
ea36e3b
640dc7a
ea36e3b
 
4c8c6b4
fdda1da
4b7975c
7c17d65
35153c2
77d952e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c8c6b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr

from utils import duckdb_queries as dq
from utils.gradio import get_window_url_params
from utils.indicators import IndexGenerator

# Instantiate outside gradio app to avoid re-initializing GEE, which is slow
indexgenerator = IndexGenerator()

with gr.Blocks() as demo:
    with gr.Column():
        m1 = gr.Plot()
        with gr.Row():
            project_name = gr.Dropdown([], label="Project", value="Select project")
            start_year = gr.Number(value=2017, label="Start Year", precision=0)
            end_year = gr.Number(value=2022, label="End Year", precision=0)
        with gr.Row():
            view_btn = gr.Button(value="Show project map")
            calc_btn = gr.Button(value="Calculate!")
            # save_btn = gr.Button(value="Save")
        results_df = gr.Dataframe(
            headers=["Year", "Project Name", "Score"],
            datatype=["number", "str", "number"],
            label="Biodiversity scores by year",
        )
    calc_btn.click(
        indexgenerator.calculate_biodiversity_score,
        inputs=[start_year, end_year, project_name],
        outputs=results_df,
    )
    view_btn.click(
        fn=indexgenerator.show_project_map,
        inputs=[project_name],
        outputs=[m1],
    )

    def update_project_dropdown_list(url_params):
        username = url_params.get("username", "default")
        projects = dq.list_projects_by_author(author_id=username)
        return gr.Dropdown.update(choices=projects["name"].tolist())

    # Get url params
    url_params = gr.JSON({"username": "default"}, visible=False, label="URL Params")

    # Gradio has a bug
    # For dropdown to update by demo.load, dropdown value must be called downstream
    b1 = gr.Button("Hidden button that fixes bug.", visible=False)
    b1.click(lambda x: x, inputs=project_name, outputs=[])

    # Update project dropdown list on page load
    demo.load(
        fn=update_project_dropdown_list,
        inputs=[url_params],
        outputs=[project_name],
        _js=get_window_url_params,
        queue=False,
    )

demo.launch()