📖 Mastering Gradio Syntax
Gradio provides modular building blocks that map directly to Python functions. This reference guide categorizes all major syntax elements including high-level interfaces, flexible layout blocks, multi-modal input/output components, and server execution methods.
1. Core Interface & Layout Architecture Syntax
Commands used to initialize applications, wrap functions, and structure layout containers.
Python Syntax Reference
import gradio as gr
# 1. High-Level Quick Interface
demo = gr.Interface(fn=my_function, inputs="text", outputs="text")
# 2. Flexible Custom Layout Blocks
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# App Title")
with gr.Row():
input_box = gr.Textbox(label="Input")
output_box = gr.Textbox(label="Output")
btn = gr.Button("Submit")
btn.click(fn=my_function, inputs=input_box, outputs=output_box)
# 3. High-Level Chat Interface
chat_demo = gr.ChatInterface(fn=bot_function, title="AI Chatbot")
# 4. Server Launch Command
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
Initializes core application wrappers, layout grids, event bindings, and server hosting execution.
2. Input Components Syntax (`gr.inputs` / Direct Components)
UI widgets allowing users to pass text, numbers, files, images, and audio into Python functions.
Python Syntax Reference
# Text & Numeric Inputs
gr.Textbox(label="Text Input", placeholder="Type here...", lines=3)
gr.Number(label="Numeric Value", value=10)
gr.Slider(minimum=0, maximum=100, step=5, value=50, label="Range Slider")
gr.Checkbox(label="Enable Feature", value=True)
gr.Radio(choices=["Option A", "Option B", "Option C"], label="Select One")
gr.Dropdown(choices=["Python", "JavaScript", "C++"], label="Programming Language")
# Multi-Modal Inputs
gr.Image(label="Upload Image or Webcam", type="numpy")
gr.Audio(label="Record or Upload Audio", source="microphone", type="filepath")
gr.Video(label="Upload Video File")
gr.File(label="Upload Any Document (CSV/PDF)")
gr.Dataframe(label="Tabular Data Input", headers=["Col 1", "Col 2"])
Renders interactive web form elements to capture user data types.
3. Output Components Syntax
UI widgets designed to display processed results returned by Python functions.
Python Syntax Reference
# Text & Formatted Outputs
gr.Textbox(label="Text Result")
gr.Markdown("### Rendered Markdown Output")
gr.JSON(label="JSON Data Structure")
gr.HTML(label="Raw HTML Render")
# Visual & Media Outputs
gr.Image(label="Processed Image Output")
gr.Audio(label="Generated Audio Output")
gr.Video(label="Processed Video Output")
gr.File(label="Downloadable File Output")
# Data & Charts
gr.Dataframe(label="Tabular Data Grid")
gr.Plot(label="Matplotlib / Plotly Chart Output")
gr.Chatbot(label="Chat History Window")
Renders formatted text, media previews, data tables, and dynamic charts on the web interface.
4. Layout & Structural Blocks Syntax (`gr.Blocks`)
Syntax elements used to organize components into rows, columns, tabs, and collapsible accordions.
Python Syntax Reference
with gr.Blocks() as demo:
# Group components horizontally
with gr.Row():
comp1 = gr.Textbox()
comp2 = gr.Textbox()
# Group components vertically
with gr.Column():
comp3 = gr.Button("Click")
comp4 = gr.Label()
# Organize content in tabs
with gr.Tabs():
with gr.Tab("Tab 1"):
gr.Markdown("Content for Tab 1")
with gr.Tab("Tab 2"):
gr.Markdown("Content for Tab 2")
# Collapsible section
with gr.Accordion("Advanced Settings", open=False):
slider = gr.Slider(0, 10)
Enables advanced UI dashboard structuring, responsive grid alignments, and clean navigation views.
5. Event Handling & Interactivity Syntax
Methods used to bind user actions (clicks, value changes, file uploads) to Python functions.
Python Syntax Reference
# 1. Button Click Event
submit_btn.click(fn=process_data, inputs=input_box, outputs=output_box)
# 2. Real-time Value Change Event
slider_input.change(fn=update_plot, inputs=slider_input, outputs=plot_output)
# 3. Enter Key Press Event in Textbox
text_input.submit(fn=generate_text, inputs=text_input, outputs=text_output)
# 4. Multi-Input / Multi-Output Event Binding
btn.click(
fn=complex_ai_model,
inputs=[image_input, slider_input, text_input],
outputs=[image_output, json_output]
)
Connects frontend event triggers to backend Python execution handlers dynamically.