Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 6e8f2f4

Browse files
[feat] add checkbox group tutorial with favorite colors selection
1 parent d6fd78c commit 6e8f2f4

File tree

1 file changed

+39
-0
lines changed
  • 8_how-to-create-a-checkbox-group

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
app.py
3+
4+
This file contains a simple Gradio application that demonstrates how to create a checkbox group for user input.
5+
"""
6+
7+
import gradio as gr
8+
9+
# This function processes the selected colors from the checkbox group.
10+
# It returns a message indicating which colors were selected.
11+
def favorite_colors(selected_colors):
12+
"""
13+
This function takes a list of selected colors and returns a message.
14+
If no colors are selected, it returns a different message.
15+
16+
Args:
17+
selected_colors (list): A list of colors selected by the user.
18+
19+
Returns:
20+
str: A message indicating the selected colors or that no colors were selected.
21+
"""
22+
if not selected_colors:
23+
return "You didn't select any colors."
24+
return f"You selected: {', '.join(selected_colors)}"
25+
26+
# Create a Gradio interface for a checkbox group question
27+
iface = gr.Interface(
28+
fn=favorite_colors,
29+
inputs=gr.CheckboxGroup(
30+
choices=["Red", "Green", "Blue", "Yellow"],
31+
label="Select your favorite colors",
32+
type="value"
33+
),
34+
outputs="text"
35+
)
36+
37+
if __name__ == "__main__":
38+
# Launch the Gradio interface
39+
iface.launch()

0 commit comments

Comments
 (0)