Sub topic 1
What Gradio Is and How to Launch an App
Prerequisites
This chapter assumes you can do the following:
- Write Python functions and read simple generators.
- Use
pipand run a script from a terminal. - Describe what a chat or LLM API does: you send messages and receive a reply.
- Understand that a web app can listen on a local port such as 7860.
You do not need any web development experience. You do not need an LLM account yet.
What Gradio is
Gradio is a Python library that turns a function into a web interface. You write a normal Python function. Gradio starts a local web server, builds the page, and calls your function when someone uses it. You write no HTML, CSS, or JavaScript.
For this tutorial, the useful part is that Gradio ships a chat component. Later chapters put that component in front of a Python function and then in front of an LLM.
Gradio offers three levels of control:
| Level | Class | Use it when |
|---|---|---|
| High-level | `gr.Interface`, `gr.ChatInterface` | One function maps to one screen |
| Layout | `gr.Blocks` | You need custom layout and event wiring |
| Client | `gradio_client` | Another program calls the app |
This tutorial starts with gr.ChatInterface and moves to gr.Blocks later.
Install and check the version
Install Gradio into a virtual environment:
python -m venv .venv
source .venv/bin/activate
pip install --upgrade gradio
python -c "import gradio as gr; print(gr.__version__)"The examples in this tutorial pass type="messages" to chat components. That setting applies to Gradio 5.x. Newer versions may use this format by default.
Launch your first app
Save this as app.py:
import gradio as gr
def respond(message, history):
return f"You said: {message}"
demo = gr.ChatInterface(fn=respond, type="messages")
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. Type a message and the app replies with You said: ....
What happens at launch
demo is only an object that describes the app. Nothing is served until you call demo.launch(). That call starts a web server in a background thread and keeps the script alive so the server keeps running.
The diagram must use the required fence form, so here it is in full:
The if __name__ == "__main__": guard means the server starts only when you run the file directly. It does not start when another file imports demo. Keep it, because later chapters import and deploy these apps.
Common launch problems
- Port already in use. Gradio starts at port 7860 and tries the next free port. To pick one yourself, call
demo.launch(server_port=8000). - Code changes do not appear. The running process keeps the old code. Stop it with
Ctrl+Cand run it again, or usegradio app.pyto start it in reload mode. - Page not reachable from another machine. By default the server listens on
127.0.0.1, which only your own computer can reach. A later chapter coversserver_nameand sharing.
Exercise
You run python app.py and see "Running on local URL: http://127.0.0.1:7860". Why can a colleague on another computer not open that address, and which launch argument changes this?
Show answer
127.0.0.1 is the loopback address, so the server only accepts connections from the same machine. Passing server_name="0.0.0.0" to launch() makes it listen on all network interfaces.