Spaces:
Build error
Build error
File size: 3,689 Bytes
12fd562 83c7368 3c75706 5f4fd41 12ca36b 35153c2 84d8075 6a0f6c2 12ca36b 6a0f6c2 12fd562 12ca36b e33c38c 3c75706 5f4fd41 3c75706 640dc7a fdda1da 3c75706 12ca36b ea36e3b 12fd562 12ca36b fdda1da ea36e3b e33c38c 12ca36b 0264353 12ca36b d03f5ec 0264353 102f4ad d03f5ec 3c75706 d03f5ec ea36e3b 640dc7a ea36e3b 4c8c6b4 fdda1da 4b7975c 7c17d65 35153c2 102f4ad 12fd562 102f4ad 77d952e 12fd562 3c75706 e33c38c 12fd562 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
import os
import gradio as gr
import plotly.express as px
import pandas as pd
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()
metric_names = os.listdir('metrics')
for i in range(len(metric_names)):
metric_names[i] = metric_names[i].split('.yaml')[0].replace('_', ' ')
def toggle_metric_definition_box(text_input):
if text_input is None or text_input == '':
return indexgenerator.get_metric_file()
else:
return None
def make_timeseries_plot(df):
for col in df.columns:
df[col] = pd.to_numeric(df[col], errors='ignore')
return px.line(df, x='Year', y='Score (Area * Value)', markers=True)
with gr.Blocks() as demo:
with gr.Column():
with gr.Row():
m1 = gr.Plot(label='Project Map')
ts_plot = gr.Plot(label='Metric scores over time')
with gr.Row():
project_name = gr.Dropdown([], label="Project", value="Select project")
metric = gr.Dropdown(metric_names, label='Metric', value='Select metric')
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 metric")
metric_btn = gr.Button(value='Show/hide metric definition')
metric_docs = gr.Textbox(
label="The chosen metric is a linear combination of these components normalized to a range of 0 to 1 and with the given coefficients",
interactive=False)
results_df = gr.Dataframe(
headers=["Year", "Project Name", "Score"],
datatype=["number", "str", "number"],
label="Scores by year",
)
calc_btn.click(
indexgenerator.calculate_score,
inputs=[start_year, end_year],
outputs=results_df,
).then(
make_timeseries_plot,
inputs=results_df,
outputs=ts_plot
)
view_btn.click(
fn=indexgenerator.show_project_map,
outputs=[m1],
)
def update_project_dropdown_list(url_params):
username = url_params.get("username", "default")
projects = dq.list_projects_by_author(author_id=username)
# Initialize the first project in the list
project_names = projects['name'].tolist()
return gr.Dropdown.update(choices=project_names)
# Change the project name in the index generator object when the
# user selects a new project
project_name.change(
indexgenerator.set_project,
inputs=project_name
)
# Set the metric to be calculated
metric.change(
indexgenerator.set_metric,
inputs=metric
)
# Toggle display of metric information
metric_btn.click(
toggle_metric_definition_box,
inputs=metric_docs,
outputs=metric_docs
)
# 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()
|