Aivis commited on
Commit
fae50e0
·
verified ·
1 Parent(s): 004d11e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -90
app.py CHANGED
@@ -8,16 +8,16 @@ from typing import Any, Dict, List, Tuple
8
  def get_topics(name:str = '') -> dict:
9
  """Available topics from Official Statistics Portal of Latvia (CSP or Centrālā statistikas pārvalde).
10
 
11
- Args:
12
- name (str): name of the topic. If not defined, function will return all available topics.
13
- Returns:
14
- dict: The dictionary of topics, where key is topic name and value is topic code.
15
- Examples:
16
- >>> get_topics('vide')
17
- {'Vide': 'ENV'}
18
- >>> print(get_topics())
19
- {'Iedzīvotāji': 'POP', 'Darbs': 'EMP', 'Sociālā aizsardzība un veselība': 'VES',...
20
- """
21
  name_capit = name.capitalize()
22
  base_url = 'https://data.stat.gov.lv/api/v1/lv/OSP_PUB/'
23
  content = requests.get(base_url)
@@ -32,22 +32,22 @@ def get_topics(name:str = '') -> dict:
32
  def get_topic_content(topic: str) -> dict:
33
  """Available contents of the topic from Official Statistics Portal of Latvia (CSP or Centrālā statistikas pārvalde).
34
 
35
- Args:
36
- topic (str): topic code. Use get_topics to get topic code.
37
- Returns:
38
- dict: The dictionary of the contents of the topic, where key is the topic content and value is the topic content code.
39
- Examples:
40
- >>> # First get topic code
41
- ... get_topics('vide')
42
- {'Vide': 'ENV'}
43
- >>> # Then use this code to get content
44
- ... print(get_topic_content('ENV'))
45
- {'Vides konti': 'VI', 'Atkritumu apsaimniekošana': 'AK', 'Agro-vides rādītāji': 'AV',...
46
- >>> get_topics('Iedzīvotāji')
47
- {'Iedzīvotāji': 'POP'}
48
- >>> print(get_topic_content('POP'))
49
- {'Iedzīvotāju skaits un raksturojošie rādītāji': 'IR', 'Dzimstība': 'ID', 'Mirstība': 'IM', 'Nāves cēloņi': 'NC',...
50
- """
51
  base_url = 'https://data.stat.gov.lv/api/v1/lv/OSP_PUB/START/'
52
  content = requests.get(base_url+topic.upper())
53
  content_short = {i['text']: i['id'] for i in content.json()}
@@ -197,49 +197,43 @@ 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
- """
201
- Given a topic name, updates the UI dropdown choices with the corresponding content,
202
  and returns the content dictionary (what content is available under given topic_name) and internal topic code (ID of the topic_name).
203
 
204
- Args:
205
- 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',\
206
- 'Sociālā aizsardzība un veselība', 'Tirdzniecība un pakalpojumi', 'Uzņēmējdarbība', 'Valsts un ekonomika', 'Vide'.
207
- Returns:
208
- tuple:
209
- - gr.update: Gradio UI update object with new dropdown choices and visibility set to True.
210
- - content_dict (dict): Dictionary containing content entries for the selected topic.
211
- - topic_code (str): Internal code corresponding to the topic name.
212
- """
213
  topic_code = topic_dict[topic_name]
214
  content_dict = get_topic_content(topic_code)
215
  return gr.update(choices=list(content_dict.keys()), visible=True), content_dict, topic_code
216
 
217
  def update_reports(topic_content_name: str, content_dict: dict = None, topic_name: str = '')-> Tuple[Dict[str, str], str, Any]:
218
- """
219
- Updates the UI dropdown menu with available report titles for a selected topic content,
220
  and returns the titles dictionary and the corresponding content code.
221
 
222
- Args:
223
- 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).
224
- content_dict (dict): A dictionary mapping topic content names to their corresponding content codes. Obtained from `update_topic_content` (2nd returned object: `content_dict`).
225
- 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',\
226
- 'Sociālā aizsardzība un veselība', 'Tirdzniecība un pakalpojumi', 'Uzņēmējdarbība', 'Valsts un ekonomika', 'Vide'.
227
-
228
- Returns:
229
- tuple:
230
- - gr.update: A Gradio UI update object to populate a dropdown with the list of report titles
231
- and make it visible.
232
- - titles_dict (dict): A dictionary of available report titles from the Official Statistics Portal
233
- of Latvia (CSP). Each key is a human-readable report title, and each value
234
- is a list of metadata: `[topic_code, topic_content_code, sub_content_code, report_id]`.
235
- This list (as a string) of metadata later can be used in the `run_get_csp_data` function (into topic_params_str parameter)
236
- - topic_content_code (str): The internal code associated with the selected topic content,
237
- used to retrieve titles via the `get_titles` function.
238
-
239
- Dependencies:
240
- - Relies on `get_titles(topic_content_code)` to fetch metadata from the CSP's API at:
241
- https://data.stat.gov.lv/api/v1/lv/OSP_PUB?query=*&filter=*
242
- """
243
  if len(topic_name) > 0:
244
  topic_code = topic_dict[topic_name]
245
  content_dict = get_topic_content(topic_code)
@@ -249,38 +243,34 @@ with gr.Blocks() as demo:
249
  return titles_dict, topic_content_code, gr.update(choices=list(titles_dict.keys()), visible=True)
250
 
251
  def update_topic_params_and_link(report_title: str, titles_dict: str)-> Tuple[str, Any, Any]:
252
- """
253
- Prepares and returns metadata, a hyperlink, and query parameter preview for a selected report
254
  from the Official Statistics Portal of Latvia (CSP).
255
 
256
- Args:
257
- report_title (str): The title of the selected report, as shown in the dropdown.
258
- titles_dict (dict): Dictionary mapping report titles to their metadata list:
259
- [topic_code, content_code, sub_content_code, report_id],
260
- typically retrieved using `get_titles(topic_content_code)`.
261
-
262
- Returns:
263
- tuple:
264
- - topic_params_str (str): String representation of the internal report metadata (code list),
265
- useful for debugging or internal reference.
266
- This scring can be used in the `run_get_csp_data` (parameter `topic_params_str`).
267
- - gr.update: Gradio component update with a Markdown-style hyperlink pointing to the
268
- CSP page for the selected report.
269
- - gr.update: Gradio component update showing a sample query parameter, particularly
270
- for the `TIME` dimension if present, using the most recent 3 values.
271
-
272
- Details:
273
- - The function extracts the internal metadata for the selected report.
274
- - It generates a URL using `construct_csp_link(...)` that links directly to the CSP report page.
275
- - It attempts to fetch available query parameters using `get_query_values(...)`, then isolates
276
- the `TIME` filter and selects the last 3 available values (e.g., most recent years).
277
- - If fetching query parameters fails, an empty dictionary (`'{}'`) is returned as the fallback.
278
-
279
- Example Output:
280
- - topic_params_str: "['POP', 'ID', 'IDS', 'IDS010']"
281
- - link (Markdown): "[Dzimušo skaits pēc dzimuma](https://data.stat.gov.lv/.../IDS010)"
282
- - query_str: "{'TIME': ['2020', '2021', '2022']}"
283
- """
284
  title_value = titles_dict[report_title]
285
  topic_params_str = str(title_value)
286
  link = construct_csp_link(title_value)
 
8
  def get_topics(name:str = '') -> dict:
9
  """Available topics from Official Statistics Portal of Latvia (CSP or Centrālā statistikas pārvalde).
10
 
11
+ Args:
12
+ name (str): name of the topic. If not defined, function will return all available topics.
13
+ Returns:
14
+ dict: The dictionary of topics, where key is topic name and value is topic code.
15
+ Examples:
16
+ >>> get_topics('vide')
17
+ {'Vide': 'ENV'}
18
+ >>> print(get_topics())
19
+ {'Iedzīvotāji': 'POP', 'Darbs': 'EMP', 'Sociālā aizsardzība un veselība': 'VES',...
20
+ """
21
  name_capit = name.capitalize()
22
  base_url = 'https://data.stat.gov.lv/api/v1/lv/OSP_PUB/'
23
  content = requests.get(base_url)
 
32
  def get_topic_content(topic: str) -> dict:
33
  """Available contents of the topic from Official Statistics Portal of Latvia (CSP or Centrālā statistikas pārvalde).
34
 
35
+ Args:
36
+ topic (str): topic code. Use get_topics to get topic code.
37
+ Returns:
38
+ dict: The dictionary of the contents of the topic, where key is the topic content and value is the topic content code.
39
+ Examples:
40
+ >>> # First get topic code
41
+ ... get_topics('vide')
42
+ {'Vide': 'ENV'}
43
+ >>> # Then use this code to get content
44
+ ... print(get_topic_content('ENV'))
45
+ {'Vides konti': 'VI', 'Atkritumu apsaimniekošana': 'AK', 'Agro-vides rādītāji': 'AV',...
46
+ >>> get_topics('Iedzīvotāji')
47
+ {'Iedzīvotāji': 'POP'}
48
+ >>> print(get_topic_content('POP'))
49
+ {'Iedzīvotāju skaits un raksturojošie rādītāji': 'IR', 'Dzimstība': 'ID', 'Mirstība': 'IM', 'Nāves cēloņi': 'NC',...
50
+ """
51
  base_url = 'https://data.stat.gov.lv/api/v1/lv/OSP_PUB/START/'
52
  content = requests.get(base_url+topic.upper())
53
  content_short = {i['text']: i['id'] for i in content.json()}
 
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, topic_name: str = '')-> Tuple[Dict[str, str], str, Any]:
217
+ """Updates the UI dropdown menu with available report titles for a selected topic content,
 
218
  and returns the titles dictionary and the corresponding content code.
219
 
220
+ Args:
221
+ 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).
222
+ content_dict (dict): A dictionary mapping topic content names to their corresponding content codes. Obtained from `update_topic_content` (2nd returned object: `content_dict`).
223
+ 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',\
224
+ 'Sociālā aizsardzība un veselība', 'Tirdzniecība un pakalpojumi', 'Uzņēmējdarbība', 'Valsts un ekonomika', 'Vide'.
225
+ Returns:
226
+ tuple:
227
+ - gr.update: A Gradio UI update object to populate a dropdown with the list of report titles and make it visible.
228
+ - titles_dict (dict): A dictionary of available report titles from the Official Statistics Portal
229
+ of Latvia (CSP). Each key is a human-readable report title, and each value
230
+ is a list of metadata: `[topic_code, topic_content_code, sub_content_code, report_id]`.
231
+ This list (as a string) of metadata later can be used in the `run_get_csp_data` function (into topic_params_str parameter)
232
+ - topic_content_code (str): The internal code associated with the selected topic content, used to retrieve titles via the `get_titles` function.
233
+ Dependencies:
234
+ - Relies on `get_titles(topic_content_code)` to fetch metadata from the CSP's API at:
235
+ https://data.stat.gov.lv/api/v1/lv/OSP_PUB?query=*&filter=*
236
+ """
 
 
 
 
237
  if len(topic_name) > 0:
238
  topic_code = topic_dict[topic_name]
239
  content_dict = get_topic_content(topic_code)
 
243
  return titles_dict, topic_content_code, gr.update(choices=list(titles_dict.keys()), visible=True)
244
 
245
  def update_topic_params_and_link(report_title: str, titles_dict: str)-> Tuple[str, Any, Any]:
246
+ """Prepares and returns metadata, a hyperlink, and query parameter preview for a selected report
 
247
  from the Official Statistics Portal of Latvia (CSP).
248
 
249
+ Args:
250
+ report_title (str): The title of the selected report, as shown in the dropdown.
251
+ titles_dict (dict): Dictionary mapping report titles to their metadata list:
252
+ [topic_code, content_code, sub_content_code, report_id],
253
+ typically retrieved using `get_titles(topic_content_code)`.
254
+ Returns:
255
+ tuple:
256
+ - topic_params_str (str): String representation of the internal report metadata (code list),
257
+ useful for debugging or internal reference.
258
+ This scring can be used in the `run_get_csp_data` (parameter `topic_params_str`).
259
+ - gr.update: Gradio component update with a Markdown-style hyperlink pointing to the
260
+ CSP page for the selected report.
261
+ - gr.update: Gradio component update showing a sample query parameter, particularly
262
+ for the `TIME` dimension if present, using the most recent 3 values.
263
+ Details:
264
+ - The function extracts the internal metadata for the selected report.
265
+ - It generates a URL using `construct_csp_link(...)` that links directly to the CSP report page.
266
+ - It attempts to fetch available query parameters using `get_query_values(...)`, then isolates
267
+ the `TIME` filter and selects the last 3 available values (e.g., most recent years).
268
+ - If fetching query parameters fails, an empty dictionary (`'{}'`) is returned as the fallback.
269
+ Example Output:
270
+ - topic_params_str: "['POP', 'ID', 'IDS', 'IDS010']"
271
+ - link (Markdown): "[Dzimušo skaits pēc dzimuma](https://data.stat.gov.lv/.../IDS010)"
272
+ - query_str: "{'TIME': ['2020', '2021', '2022']}"
273
+ """
 
 
 
274
  title_value = titles_dict[report_title]
275
  topic_params_str = str(title_value)
276
  link = construct_csp_link(title_value)