Gradio provides a powerful framework for building interactive applications and developers can quickly prototype and deploy chatbots without extensive knowledge of web development or user interface design.
Key takeaways:
Row()
,Column()
,Tab()
, andGroup()
are fundamental to designing user interfaces in Gradio by arranging components efficiently.
Row()
arranges components horizontally within a layout.
Column()
stacks components vertically, with control over width and style.
Tab()
creates tabbed interfaces for organizing content into separate, selectable panels.
Group()
groups multiple elements together without padding or margin, useful for keeping related components closely grouped.
To build effective and visually appealing user interfaces (UI), it's crucial to understand how to organize components efficiently. In UI design, components like buttons, input fields, and other elements need to be aligned properly to ensure a smooth user experience. This is where tools like Gradio come in, offering simple yet powerful ways to structure your layout using elements such as Row()
, Column()
, Tab()
, and Group()
.
Gradio simplifies the process of creating interactive UIs, especially when working with machine-learning models. Its layout elements allow for seamless integration of various UI elements, making it easier to design and deploy interactive applications.
Row()
: Used to arrange UI elements horizontally.
Column()
: Stacks UI elements vertically.
Tab()
: Allows the creation of tabbed layouts for organizing content.
Group()
: Groups elements together in a logical and organized way.
Row()
layout element of GradioThe Row()
layout in Gradio is used within Blocks to arrange elements horizontally. This layout makes it easy to display multiple elements in a single row. The following syntax is used to define Row()
:
gr.Row(variant='default', visible=True, elem_id=None, elem_classes=None, render=True, height=None, max_height=None, min_height=None, equal_height=False, show_progress=False)
variant
(Literal['default', 'panel', 'compact']
): Defines the style of the row. Default is 'default'
.
visible
(bool
): If set to False
, the row will not be visible. The default value is True
.
elem_id
(str | None
): An optional ID for the row element in the HTML DOM for targeting with CSS.
elem_classes
(list[str] | str | None
): CSS class or list of classes for targeting the row.
render
(bool
): If False
, the row is not rendered. Default is True
.
height
(int | str | None
): Sets the height of the row in pixels or CSS units.
max_height
(int | str | None
): Sets the maximum height for the row.
min_height
(int | str | None
): Sets the minimum height for the row.
equal_height
(bool
): If True
, all child elements inside the row will have equal height.
show_progress
(bool
): If True
, shows progress animation when the row is updated.
Here is the code example of using the Row()
layout:
import gradio as gr with gr.Blocks() as demo: with gr.Row(): gr.Button("Option 1") gr.Button("Option 2") gr.Button("Option 3") demo.launch(server_name="0.0.0.0", server_port=7860)
Column()
layout element of GradioThe Column()
layout in Gradio is used within Blocks to stack components vertically. It organizes elements in a top-to-bottom fashion.
gr.Column(scale=1, min_width=320, variant='default', visible=True, elem_id=None, elem_classes=None, render=True, show_progress=False)
scale
(int
): Defines the relative width of the column compared to adjacent columns. Default is 1
. For example, a column with scale=2 will be twice as wide as a column with scale=1.
min_width
(int
): Specifies the minimum width of the column in pixels. Default is 320
. If the column’s scale would make it narrower than this value, the column will not shrink below the specified width.
variant
(Literal['default', 'panel', 'compact']
): Defines the style of the column. The options are 'default'
(no background), 'panel'
(gray background with rounded corners), or 'compact'
(rounded corners, no internal gap).
visible
(bool
): If set to False
, the column will be hidden. Default is True
.
elem_id
(str | None
): An optional string for the HTML DOM element ID, which can be used to target CSS styles.
elem_classes
(list[str] | str | None
): A string or list of strings for the CSS classes of the column, useful for targeting specific styles.
render
(bool
): If set to False
, the column will not be rendered. Default is True
.
show_progress
(bool
): If set to True
, displays a progress animation when the column is updated. Default is False
.
Here is the code example to use Column()
layout:
import gradio as gr with gr.Blocks() as demo: with gr.Column(): gr.Button("Item 1") gr.Button("Item 2") gr.Button("Item 3") demo.launch(server_name="0.0.0.0", server_port=7860)
Tab()
layout element of GradioThe Tab()
layout in Gradio is used within Blocks to create a tabbed interface. This allows users to switch between different sections or panels in the layout.
gr.Tab(label=None, visible=True, interactive=True, id=None, elem_id=None, elem_classes=None, render=True)
label
(str | None
): The visual label for the tab. Default is None
.
visible
(bool
): If set to False
, the tab will be hidden. Default is True
.
interactive
(bool
): If set to False
, the tab will not be clickable. Default is True
.
id
(int | str | None
): An optional identifier for the tab, useful for controlling the selected tab programmatically. Default is None
.
elem_id
(str | None
): An optional string for the HTML DOM element ID, which can be used to target CSS styles. Default is None
.
elem_classes
(list[str] | str | None
): An optional string or list of CSS classes for styling. Default is None
.
render
(bool
): If set to False
, the tab won’t render. Default is True
.
Here is the code example to use the Tab()
layout:
import gradio as gr with gr.Blocks() as demo: with gr.Tabs(): with gr.Tab("Tab 1"): gr.Button("Option 1 in Tab 1") with gr.Tab("Tab 2"): gr.Button("Option 2 in Tab 2") demo.launch(server_name="0.0.0.0", server_port=7860)
Group()
layout element of GradioThe Group()
layout in Gradio is used within Blocks to logically group components together. It’s useful for organizing multiple layout components as one unit without displaying them inline.
gr.Group(visible=True, elem_id=None, elem_classes=None, render=True)
visible
(bool
): If set to False
, the group will be hidden. Default is True
.
elem_id
(str | None
): An optional string for the HTML DOM element ID, useful for targeting CSS styles. Default is None
.
elem_classes
(list[str] | str | None
): An optional string or list of CSS classes. Default is None
.
render
(bool
): If set to False
, the group won’t render. Default is True
.
Here is the code example to use Group()
layout:
import gradio as gr with gr.Blocks() as demo: with gr.Group(): gr.Textbox(label="Enter your message") gr.Button("Submit") demo.launch(server_name="0.0.0.0", server_port=7860)
Unlock the potential of Python and advanced technologies in the “Guide to Building Python and LLM-Based Multimodal Chatbots” course. Learn to integrate Gradio, Rasa, Gemini, and Whisper v3 to create multimodal, interactive AI chatbots and deploy them on Hugging Face.
In conclusion, Gradio’s layout elements offer powerful and flexible ways to structure UIs in a simple, intuitive manner. Understanding how to use these elements can significantly enhance the user experience, whether we’re building a machine-learning interface or any interactive web application. These elements help organize content effectively, improving both functionality and aesthetic appeal.
Haven’t found what you were looking for? Contact Us
Free Resources