File size: 2,381 Bytes
fc85e7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import gradio as gr

# Function that processes the Textbox input and generates new CheckboxGroups
def generate_checkbox_groups(textbox_input):
    try:
        num_groups = int(textbox_input)
    except ValueError:
        return "Please enter a valid number."

    # Dynamically create a list of CheckboxGroups
    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

# Function to process the selections of the dynamically created CheckboxGroups
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 for user input to specify the number of CheckboxGroups
    textbox = gr.Textbox(label="Enter the number of Checkbox Groups to create")
    
    # Button to trigger the generation of CheckboxGroups
    generate_button = gr.Button("Generate Checkbox Groups")
    
    # A placeholder for the dynamically generated CheckboxGroups
    dynamic_checkbox_groups = gr.Column()
    
    # Output Textbox to display the results
    output = gr.Textbox(label="Output")

    # Generate CheckboxGroups based on Textbox input
    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
    )

    # Button to process selections from the dynamically generated CheckboxGroups
    process_button = gr.Button("Submit Selections")
    
    # Process the selections when the second button is clicked
    def process_dynamic_groups(*checkbox_groups):
        return process_selections(*checkbox_groups)
    
    process_button.click(
        fn=process_dynamic_groups,
        inputs=dynamic_checkbox_groups.children,  # Unpack the dynamically generated groups
        outputs=output
    )

demo.launch(server_name="0.0.0.0")