Hot reload mode
When you’re building a Gradio app, particularly with Blocks, you may find it cumbersome to keep re-running your code to test your changes. Hot reload mode solves this by automatically reloading your app whenever you make changes to your code.Basic usage
Instead of running your app withpython app.py, simply use the gradio command:
app.py, Gradio will automatically reload your app. You’ll see output like this:
Watching... line indicates that Gradio is observing the directory where your app file lives. When the file changes, it will automatically rerun the file for you, so you can focus on writing your code and your Gradio demo will refresh automatically.
Custom demo name
Gradio specifically looks for a Gradio Blocks/Interface demo calleddemo in your code. If you’ve named your demo something else, you need to pass the name as the second parameter:
Encoding settings
By default, Gradio uses UTF-8 encoding for scripts. If you’re using a different encoding format (such as cp1252):-
Add an encoding declaration to your Python script:
- Confirm your code editor has identified that encoding format
-
Run with the encoding flag:
Command line arguments
If your application accepts command line arguments, you can pass them in as well:Controlling the reload
By default, reload mode will re-run your entire script for every change you make. However, there are cases where this is not desirable.Preventing code re-execution
For example, loading a machine learning model should probably only happen once to save time. Some Python libraries that use C or Rust extensions (likenumpy and tiktoken) may throw errors when reloaded.
In these situations, you can place code that you do not want to be re-run inside an if gr.NO_RELOAD: code block:
Vibe mode
Vibe mode provides an in-browser chat interface that can be used to write or edit your Gradio app using natural language. To enable this, use the--vibe flag:
Hot reload in Jupyter notebooks
If you use Jupyter Notebooks (or Colab Notebooks), you can use Gradio’s magic command for hot reloading.Setting up the magic command
Load the Gradio extension at the top of your notebook:%%blocks at the top:
- You do not need to call
launch()- Gradio does that for you automatically - Every time you rerun the cell, Gradio will re-render your app on the same port and using the same underlying web server
- You’ll see your changes much faster than if you were rerunning the cell normally
You may have to use
%%blocks --share in Colab to get the demo to appear in the cell.Benefits of hot reload mode
Hot reload mode significantly improves your development workflow by:- Eliminating the need to manually restart your app after every change
- Preserving the app’s network port across reloads
- Allowing you to see changes instantly
- Supporting both Python scripts and Jupyter notebooks
- Working seamlessly with your existing development tools