Commit
·
a4206dc
1
Parent(s):
fdad3be
fix: fixed graphs display bugs
Browse files- front/tabs/tab_ipcc.py +41 -44
front/tabs/tab_ipcc.py
CHANGED
@@ -1,11 +1,9 @@
|
|
1 |
-
from operator import index
|
2 |
from random import choices
|
3 |
import gradio as gr
|
4 |
-
from typing import TypedDict
|
5 |
-
import pandas as pd
|
6 |
-
import os
|
7 |
from climateqa.engine.talk_to_data.main import ask_ipcc
|
8 |
-
from climateqa.engine.talk_to_data.ipcc.config import IPCC_MODELS, IPCC_UI_TEXT
|
|
|
9 |
|
10 |
class ipccUIElements(TypedDict):
|
11 |
tab: gr.Tab
|
@@ -30,6 +28,15 @@ async def ask_ipcc_query(query: str, index_state: int, user_id: str):
|
|
30 |
result = await ask_ipcc(query, index_state, user_id)
|
31 |
return result
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
def show_results(sql_queries_state, dataframes_state, plots_state, table_names):
|
35 |
if not sql_queries_state or not dataframes_state or not plots_state:
|
@@ -55,12 +62,15 @@ def show_results(sql_queries_state, dataframes_state, plots_state, table_names):
|
|
55 |
def show_filter_by_scenario(table_names, index_state, dataframes):
|
56 |
if len(table_names) > 0 and table_names[index_state].startswith("Choropleth Map"):
|
57 |
df = dataframes[index_state]
|
58 |
-
|
|
|
59 |
else:
|
60 |
return gr.update(visible=False)
|
61 |
|
62 |
-
def filter_by_scenario(dataframes, figures, index_state, scenario):
|
63 |
df = dataframes[index_state]
|
|
|
|
|
64 |
if df.empty:
|
65 |
return df, None
|
66 |
if "scenario" not in df.columns:
|
@@ -82,6 +92,7 @@ def display_table_names(table_names, index_state):
|
|
82 |
def on_table_click(selected_label, table_names, sql_queries, dataframes, plot_informations, plots):
|
83 |
index = table_names.index(selected_label)
|
84 |
figure = plots[index](dataframes[index])
|
|
|
85 |
return (
|
86 |
sql_queries[index],
|
87 |
dataframes[index],
|
@@ -149,7 +160,7 @@ def create_ipcc_ui() -> ipccUIElements:
|
|
149 |
|
150 |
with gr.Row():
|
151 |
scenario_selection = gr.Dropdown(
|
152 |
-
label="Scenario", choices=
|
153 |
)
|
154 |
|
155 |
with gr.Accordion(label="Informations about the plot", open=False):
|
@@ -195,6 +206,13 @@ def setup_ipcc_events(ui_elements: ipccUIElements, share_client=None, user_id=No
|
|
195 |
table_names_list = gr.State([])
|
196 |
user_id = gr.State(user_id)
|
197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
# Handle example selection
|
199 |
ui_elements["examples_hidden"].change(
|
200 |
lambda x: (gr.Accordion(open=False), gr.Textbox(value=x)),
|
@@ -205,49 +223,18 @@ def setup_ipcc_events(ui_elements: ipccUIElements, share_client=None, user_id=No
|
|
205 |
inputs=None,
|
206 |
outputs=ui_elements["image_examples"]
|
207 |
).then(
|
208 |
-
|
209 |
-
inputs=
|
210 |
-
outputs=[
|
211 |
-
ui_elements["ipcc_sql_query"],
|
212 |
-
ui_elements["ipcc_table"],
|
213 |
-
ui_elements["ipcc_display"],
|
214 |
-
ui_elements["plot_information"],
|
215 |
-
sql_queries_state,
|
216 |
-
dataframes_state,
|
217 |
-
plots_state,
|
218 |
-
plot_informations_state,
|
219 |
-
index_state,
|
220 |
-
table_names_list,
|
221 |
-
ui_elements["result_text"],
|
222 |
-
],
|
223 |
-
).then(
|
224 |
-
show_results,
|
225 |
-
inputs=[sql_queries_state, dataframes_state, plots_state, table_names_list],
|
226 |
outputs=[
|
227 |
ui_elements["result_text"],
|
228 |
ui_elements["query_accordion"],
|
229 |
ui_elements["table_accordion"],
|
230 |
ui_elements["chart_accordion"],
|
231 |
ui_elements["table_names_display"],
|
232 |
-
]
|
233 |
-
).then(
|
234 |
-
show_filter_by_scenario,
|
235 |
-
inputs=[table_names_list, index_state, dataframes_state],
|
236 |
-
outputs=[ui_elements["scenario_selection"]],
|
237 |
-
)
|
238 |
-
|
239 |
-
# Handle direct question submission
|
240 |
-
ui_elements["ipcc_direct_question"].submit(
|
241 |
-
lambda: gr.Accordion(open=False),
|
242 |
-
inputs=None,
|
243 |
-
outputs=[ui_elements["details_accordion"]]
|
244 |
-
).then(
|
245 |
-
lambda: gr.update(visible=False),
|
246 |
-
inputs=None,
|
247 |
-
outputs=ui_elements["image_examples"]
|
248 |
).then(
|
249 |
ask_ipcc_query,
|
250 |
-
inputs=[ui_elements["
|
251 |
outputs=[
|
252 |
ui_elements["ipcc_sql_query"],
|
253 |
ui_elements["ipcc_table"],
|
@@ -275,12 +262,17 @@ def setup_ipcc_events(ui_elements: ipccUIElements, share_client=None, user_id=No
|
|
275 |
show_filter_by_scenario,
|
276 |
inputs=[table_names_list, index_state, dataframes_state],
|
277 |
outputs=[ui_elements["scenario_selection"]],
|
|
|
|
|
|
|
|
|
278 |
)
|
279 |
|
|
|
280 |
# Handle model selection change
|
281 |
ui_elements["scenario_selection"].change(
|
282 |
filter_by_scenario,
|
283 |
-
inputs=[dataframes_state, plots_state, index_state, ui_elements["scenario_selection"]],
|
284 |
outputs=[ui_elements["ipcc_table"], ui_elements["ipcc_display"]],
|
285 |
)
|
286 |
|
@@ -293,8 +285,13 @@ def setup_ipcc_events(ui_elements: ipccUIElements, share_client=None, user_id=No
|
|
293 |
show_filter_by_scenario,
|
294 |
inputs=[table_names_list, index_state, dataframes_state],
|
295 |
outputs=[ui_elements["scenario_selection"]],
|
|
|
|
|
|
|
|
|
296 |
)
|
297 |
|
|
|
298 |
def create_ipcc_tab(share_client=None, user_id=None):
|
299 |
"""Create the ipcc tab with all its components and event handlers."""
|
300 |
ui_elements = create_ipcc_ui()
|
|
|
|
|
1 |
from random import choices
|
2 |
import gradio as gr
|
3 |
+
from typing import TypedDict
|
|
|
|
|
4 |
from climateqa.engine.talk_to_data.main import ask_ipcc
|
5 |
+
from climateqa.engine.talk_to_data.ipcc.config import IPCC_MODELS, IPCC_SCENARIO, IPCC_UI_TEXT
|
6 |
+
import uuid
|
7 |
|
8 |
class ipccUIElements(TypedDict):
|
9 |
tab: gr.Tab
|
|
|
28 |
result = await ask_ipcc(query, index_state, user_id)
|
29 |
return result
|
30 |
|
31 |
+
def hide_outputs():
|
32 |
+
"""Hide all outputs initially."""
|
33 |
+
return (
|
34 |
+
gr.update(visible=True), # Show the result text
|
35 |
+
gr.update(visible=False), # Hide the query accordion
|
36 |
+
gr.update(visible=False), # Hide the table accordion
|
37 |
+
gr.update(visible=False), # Hide the chart accordion
|
38 |
+
gr.update(visible=False), # Hide table names
|
39 |
+
)
|
40 |
|
41 |
def show_results(sql_queries_state, dataframes_state, plots_state, table_names):
|
42 |
if not sql_queries_state or not dataframes_state or not plots_state:
|
|
|
62 |
def show_filter_by_scenario(table_names, index_state, dataframes):
|
63 |
if len(table_names) > 0 and table_names[index_state].startswith("Choropleth Map"):
|
64 |
df = dataframes[index_state]
|
65 |
+
scenarios = sorted(df["scenario"].unique())
|
66 |
+
return gr.update(visible=True, choices=scenarios, value=scenarios[0])
|
67 |
else:
|
68 |
return gr.update(visible=False)
|
69 |
|
70 |
+
def filter_by_scenario(dataframes, figures, table_names, index_state, scenario):
|
71 |
df = dataframes[index_state]
|
72 |
+
if not table_names[index_state].startswith("Choropleth Map"):
|
73 |
+
return df, figures[index_state](df)
|
74 |
if df.empty:
|
75 |
return df, None
|
76 |
if "scenario" not in df.columns:
|
|
|
92 |
def on_table_click(selected_label, table_names, sql_queries, dataframes, plot_informations, plots):
|
93 |
index = table_names.index(selected_label)
|
94 |
figure = plots[index](dataframes[index])
|
95 |
+
|
96 |
return (
|
97 |
sql_queries[index],
|
98 |
dataframes[index],
|
|
|
160 |
|
161 |
with gr.Row():
|
162 |
scenario_selection = gr.Dropdown(
|
163 |
+
label="Scenario", choices=IPCC_SCENARIO, value=IPCC_SCENARIO[0], interactive=True, visible=False
|
164 |
)
|
165 |
|
166 |
with gr.Accordion(label="Informations about the plot", open=False):
|
|
|
206 |
table_names_list = gr.State([])
|
207 |
user_id = gr.State(user_id)
|
208 |
|
209 |
+
# Handle direct question submission - trigger the same workflow by setting examples_hidden
|
210 |
+
ui_elements["ipcc_direct_question"].submit(
|
211 |
+
lambda x: gr.update(value=x),
|
212 |
+
inputs=[ui_elements["ipcc_direct_question"]],
|
213 |
+
outputs=[ui_elements["examples_hidden"]],
|
214 |
+
)
|
215 |
+
|
216 |
# Handle example selection
|
217 |
ui_elements["examples_hidden"].change(
|
218 |
lambda x: (gr.Accordion(open=False), gr.Textbox(value=x)),
|
|
|
223 |
inputs=None,
|
224 |
outputs=ui_elements["image_examples"]
|
225 |
).then(
|
226 |
+
hide_outputs,
|
227 |
+
inputs=None,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
outputs=[
|
229 |
ui_elements["result_text"],
|
230 |
ui_elements["query_accordion"],
|
231 |
ui_elements["table_accordion"],
|
232 |
ui_elements["chart_accordion"],
|
233 |
ui_elements["table_names_display"],
|
234 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
).then(
|
236 |
ask_ipcc_query,
|
237 |
+
inputs=[ui_elements["examples_hidden"], index_state, user_id],
|
238 |
outputs=[
|
239 |
ui_elements["ipcc_sql_query"],
|
240 |
ui_elements["ipcc_table"],
|
|
|
262 |
show_filter_by_scenario,
|
263 |
inputs=[table_names_list, index_state, dataframes_state],
|
264 |
outputs=[ui_elements["scenario_selection"]],
|
265 |
+
).then(
|
266 |
+
filter_by_scenario,
|
267 |
+
inputs=[dataframes_state, plots_state, table_names_list, index_state, ui_elements["scenario_selection"]],
|
268 |
+
outputs=[ui_elements["ipcc_table"], ui_elements["ipcc_display"]],
|
269 |
)
|
270 |
|
271 |
+
|
272 |
# Handle model selection change
|
273 |
ui_elements["scenario_selection"].change(
|
274 |
filter_by_scenario,
|
275 |
+
inputs=[dataframes_state, plots_state, table_names_list, index_state, ui_elements["scenario_selection"]],
|
276 |
outputs=[ui_elements["ipcc_table"], ui_elements["ipcc_display"]],
|
277 |
)
|
278 |
|
|
|
285 |
show_filter_by_scenario,
|
286 |
inputs=[table_names_list, index_state, dataframes_state],
|
287 |
outputs=[ui_elements["scenario_selection"]],
|
288 |
+
).then(
|
289 |
+
filter_by_scenario,
|
290 |
+
inputs=[dataframes_state, plots_state, table_names_list, index_state, ui_elements["scenario_selection"]],
|
291 |
+
outputs=[ui_elements["ipcc_table"], ui_elements["ipcc_display"]],
|
292 |
)
|
293 |
|
294 |
+
|
295 |
def create_ipcc_tab(share_client=None, user_id=None):
|
296 |
"""Create the ipcc tab with all its components and event handlers."""
|
297 |
ui_elements = create_ipcc_ui()
|