Layouts for gradio
- User
Layouts for gradio
- Assistant
Layouts for gradio
Gradio is a Python library that lets you build interactive web-based UIs for machine learning models and functions quickly. One important aspect of Gradio interfaces is the layout—how UI components like inputs, outputs, buttons, and text are arranged on the page.
Gradio provides several ways and components to organize layouts:
InterfaceWhen you create a simple gr.Interface, Gradio automatically arranges inputs and outputs either vertically or horizontally depending on the layout parameter:
import gradio as gr
def greet(name):
return "Hello " + name + "!"
# Basic vertical layout (default)
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
# Horizontal layout using layout param
iface = gr.Interface(fn=greet, inputs="text", outputs="text", layout="horizontal")
iface.launch()
layout="vertical" (default) stacks inputs above outputslayout="horizontal" puts inputs and outputs side-by-side.BlocksFor more customized layouts, Gradio provides the Blocks API. Here you can create rows, columns, tabs, and customize the positioning freely.
Example with rows and columns:
import gradio as gr
with gr.Blocks() as demo:
with gr.Row():
name_input = gr.Textbox(label="Name")
age_input = gr.Number(label="Age")
with gr.Row():
greet_btn = gr.Button("Greet")
output = gr.Textbox(label="Output")
def greet(name, age):
return f"Hello {name}, you are {age} years old!"
greet_btn.click(greet, inputs=[name_input, age_input], outputs=[output])
demo.launch()
BlocksExample for tabs:
with gr.Blocks() as demo:
with gr.Tabs():
with gr.TabItem("Tab 1"):
gr.Textbox(label="In Tab 1")
with gr.TabItem("Tab 2"):
gr.Button("Button in Tab 2")
demo.launch()
with gr.Blocks() as demo:
inp = gr.Textbox(label="Your name")
btn = gr.Button("Submit")
out = gr.Textbox(label="Greeting")
btn.click(lambda x: f"Hello {x}!", inputs=inp, outputs=out)
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
inp = gr.Textbox(label="Input")
btn = gr.Button("Run")
with gr.Column():
out = gr.Textbox(label="Output")
btn.click(lambda x: f"Output is {x}", inputs=inp, outputs=out)
with gr.Blocks() as demo:
with gr.Accordion("More options"):
opt = gr.Checkbox(label="Enable feature X")
Rows and Columns to create grids.gr.Box to visually group components.label to improve user experience.scale parameter inside Row or Column to control relative widths.If you want, I can help you create a specific layout—just describe your UI needs!
Not yet included in a published release.