gr.Blocks, gr.Interface, API usage, themes, etc. with custom components. This guide will cover how to get started making custom components.
Installation
You will need to have:- Python 3.10+ (install here)
- pip 21.3+ (
python -m pip install --upgrade pip) - Node.js 20+ (install here)
- npm 9+ (install here)
- Gradio 5+ (
pip install --upgrade gradio)
The workflow
The custom components workflow consists of 4 steps: create, dev, build, and publish.Dev
Launches a development server with a sample app and hot reloading allowing you to easily develop your custom component.
Publish
Uploads your package to PyPI and/or a sample app to HuggingFace Spaces.
gradio cc or gradio component.
Step 1: Create
Bootstrap a new template by running the following in any working directory:MyComponent, give your component any name.
Instead of SimpleTextbox, you can use any Gradio component as a template. SimpleTextbox is actually a special component that is a stripped-down version of the Textbox component that makes it particularly useful when creating your first custom component. Some other components that are good if you are starting out: SimpleDropdown, SimpleImage, or File.
What gets created
Thecreate command will:
- Create a directory with your component’s name in lowercase with the following structure:
- Install the component in development mode
Step 2: Dev
Once you have created your new component, you can start a development server by entering the directory and running:Step 3: Build
Once you are satisfied with your custom component’s implementation, you can build it to use it outside of the development server. From your component directory, run:tar.gz and .whl file in a dist/ subdirectory. If you or anyone installs that .whl file (pip install <path-to-whl>) they will be able to use your custom component in any Gradio app!
The build command will also generate documentation for your custom component. This takes the form of an interactive space and a static README.md. You can disable this by passing --no-generate-docs.
Step 4: Publish
Right now, your package is only available on a.whl file on your computer. You can share that file with the world with the publish command!
Simply run the following command from your component directory:
- Upload your distribution files to PyPI. This makes it easier to upload the demo to Hugging Face spaces. Otherwise your package must be at a publicly available URL. If you decide to upload to PyPI, you will need a PyPI username and password. You can get one here.
- Upload a demo of your component to Hugging Face Spaces. This is also optional.
Next steps
Now that you know the high-level workflow of creating custom components, you can go in depth in the following guides:- Key concepts - Learn the important concepts behind Gradio components
- Backend development - Learn how to implement your component’s Python backend
- Frontend development - Learn how to build your component’s user interface
- Configuration - Learn how to configure your custom component package