gr.HTML component lets you create custom UI elements with HTML, CSS, and JavaScript. You can build everything from simple styled displays to interactive custom components.
Basic HTML
At its simplest, you can display custom HTML:HTML templates
For dynamic content, use thehtml_template parameter. Templates support two syntaxes:
${}: Custom JavaScript expressions{{}}: Handlebars templating for loops and conditionals
Templating with lists
Use Handlebars loops to iterate over arrays:Adding CSS styles
Use thecss_template parameter to style your HTML:
By default,
gr.HTML applies some CSS to match the Gradio theme. Disable this with apply_default_css=False if you want full control over styling.Dynamic updates with templates
Templates automatically update when you change the value:Additional props
You can pass extra properties beyondvalue to your templates:
html_template and css_template, and any prop can be updated via event listeners.
Triggering events and custom inputs
To create interactive custom components, use thejs_on_load parameter. This JavaScript code runs when the component loads and has access to:
props: All component properties includingvaluetrigger(event_name, data): Function to trigger events that Gradio can listen to
Important: Event listeners attached in
js_on_load are only attached once when the component first renders. If you create new elements dynamically that need listeners, use event delegation:Uploading files
Thejs_on_load scope includes an upload() function that uploads JavaScript File objects to the Gradio server:
upload() function returns a dictionary with:
path: Server-side file pathurl: Public URL to access the file
Server functions
You can call Python functions directly from yourjs_on_load code using server_functions:
Server functions are available as async methods on the
server object inside js_on_load. They can accept any JSON-serializable arguments and return JSON-serializable values.Creating reusable component classes
For components you’ll use multiple times, create a class by subclassinggr.HTML:
Embedding components in HTML
You can embed other Gradio components inside your HTML using the@children placeholder:
The
@children placeholder must be at the top level of the html_template. Target the parent element directly with CSS or JavaScript to style or interact with the container.API and MCP support
To make your custom HTML component work with Gradio’s API and MCP (Model Context Protocol), define how its data should be serialized.Option 1: Define api_info method
Option 2: Define a Pydantic data model
For complex data structures, use a Pydantic model:GradioModel for dictionary-like data with named fields, or GradioRootModel for simple types (strings, lists, etc.) that don’t need wrapping.
Security considerations
Next steps
Explore example custom components:- Custom HTML component examples on GitHub
- Learn about custom CSS and JavaScript for app-wide styling
- Build dynamic apps that create HTML components on the fly