Tom Aarsen commited on
Commit
2d428eb
1 Parent(s): 56def8a

Introduce tabs <-> URL relation for easier sharing

Browse files
Files changed (1) hide show
  1. app.py +99 -19
app.py CHANGED
@@ -1820,30 +1820,89 @@ data = {
1820
  ]
1821
  }
1822
  }
 
1823
  dataframes = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1824
 
1825
  with gr.Blocks(css=css) as block:
1826
- with gr.Tabs():
 
 
 
 
 
 
 
 
 
1827
  for task, task_values in data.items():
1828
  metric = task_values["metric"]
1829
- with gr.Tab(task):
1830
- for item in task_values["data"]:
1831
- with gr.Tab(item["language"]):
1832
- with gr.Row():
1833
- gr.Markdown(f"""
1834
- {item['description']}
1835
-
1836
- - **Metric:** {metric}
1837
- - **Languages:** {item['language_long'] if 'language_long' in item else item['language']}
1838
- {"- **Credits:** " + item['credits'] if "credits" in item else ''}
1839
- """)
1840
- with gr.Row():
1841
- datatype = ["number", "markdown"] + ["number"] * len(item["data"])
1842
- dataframe = gr.Dataframe(item["data"], datatype=datatype, type="pandas", height=600)
1843
- dataframes.append(dataframe)
1844
- with gr.Row():
1845
- refresh_button = gr.Button("Refresh")
1846
- refresh_button.click(item["refresh"], inputs=None, outputs=dataframe)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1847
 
1848
  gr.Markdown(f"""
1849
  - **Total Datasets**: {NUM_DATASETS}
@@ -1866,6 +1925,27 @@ with gr.Blocks(css=css) as block:
1866
  ```
1867
  """)
1868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1869
  block.queue(max_size=10)
1870
  block.launch()
1871
 
 
1820
  ]
1821
  }
1822
  }
1823
+
1824
  dataframes = []
1825
+ tabs = []
1826
+
1827
+ # The following JavaScript function updates the URL parameters based on the selected task and language
1828
+ # Additionally, `update_url_task` and `update_url_language` are used to update the current task and language
1829
+ # The current task and language are stored in the `current_task_language` and `language_per_task` JSON objects
1830
+ # This is all a bit hacky, but it might be the only way to pass options to a JavaScript function via Gradio
1831
+ set_window_url_params = """
1832
+ function(goalUrlObject) {
1833
+ const params = new URLSearchParams(window.location.search);
1834
+ for (const [key, value] of Object.entries(goalUrlObject)) {
1835
+ params.set(key, value);
1836
+ };
1837
+ const queryString = '?' + params.toString();
1838
+ console.log(queryString);
1839
+ window.history.replaceState({}, '', queryString);
1840
+ return [];
1841
+ }
1842
+ """
1843
+
1844
+ def update_url_task(event: gr.SelectData, current_task_language: dict, language_per_task: dict):
1845
+ current_task_language["task"] = event.target.id
1846
+ # Either use the cached language for this task or the 1st language
1847
+ current_task_language["language"] = language_per_task.get(event.target.id, event.target.children[0].children[0].id)
1848
+ return current_task_language, language_per_task
1849
+
1850
+ def update_url_language(event: gr.SelectData, current_task_language: dict, language_per_task: dict):
1851
+ current_task_language["language"] = event.target.id
1852
+ if "task" not in current_task_language:
1853
+ current_task_language["task"] = "overall"
1854
+ language_per_task[current_task_language["task"]] = event.target.id
1855
+ return current_task_language, language_per_task
1856
 
1857
  with gr.Blocks(css=css) as block:
1858
+
1859
+ # Store the current task and language for updating the URL. This is a bit hacky, but it works
1860
+ # for passing the current task and language to the JavaScript function via Gradio
1861
+ current_task_language = gr.JSON(value=dict(), visible=False)
1862
+ language_per_task = gr.JSON(value=dict(), visible=False)
1863
+
1864
+ with gr.Tabs() as outer_tabs:
1865
+ # Store the tabs for updating them on load based on URL parameters
1866
+ tabs.append(outer_tabs)
1867
+
1868
  for task, task_values in data.items():
1869
  metric = task_values["metric"]
1870
+ task_tab_id = task.lower().replace(" ", "-")
1871
+
1872
+ # Overall, Bitext Mining, Classification, etc.
1873
+ with gr.Tab(task, id=task_tab_id) as task_tab:
1874
+ # For updating the 'task' in the URL
1875
+ task_tab.select(update_url_task, [current_task_language, language_per_task], [current_task_language, language_per_task]).then(None, [current_task_language], [], js=set_window_url_params)
1876
+
1877
+ with gr.Tabs() as task_tabs:
1878
+ # Store the task tabs for updating them on load based on URL parameters
1879
+ tabs.append(task_tabs)
1880
+
1881
+ for item in task_values["data"]:
1882
+ item_tab_id = item["language"].lower().replace(" ", "-")
1883
+
1884
+ # English, Chinese, French, etc.
1885
+ with gr.Tab(item["language"], id=item_tab_id) as item_tab:
1886
+ # For updating the 'language' in the URL
1887
+ item_tab.select(update_url_language, [current_task_language, language_per_task], [current_task_language, language_per_task], trigger_mode="always_last").then(None, [current_task_language], [], js=set_window_url_params)
1888
+
1889
+ with gr.Row():
1890
+ gr.Markdown(f"""
1891
+ {item['description']}
1892
+
1893
+ - **Metric:** {metric}
1894
+ - **Languages:** {item['language_long'] if 'language_long' in item else item['language']}
1895
+ {"- **Credits:** " + item['credits'] if "credits" in item else ''}
1896
+ """)
1897
+
1898
+ with gr.Row():
1899
+ datatype = ["number", "markdown"] + ["number"] * len(item["data"])
1900
+ dataframe = gr.Dataframe(item["data"], datatype=datatype, type="pandas", height=600)
1901
+ dataframes.append(dataframe)
1902
+
1903
+ with gr.Row():
1904
+ refresh_button = gr.Button("Refresh")
1905
+ refresh_button.click(item["refresh"], inputs=None, outputs=dataframe)
1906
 
1907
  gr.Markdown(f"""
1908
  - **Total Datasets**: {NUM_DATASETS}
 
1925
  ```
1926
  """)
1927
 
1928
+ def set_tabs_on_load(request: gr.Request):
1929
+ """Set the selected tab based on the URL parameters on load."""
1930
+ global tabs
1931
+ valid_task_keys = [child.id for child in tabs[0].children]
1932
+ return_tabs = [gr.Tabs()] * len(tabs)
1933
+
1934
+ query_params = request.request.query_params
1935
+ task_key = query_params.get("task", "overall")
1936
+ if task_key not in valid_task_keys:
1937
+ task_key = "overall"
1938
+ return_tabs[0] = gr.Tabs(selected=task_key)
1939
+
1940
+ tabs_idx = valid_task_keys.index(task_key) + 1
1941
+ language_key = query_params.get("language", "english")
1942
+ return_tabs[tabs_idx] = gr.Tabs(selected=language_key)
1943
+ current_task_language = {"task": task_key, "language": language_key}
1944
+ language_per_task = {task_key: language_key}
1945
+ return return_tabs + [current_task_language, language_per_task]
1946
+
1947
+ block.load(set_tabs_on_load, inputs=[], outputs=tabs + [current_task_language, language_per_task])
1948
+
1949
  block.queue(max_size=10)
1950
  block.launch()
1951