Spaces:
Sleeping
Sleeping
| # 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() | |