Spaces:
Sleeping
Sleeping
| 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") | |
| with gr.Row(): | |
| login_btn = gr.Button("Login", elem_id="login_btn") | |
| sign_up_btn = gr.Button("Sign up", elem_id="sign_up_btn") | |
| reset_password_btn = gr.Button("Reset Password", elem_id="reset_password_btn") | |
| logout_btn = gr.Button("Logout", elem_id="logout_btn",visible=False) | |
| login_status = gr.Markdown("") | |
| with gr.Row(): | |
| proceed_btn = gr.Button("Proceed") | |
| with gr.Row(): | |
| change_password_field = gr.Textbox( | |
| label="Change Password", type="password", elem_id="change_password_field", visible=True | |
| ) | |
| change_password_field_confirm = gr.Textbox( | |
| label="Confirm New Password", type="password", elem_id="change_password_field_confirm", visible=True | |
| ) | |
| with gr.Row(): | |
| change_password_btn = gr.Button("Change Password", elem_id="change_password_btn", visible=True) | |
| change_password_status = gr.Markdown("") | |
| return selection_page, country_choice, language_choice, proceed_btn, username, password, intro_markdown, login_btn, sign_up_btn, reset_password_btn, login_status, logout_btn, change_password_field, change_password_field_confirm, change_password_btn, change_password_status |