|
import gradio as gr |
|
|
|
|
|
def generate_checkbox_groups(textbox_input): |
|
try: |
|
num_groups = int(textbox_input) |
|
except ValueError: |
|
return "Please enter a valid number." |
|
|
|
|
|
checkbox_groups = [] |
|
for i in range(num_groups): |
|
checkbox_groups.append(gr.CheckboxGroup( |
|
choices=["Option 1", "Option 2", "Option 3"], |
|
label=f"Checkbox Group {i + 1}", |
|
interactive=True |
|
)) |
|
|
|
return checkbox_groups |
|
|
|
|
|
def process_selections(*checkbox_values): |
|
results = [] |
|
for i, selections in enumerate(checkbox_values): |
|
selected_options = ', '.join(selections) if selections else 'None' |
|
results.append(f"Selected options for group {i + 1}: {selected_options}") |
|
|
|
return "\n".join(results) |
|
|
|
with gr.Blocks() as demo: |
|
|
|
textbox = gr.Textbox(label="Enter the number of Checkbox Groups to create") |
|
|
|
|
|
generate_button = gr.Button("Generate Checkbox Groups") |
|
|
|
|
|
dynamic_checkbox_groups = gr.Column() |
|
|
|
|
|
output = gr.Textbox(label="Output") |
|
|
|
|
|
def update_dynamic_groups(textbox_value): |
|
groups = generate_checkbox_groups(textbox_value) |
|
dynamic_checkbox_groups.children = groups |
|
return dynamic_checkbox_groups |
|
|
|
generate_button.click( |
|
fn=update_dynamic_groups, |
|
inputs=textbox, |
|
outputs=dynamic_checkbox_groups |
|
) |
|
|
|
|
|
process_button = gr.Button("Submit Selections") |
|
|
|
|
|
def process_dynamic_groups(*checkbox_groups): |
|
return process_selections(*checkbox_groups) |
|
|
|
process_button.click( |
|
fn=process_dynamic_groups, |
|
inputs=dynamic_checkbox_groups.children, |
|
outputs=output |
|
) |
|
|
|
demo.launch(server_name="0.0.0.0") |