Spaces:
Sleeping
Sleeping
File size: 1,753 Bytes
0794d44 |
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 |
# First, make sure you have gradio installed:
# pip install gradio
import gradio as gr
def process_form(name, age, is_student):
"""
This function takes the form inputs and processes them.
It returns a formatted string that will be displayed as the output.
"""
# Determine the student status string based on the checkbox value
student_status = "a student" if is_student else "not a student"
# Create the output message
output_message = f"Hello, {name}! You are {age} years old and you are {student_status}."
return output_message
# We use gr.Blocks() for more layout control, which is common for forms.
with gr.Blocks(theme=gr.themes.Soft()) as demo:
# Add a title using Markdown
gr.Markdown("# Simple User Information Form")
gr.Markdown("Enter your details below and click 'Submit'.")
# Define the input components for the form
with gr.Row():
name_input = gr.Textbox(label="Name", placeholder="e.g., Jane Doe")
age_input = gr.Number(label="Age", value=25) # Set a default value
student_checkbox = gr.Checkbox(label="Are you a student?")
# Define the submit button
submit_button = gr.Button("Submit")
# Define the output component where the result will be displayed
output_text = gr.Text(label="Greeting Message")
# Define the action when the submit button is clicked.
# It calls the 'process_form' function with the values from the input components
# and places the return value into the 'output_text' component.
submit_button.click(
fn=process_form,
inputs=[name_input, age_input, student_checkbox],
outputs=output_text
)
# Launch the Gradio app
if __name__ == "__main__":
demo.launch()
|