import gradio as gr | |
# Function to update the second dropdown based on the first | |
def update_dropdown(class_prefix): | |
# This generates a list of values like "5H01", "5H02", ..., "5H40" | |
return [f"{class_prefix}{str(i).zfill(2)}" for i in range(1, 41)] | |
# This is the initial value for the first dropdown | |
initial_class = "5H" | |
# Create the Gradio interface | |
with gr.Blocks() as demo: | |
# The first dropdown is initialized with "5H" as the default value | |
class_dropdown = gr.Dropdown(label="Class", choices=["5H", "5E", "5RS"], value=initial_class) | |
# The second dropdown is initialized with the values corresponding to "5H" | |
student_dropdown = gr.Dropdown(label="Student", choices=update_dropdown(initial_class)) | |
# When the first dropdown changes, the second dropdown's choices are updated | |
class_dropdown.change(fn=update_dropdown, inputs=class_dropdown, outputs=student_dropdown) | |
# Launch the app | |
demo.launch() |