Skip to main content
The 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 the html_template parameter. Templates support two syntaxes:
  • ${}: Custom JavaScript expressions
  • {{}}: Handlebars templating for loops and conditionals
This renders as:

Templating with lists

Use Handlebars loops to iterate over arrays:

Adding CSS styles

Use the css_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 beyond value to your templates:
All props are available in both 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 the js_on_load parameter. This JavaScript code runs when the component loads and has access to:
  • props: All component properties including value
  • trigger(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

The js_on_load scope includes an upload() function that uploads JavaScript File objects to the Gradio server:
The upload() function returns a dictionary with:
  • path: Server-side file path
  • url: Public URL to access the file

Server functions

You can call Python functions directly from your js_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 subclassing gr.HTML:
Important: Gradio requires all components to accept certain arguments (like render, key, elem_id, etc.). Always add **kwargs to your __init__ method and pass it to super().__init__() to ensure your component works correctly.

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:
Use GradioModel for dictionary-like data with named fields, or GradioRootModel for simple types (strings, lists, etc.) that don’t need wrapping.

Security considerations

Important security notes:
  1. XSS vulnerabilities: Using gr.HTML injects raw HTML and JavaScript into your app. Never use untrusted user input directly in html_template or js_on_load.
  2. Input validation: Python event listeners that accept your gr.HTML component as input can receive arbitrary values, not just the values your frontend sets. Always sanitize and validate input in production apps.
  3. Trust your code: Only use gr.HTML with code you trust and control.

Next steps

Explore example custom components: