The directory structure
The frontend code should have, at minimum, two files:Index.svelte: This is the main export and where your component’s layout and logic should live.Example.svelte: This is where the example view of the component is defined.
package.json file:
The Index.svelte file
Your component should expose the following props that will be passed down from the parent Gradio application.Props explained
elem_id and elem_classes
elem_id and elem_classes
Allow Gradio app developers to target your component with custom CSS and JavaScript from the Python
Blocks class.scale and min_width
scale and min_width
Allow Gradio app developers to control how much space your component takes up in the UI.
loading_status
loading_status
Used to display a loading status over the component when it is the output of an event.
mode
mode
How the parent Gradio app tells your component whether the
interactive or static version should be displayed.gradio
gradio
The
gradio object is created by the parent Gradio app. It stores some application-level configuration that will be useful in your component, like internationalization. You must use it to dispatch events from your component.Minimal example
A minimalIndex.svelte file would look like:
The Example.svelte file
TheExample.svelte file should expose the following props:
Props explained
value
value
The example value that should be displayed.
type
type
This is a variable that can be either
"gallery" or "table" depending on how the examples are displayed. The "gallery" form is used when the examples correspond to a single input component, while the "table" form is used when a user has multiple input components, and the examples need to populate all of them.selected
selected
You can also adjust how the examples are displayed if a user “selects” a particular example by using the
selected variable.index
index
The current index of the selected value.
Example from Radio component
This is theExample.svelte file for the core Radio component:
Handling files
If your component deals with files, these files should be uploaded to the backend server. The@gradio/client npm package provides the upload and prepare_files utility functions to help you do this.
- The
prepare_filesfunction will convert the browser’sFiledatatype to Gradio’s internalFileDatatype. - The
uploadfunction will upload an array ofFileDatavalues to the server.
FileData data type in your component to keep track of uploaded files.
Example: Loading files from input
Here’s an example of loading files from an<input> element when its value changes:
root. This is passed down by the parent Gradio app and it represents the base URL that the files will be uploaded to and fetched from.
WASM support
For WASM support, you should get the upload function from theContext and pass that as the third parameter of the upload function:
Leveraging existing Gradio components
Most of Gradio’s frontend components are published on npm, the JavaScript package repository. This means that you can use them to save yourself time while incorporating common patterns in your component, like uploading files. For example, the@gradio/upload package has Upload and ModifyUpload components for properly uploading files to the Gradio server. Here is how you can use them to create a user interface to upload and display PDF files:
Matching Gradio core’s design system
You can explore our component library via Storybook. You’ll be able to interact with our components and see them in their various states. For those interested in design customization, we provide the CSS variables consisting of our color palette, radii, spacing, and the icons we use - so you can easily match up your custom component with the style of our core components. This Storybook will be regularly updated with any new additions or changes. View StorybookCustom configuration
If you want to make use of the vast Vite ecosystem, you can use thegradio.config.js file to configure your component’s build process. This allows you to make use of tools like TailwindCSS, mdsvex, and more.
Currently, it is possible to configure the following:
Vite options:
plugins: A list of Vite plugins to use.
preprocess: A list of Svelte preprocessors to use.extensions: A list of file extensions to compile to.sveltefiles.build.target: The target to build for (see esbuild docs).
gradio.config.js file should be placed in the root of your component’s frontend directory.
Example: Vite plugin (TailwindCSS)
Custom components can use Vite plugins to customize the build process. Here we configure TailwindCSS, a utility-first CSS framework. Setup is easiest using the version 4 prerelease:gradio.config.js:
style.css file with the following content:
Index.svelte. Note that you need to import the CSS file containing @import and cannot just use a <style> tag:
Example: Svelte options (mdsvex)
Ingradio.config.js you can also specify some Svelte options to apply to the Svelte compilation. In this example we will add support for mdsvex, a Markdown preprocessor for Svelte.
First, install the mdsvex plugin:
gradio.config.js:
mdsvex documents in our component’s frontend directory and they will be compiled to .svelte files:
HelloWorld.svx file in our components: