Chapter-3
By now, you’ve learned how to build basic Gradio apps and work with various input/output types. But raw functionality is only half the story. Great design makes your AI demos easier to use, more professional, and enjoyable to interact with.
In this part, we’ll explore:
Gradio’s layout systems: Interface
vs Blocks
Gradio Interface
— Quick and Easy UI
The gr.Interface
class is perfect when you just want to:
-
Wrap a single function
-
Provide a few inputs and outputs
-
Launch a simple demo fast
What happens here?
-
A textbox appears for the user to type their name
-
When they click "Submit", the function
greet
is called -
The output (greeting message) is displayed below
Limitation of Interface
:
You cannot:
-
Add multiple pages or tabs
-
Nest layouts like side-by-side or columns
-
Add multiple functions reacting to different inputs
-
Dynamically change the UI
Gradio Blocks
— Full Layout and Logic Control
The gr.Blocks
API is much more powerful. Use it when:
-
You need multiple sections, pages, or workflows
-
You want inputs/outputs to appear in custom order
-
You want to chain functions or update outputs dynamically
What’s Happening Here?
-
You control layout using
Row()
andMarkdown()
-
The greeting appears in a custom location
-
You can use change events (
.change
) for live updates -
Much easier to scale later by adding tabs, sliders, or charts
Feature | Interface |
Blocks |
---|---|---|
Simplicity | ✅ Easier, quick setup | ❌ Slightly more code |
Flexibility | ❌ Limited layout or interactivity | ✅ Highly customizable |
UI Control | ❌ Pre-built layout only | ✅ Full control (rows, tabs, themes) |
Use Case | Demos, tutorials, simple forms | Complex apps, dashboards, workflows |
Use Case | Use Interface? | Use Blocks? |
---|---|---|
A quick ML demo (e.g., image classifier) | ✅ Yes | ❌ Not needed |
Build a full app with multiple steps | ❌ No | ✅ Yes |
Use custom layouts (rows/columns) | ❌ No | ✅ Yes |
Dynamic updates on events | ❌ No | ✅ Yes |
Add tabs or multi-page layout | ❌ No | ✅ Yes |
Use
Interface
when you're starting out or building a fast proof of concept.Use
Blocks
when you want control, custom layout, interactivity, or multiple functions.
Comments
Post a Comment