internal-v0 / ui /selection_page.py
carlosh93's picture
Updating app version July 15th, 2025
1cfc9e8
import os.path
import gradio as gr
from functools import partial
from .main_page import sort_with_pyuca
def update_language_options(selected_country, metadata):
with open(metadata["USA"]["English"]["Intro"], "r", encoding="utf-8") as f:
INTRO_TEXT = f.read() # default
# Retrieve the list of languages available for the selected country.
if selected_country in metadata:
languages = sort_with_pyuca(list(metadata[selected_country].keys()))
if len(languages) == 0:
gr.Warning("No languages available for the selected country.")
return gr.Dropdown(choices=[], interactive=False, value=None), gr.Markdown(INTRO_TEXT)
# select the first language as default
fn = metadata[selected_country][languages[0]]["Intro"]
if os.path.exists(fn):
with open(fn, "r", encoding="utf-8") as f:
INTRO_TEXT = f.read()
# Use gr.Dropdown.update to change the choices of the language dropdown.
val = languages[0] if len(languages) == 1 else None
return gr.Dropdown(choices=languages, interactive=True, value=val, allow_custom_value=False), gr.Markdown(INTRO_TEXT)
else:
return gr.Dropdown(choices=[], interactive=False, value=None, allow_custom_value=False), gr.Markdown(INTRO_TEXT)
def build_selection_page(metadata_dict):
with gr.Column(visible=True, elem_classes=["compact-container"]) as selection_page:
with gr.Row():
with open(metadata_dict["USA"]["English"]["Intro"], "r", encoding="utf-8") as f:
INTRO_TEXT = f.read()
intro_markdown = gr.Markdown(INTRO_TEXT)
gr.Markdown("## Please select your country")
countries = sort_with_pyuca(list(metadata_dict.keys()))
country_choice = gr.Dropdown(
choices=countries, label="Country", allow_custom_value=False, value=None,
)
language_choice = gr.Dropdown(
choices=[],
label="Language",
allow_custom_value=False,
interactive=False # Initially disabled.
)
# When the country selection changes, update the language dropdown.
country_choice.change(
fn=partial(update_language_options, metadata=metadata_dict),
inputs=country_choice,
outputs=[language_choice, intro_markdown]
)
with gr.Row():
username = gr.Textbox(label="Email (optional)", type="email", elem_id="username_text")
password = gr.Textbox(label="Password (optional)", type="password", elem_id="password_text")
proceed_btn = gr.Button("Proceed")
return selection_page, country_choice, language_choice, proceed_btn, username, password, intro_markdown