Update app.py
Browse files
app.py
CHANGED
@@ -197,79 +197,75 @@ with gr.Blocks() as demo:
|
|
197 |
run_button = gr.Button("Run Query")
|
198 |
|
199 |
def update_topic_content(topic_name: str)-> Tuple[Any, Dict[str, str], str]:
|
200 |
-
"""Given a topic name, updates the UI dropdown choices with the corresponding content,
|
201 |
-
and returns the content dictionary (what content is available under given topic_name) and internal topic code (ID of the topic_name).
|
202 |
-
|
203 |
-
Args:
|
204 |
-
topic_name (str): The name of the selected topic. Possible topic names: 'Darbs', 'Iedzīvotāji', 'Informācijas tehnoloģijas', 'Izglītība, kultūra un zinātne', 'Nozares',\
|
205 |
-
'Sociālā aizsardzība un veselība', 'Tirdzniecība un pakalpojumi', 'Uzņēmējdarbība', 'Valsts un ekonomika', 'Vide'.
|
206 |
-
Returns:
|
207 |
-
tuple:
|
208 |
-
- gr.update: Gradio UI update object with new dropdown choices and visibility set to True.
|
209 |
-
- content_dict (dict): Dictionary containing content entries for the selected topic.
|
210 |
-
- topic_code (str): Internal code corresponding to the topic name.
|
211 |
-
"""
|
212 |
topic_code = topic_dict[topic_name]
|
213 |
content_dict = get_topic_content(topic_code)
|
214 |
return gr.update(choices=list(content_dict.keys()), visible=True), content_dict, topic_code
|
215 |
|
216 |
-
def update_reports(topic_content_name: str, content_dict: dict = None
|
217 |
-
"""Updates the UI dropdown menu with available report titles for a selected topic content, and returns the titles dictionary and the corresponding content code.
|
218 |
-
|
219 |
-
Args:
|
220 |
-
topic_content_name (str): The name of the selected topic content (e.g., "Darba samaksa (algas)"). Topic content names you can get from `update_topic_content` (`content_dict` object).
|
221 |
-
content_dict (dict): A dictionary mapping topic content names to their corresponding content codes. Obtained from `update_topic_content` (2nd returned object: `content_dict`).
|
222 |
-
topic_name (str): The name of the selected topic. Possible topic names: 'Darbs', 'Iedzīvotāji', 'Informācijas tehnoloģijas', 'Izglītība, kultūra un zinātne', 'Nozares',\
|
223 |
-
'Sociālā aizsardzība un veselība', 'Tirdzniecība un pakalpojumi', 'Uzņēmējdarbība', 'Valsts un ekonomika', 'Vide'.
|
224 |
-
Returns:
|
225 |
-
tuple:
|
226 |
-
- gr.update: A Gradio UI update object to populate a dropdown with the list of report titles and make it visible.
|
227 |
-
- titles_dict (dict): A dictionary of available report titles from the Official Statistics Portal
|
228 |
-
of Latvia (CSP). Each key is a human-readable report title, and each value
|
229 |
-
is a list of metadata: `[topic_code, topic_content_code, sub_content_code, report_id]`.
|
230 |
-
This list (as a string) of metadata later can be used in the `run_get_csp_data` function (into topic_params_str parameter)
|
231 |
-
- topic_content_code (str): The internal code associated with the selected topic content, used to retrieve titles via the `get_titles` function.
|
232 |
-
Dependencies:
|
233 |
-
- Relies on `get_titles(topic_content_code)` to fetch metadata from the CSP's API at:
|
234 |
-
https://data.stat.gov.lv/api/v1/lv/OSP_PUB?query=*&filter=*
|
235 |
-
"""
|
236 |
-
if len(topic_name) > 0:
|
237 |
-
topic_code = topic_dict[topic_name]
|
238 |
-
content_dict = get_topic_content(topic_code)
|
239 |
-
|
240 |
topic_content_code = content_dict[topic_content_name]
|
241 |
titles_dict = get_titles(topic_content_code)
|
242 |
return titles_dict, topic_content_code, gr.update(choices=list(titles_dict.keys()), visible=True)
|
243 |
|
244 |
def update_topic_params_and_link(report_title: str, titles_dict: dict) -> Tuple[str, Any, Any]:
|
245 |
-
"""Prepares and returns metadata, a hyperlink, and query parameter preview for a selected report
|
246 |
-
from the Official Statistics Portal of Latvia (CSP).
|
247 |
-
|
248 |
-
Args:
|
249 |
-
report_title (str): The title of the selected report, as shown in the dropdown.
|
250 |
-
titles_dict (dict): Dictionary mapping report titles to their metadata list:
|
251 |
-
[topic_code, content_code, sub_content_code, report_id],
|
252 |
-
typically retrieved using `get_titles(topic_content_code)`.
|
253 |
-
Returns:
|
254 |
-
tuple:
|
255 |
-
- topic_params_str (str): String representation of the internal report metadata (code list),
|
256 |
-
useful for debugging or internal reference.
|
257 |
-
This scring can be used in the `run_get_csp_data` (parameter `topic_params_str`).
|
258 |
-
- gr.update: Gradio component update with a Markdown-style hyperlink pointing to the
|
259 |
-
CSP page for the selected report.
|
260 |
-
- gr.update: Gradio component update showing a sample query parameter, particularly
|
261 |
-
for the `TIME` dimension if present, using the most recent 3 values.
|
262 |
-
Details:
|
263 |
-
- The function extracts the internal metadata for the selected report.
|
264 |
-
- It generates a URL using `construct_csp_link(...)` that links directly to the CSP report page.
|
265 |
-
- It attempts to fetch available query parameters using `get_query_values(...)`, then isolates
|
266 |
-
the `TIME` filter and selects the last 3 available values (e.g., most recent years).
|
267 |
-
- If fetching query parameters fails, an empty dictionary (`'{}'`) is returned as the fallback.
|
268 |
-
Example Output:
|
269 |
-
- topic_params_str: "['POP', 'ID', 'IDS', 'IDS010']"
|
270 |
-
- link (Markdown): "[Dzimušo skaits pēc dzimuma](https://data.stat.gov.lv/.../IDS010)"
|
271 |
-
- query_str: "{'TIME': ['2020', '2021', '2022']}"
|
272 |
-
"""
|
273 |
title_value = titles_dict[report_title]
|
274 |
topic_params_str = str(title_value)
|
275 |
link = construct_csp_link(title_value)
|
|
|
197 |
run_button = gr.Button("Run Query")
|
198 |
|
199 |
def update_topic_content(topic_name: str)-> Tuple[Any, Dict[str, str], str]:
|
200 |
+
# """Given a topic name, updates the UI dropdown choices with the corresponding content,
|
201 |
+
# and returns the content dictionary (what content is available under given topic_name) and internal topic code (ID of the topic_name).
|
202 |
+
#
|
203 |
+
# Args:
|
204 |
+
# topic_name (str): The name of the selected topic. Possible topic names: 'Darbs', 'Iedzīvotāji', 'Informācijas tehnoloģijas', 'Izglītība, kultūra un zinātne', 'Nozares',\
|
205 |
+
# 'Sociālā aizsardzība un veselība', 'Tirdzniecība un pakalpojumi', 'Uzņēmējdarbība', 'Valsts un ekonomika', 'Vide'.
|
206 |
+
# Returns:
|
207 |
+
# tuple:
|
208 |
+
# - gr.update: Gradio UI update object with new dropdown choices and visibility set to True.
|
209 |
+
# - content_dict (dict): Dictionary containing content entries for the selected topic.
|
210 |
+
# - topic_code (str): Internal code corresponding to the topic name.
|
211 |
+
# """
|
212 |
topic_code = topic_dict[topic_name]
|
213 |
content_dict = get_topic_content(topic_code)
|
214 |
return gr.update(choices=list(content_dict.keys()), visible=True), content_dict, topic_code
|
215 |
|
216 |
+
def update_reports(topic_content_name: str, content_dict: dict = None) -> Tuple[Dict[str, str], str, Any]:
|
217 |
+
# """Updates the UI dropdown menu with available report titles for a selected topic content, and returns the titles dictionary and the corresponding content code.
|
218 |
+
#
|
219 |
+
# Args:
|
220 |
+
# topic_content_name (str): The name of the selected topic content (e.g., "Darba samaksa (algas)"). Topic content names you can get from `update_topic_content` (`content_dict` object).
|
221 |
+
# content_dict (dict): A dictionary mapping topic content names to their corresponding content codes. Obtained from `update_topic_content` (2nd returned object: `content_dict`).
|
222 |
+
# topic_name (str): The name of the selected topic. Possible topic names: 'Darbs', 'Iedzīvotāji', 'Informācijas tehnoloģijas', 'Izglītība, kultūra un zinātne', 'Nozares',\
|
223 |
+
# 'Sociālā aizsardzība un veselība', 'Tirdzniecība un pakalpojumi', 'Uzņēmējdarbība', 'Valsts un ekonomika', 'Vide'.
|
224 |
+
# Returns:
|
225 |
+
# tuple:
|
226 |
+
# - gr.update: A Gradio UI update object to populate a dropdown with the list of report titles and make it visible.
|
227 |
+
# - titles_dict (dict): A dictionary of available report titles from the Official Statistics Portal
|
228 |
+
# of Latvia (CSP). Each key is a human-readable report title, and each value
|
229 |
+
# is a list of metadata: `[topic_code, topic_content_code, sub_content_code, report_id]`.
|
230 |
+
# This list (as a string) of metadata later can be used in the `run_get_csp_data` function (into topic_params_str parameter)
|
231 |
+
# - topic_content_code (str): The internal code associated with the selected topic content, used to retrieve titles via the `get_titles` function.
|
232 |
+
# Dependencies:
|
233 |
+
# - Relies on `get_titles(topic_content_code)` to fetch metadata from the CSP's API at:
|
234 |
+
# https://data.stat.gov.lv/api/v1/lv/OSP_PUB?query=*&filter=*
|
235 |
+
# """
|
|
|
|
|
|
|
|
|
236 |
topic_content_code = content_dict[topic_content_name]
|
237 |
titles_dict = get_titles(topic_content_code)
|
238 |
return titles_dict, topic_content_code, gr.update(choices=list(titles_dict.keys()), visible=True)
|
239 |
|
240 |
def update_topic_params_and_link(report_title: str, titles_dict: dict) -> Tuple[str, Any, Any]:
|
241 |
+
# """Prepares and returns metadata, a hyperlink, and query parameter preview for a selected report
|
242 |
+
# from the Official Statistics Portal of Latvia (CSP).
|
243 |
+
#
|
244 |
+
# Args:
|
245 |
+
# report_title (str): The title of the selected report, as shown in the dropdown.
|
246 |
+
# titles_dict (dict): Dictionary mapping report titles to their metadata list:
|
247 |
+
# [topic_code, content_code, sub_content_code, report_id],
|
248 |
+
# typically retrieved using `get_titles(topic_content_code)`.
|
249 |
+
# Returns:
|
250 |
+
# tuple:
|
251 |
+
# - topic_params_str (str): String representation of the internal report metadata (code list),
|
252 |
+
# useful for debugging or internal reference.
|
253 |
+
# This scring can be used in the `run_get_csp_data` (parameter `topic_params_str`).
|
254 |
+
# - gr.update: Gradio component update with a Markdown-style hyperlink pointing to the
|
255 |
+
# CSP page for the selected report.
|
256 |
+
# - gr.update: Gradio component update showing a sample query parameter, particularly
|
257 |
+
# for the `TIME` dimension if present, using the most recent 3 values.
|
258 |
+
# Details:
|
259 |
+
# - The function extracts the internal metadata for the selected report.
|
260 |
+
# - It generates a URL using `construct_csp_link(...)` that links directly to the CSP report page.
|
261 |
+
# - It attempts to fetch available query parameters using `get_query_values(...)`, then isolates
|
262 |
+
# the `TIME` filter and selects the last 3 available values (e.g., most recent years).
|
263 |
+
# - If fetching query parameters fails, an empty dictionary (`'{}'`) is returned as the fallback.
|
264 |
+
# Example Output:
|
265 |
+
# - topic_params_str: "['POP', 'ID', 'IDS', 'IDS010']"
|
266 |
+
# - link (Markdown): "[Dzimušo skaits pēc dzimuma](https://data.stat.gov.lv/.../IDS010)"
|
267 |
+
# - query_str: "{'TIME': ['2020', '2021', '2022']}"
|
268 |
+
# """
|
269 |
title_value = titles_dict[report_title]
|
270 |
topic_params_str = str(title_value)
|
271 |
link = construct_csp_link(title_value)
|