Sub topic 1
What Gradio is and how to launch an app
Gradio is a Python library that turns a Python function into a web app. You write the function, tell Gradio what kinds of input and output it takes, and Gradio builds a browser interface and a web server around it. This chapter covers what Gradio is made of, how to install and launch an app, and what launch() does. It also covers the settings you will change most often and the errors you are most likely to hit.
Prerequisites
This chapter assumes you can do the following:
- Write and run a Python script from a terminal, including functions and
if __name__ == "__main__":. - Use
pipand a virtual environment to install packages. - Describe what a web app on a local port is. For example, you know that
http://127.0.0.1:7860means "this computer, port 7860". - Read a simple Python f-string and call a function with keyword arguments.
You do not need any HTML, CSS, or JavaScript. You do not need an LLM API key yet. Later chapters add that.
What Gradio is
Gradio sits between your Python code and a browser. Your code stays ordinary Python. Gradio supplies three things around it:
- Components. Reusable interface pieces such as
gr.Textbox,gr.Slider,gr.Image, andgr.Chatbot. Each one converts between a browser value and a Python value. - A server. A web server that runs locally, serves the page, and receives events from the browser. Gradio starts it for you.
- Event wiring. A rule that says "when this happens in the page, call this Python function with these inputs and put the result in these outputs".
Gradio offers three levels of API. You choose one based on how much control you need.
| Level | Class | What you describe | Typical use |
|---|---|---|---|
| High | `gr.Interface` | A function, its inputs, and its outputs | Demos of a single function |
| High, chat-specific | `gr.ChatInterface` | A function that takes `message` and `history` | Chat apps (the next chapters) |
| Low | `gr.Blocks` | Layout, components, and events, one by one | Custom layouts and multi-step flows |
The high-level classes are built on gr.Blocks. A gr.Interface or gr.ChatInterface is a prebuilt Blocks app. That means everything you learn about launching applies to all three.
Install Gradio
Create a virtual environment and install the package.
python -m venv .venv
source .venv/bin/activate
pip install --upgrade gradio
python -c "import gradio as gr; print(gr.__version__)"On Windows, activate the environment with .venv\Scripts\activate. Gradio 5 needs a recent Python (3.10 or newer). Print the version, because chat-related options changed between releases. The examples in this tutorial target Gradio 5.x, and where newer versions behave differently the text says so.
Your first launched app
The smallest useful app takes one string and returns one string. Save this as app.py.
import gradio as gr
def greet(name):
return f"Hello, {name}!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
if __name__ == "__main__":
demo.launch()Run it:
python app.pyThe terminal prints a line like * Running on local URL: http://127.0.0.1:7860. Open that address in a browser. You will see a textbox, a Submit button, and an output box. Type a name and submit. The browser sends the text to your server, your function runs, and the returned string appears in the output box.
Three parts of this file matter in every Gradio app:
- The function (
greet). It does the real work. Gradio passes it the input values and shows what it returns. - The app object (
demo). Gradio'sInterface,ChatInterface, orBlocksobject. The namedemois a convention, and one tool below depends on it. launch(). The call that starts the server.
The if __name__ == "__main__": guard keeps the server from starting when another file imports your module. This matters for testing, and for tools that import demo and run it themselves.
What launch() does
demo.launch() does more than open a page. In order, it:
- Builds the app's configuration: the list of components, their layout, and the events that connect them.
- Starts a web server (FastAPI running on uvicorn) in a background thread.
- Picks a port. It starts at 7860 and, if that port is busy, tries the next ones.
- Prints the local URL, and optionally opens a browser tab or creates a public link.
- In a script, blocks the main thread so the process stays alive. In a notebook, it returns and shows the app inline.
Every browser click then becomes an HTTP request to that server. Gradio queues the request and calls your function. It then sends the result back to the page. The same server also exposes an API for each event, which is what lets another program call your app later in the tutorial.
Because the diagram fence needs the required form, here is the flow in the proper diagram block.
Launch options you will use
launch() takes many arguments. These are the ones that matter early.
| Argument | Default | Effect |
|---|---|---|
| `server_name` | `"127.0.0.1"` | Network interface to bind. Use `"0.0.0.0"` to accept connections from other machines. |
| `server_port` | `7860` | Port to listen on. If you set it explicitly and it is busy, launch fails instead of choosing another port. |
| `share` | `False` | Creates a temporary public `gradio.live` link that tunnels to your machine. |
| `inbrowser` | `False` | Opens the app in your default browser when it starts. |
| `auth` | `None` | A `(username, password)` pair that protects the app. |
| `show_error` | `False` | Shows Python exceptions in the browser instead of a generic error. |
Here is the same app with a few of them set:
import gradio as gr
def greet(name):
return f"Hello, {name}!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
if __name__ == "__main__":
demo.launch(
server_name="127.0.0.1",
server_port=7861,
inbrowser=True,
)You can also set the host and port without changing code. Gradio reads the environment variables GRADIO_SERVER_NAME and GRADIO_SERVER_PORT. This is useful in containers, where the platform decides the port:
GRADIO_SERVER_PORT=8080 GRADIO_SERVER_NAME=0.0.0.0 python app.pyReload mode for development
Restarting the script after every edit is slow. Gradio includes a reload mode that restarts the app when the file changes. Run the file with the gradio command instead of python:
gradio app.pyReload mode imports your file and looks for a variable named demo. If your app object has another name, pass it with --demo-name:
gradio app.py --demo-name my_appReload mode does not run code inside the if __name__ == "__main__": block, because it imports the file instead of running it as a script. That is why the guard is safe: the app object is defined at module level, and launch() stays inside the guard. Put your app definition outside the guard and only launch() inside it.
Common errors
OSError: Cannot find empty port in range: 7860-7860. You set server_port and something else already holds that port, often an earlier run of the same script. Stop the old process or pick another port. If you leave server_port unset, Gradio picks a free one on its own.
The page cannot be reached from another computer. The default server_name is 127.0.0.1, which only accepts connections from the same machine. Set it to "0.0.0.0" if you want remote access, and read the warning above first.
Nothing happens in reload mode, or it says it cannot find demo. Your app object has a different name, or it is created inside a function or inside the __main__ guard. Define it at module level and use --demo-name if the name differs.
The script exits immediately. In a normal script, launch() blocks. If your process ends right away, an exception stopped it before launch. Read the traceback above the URL line. In a notebook, launch() returns right away by design, and the app stays alive while the kernel runs.
Browser errors that say "Error" with no detail. By default Gradio hides Python tracebacks from the page. Read the terminal output, or set show_error=True while debugging.
Where this fits in the chat app
The chat apps in this tutorial use the same pieces you just saw. You will write a function, wrap it in gr.ChatInterface, and call launch(). The only change is that the function takes two arguments, message and history, and the interface keeps the conversation on screen for you. The next chapter defines that function contract exactly.
Exercise
You run python app.py, and the app starts on port 7860. You then start a second copy of the same script from another terminal without changing any code. The second copy prints a URL with port 7861 and works. Later you add server_port=7860 to launch() and repeat the experiment. What happens to the second copy, and why?
Show answer
With server_port=7860 set explicitly, the second copy fails with an error that it cannot find an empty port, because port 7860 is already in use by the first copy. When no port is given, Gradio searches upward from 7860 and takes the first free one, which is why the earlier second copy used 7861. An explicit port tells Gradio to use that port only.
Exercise
You wrote app.py with demo = gr.Interface(...) inside the if __name__ == "__main__": block, followed by demo.launch(). python app.py works, but gradio app.py reports that it cannot find demo. What do you change?
Show answer
Move the demo = gr.Interface(...) line to module level, above the guard, and keep only demo.launch() inside the guard. Reload mode imports the file instead of running it as a script, so the guarded block never runs and demo is never defined.