Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 37 ยท Getting Started

Launch Options, Notebooks and Reload Mode

Master Gradio's launch() options: server name and port, share links, authentication, running in Jupyter or Colab, auto-reload for development, and clean shutdown.

launch() is the single call that turns your app definition into a running server. It has many options, and knowing the important ones saves hours: how to change the port, how to expose the app on your network, how to run inside a notebook, how to reload on every file save, and how to stop cleanly. All gradio code here is read-only; runnable stand-ins at the end show the logic of port finding and configuration.

The options you will actually use

Python
demo.launch(
    server_name="127.0.0.1",   # who may connect
    server_port=7860,          # which port
    share=False,               # temporary public link
    auth=None,                 # simple login
    inbrowser=False,           # open a browser tab automatically
    show_error=True,           # show Python errors in the UI
    max_threads=40,            # worker threads for sync functions
    prevent_thread_lock=False, # return immediately instead of blocking
)
Option What it does When to change it
server_name Interface to bind. 127.0.0.1 means this computer only; 0.0.0.0 means every network interface Share on your LAN, or run inside Docker
server_port First port to try. If it is busy, Gradio tries the next ones unless you set it explicitly Run several apps
share Creates a public *.gradio.live URL that tunnels to your machine, valid for a limited time Quick demo to a colleague
auth A (user, password) tuple, a list of tuples or a function A basic gate
inbrowser Opens the default browser Local development
show_error Show server errors in the UI instead of a generic message Development
prevent_thread_lock Do not block; useful in tests and notebooks Automated tests

The environment variables GRADIO_SERVER_NAME and GRADIO_SERVER_PORT set the first two without touching code. That is how containers and hosting platforms configure the app.

server_name="0.0.0.0" and share=True both make your app reachable by other people. Anything the app can do, including reading files you pass to it, they can do too. Never expose an app that reads private files, runs shell commands or holds secrets without authentication and review.

Basic authentication

Python
demo.launch(auth=[("ada", "pass-1"), ("grace", "pass-2")])

The page now shows a login form before the app. This is simple password protection that is good enough for a prototype. It is not a user database, and passwords in source code are not a good idea: read them from environment variables (os.environ["APP_PASSWORD"]). For real user management put the app behind your own login (see the deployment chapter).

Auto-reload during development

Restarting the server after every edit is tedious. Run your file with the gradio command instead of python:

Terminal
gradio app.py

The command watches the file. When you save, it re-imports the code and refreshes the open browser tab, usually within a second. By default it looks for a variable named demo in the file. If your app has another name, pass it:

Terminal
gradio app.py --demo-name=my_app

Two details: code under if __name__ == "__main__": is not executed in reload mode (the command imports your file), so put demo.launch() there freely, and put anything the app needs at import time at module level. Reload mode also refreshes only the file you passed and its watched directory, so if you edit a helper module imported from elsewhere, restart.

Notebooks (Jupyter, Colab, VS Code)

In a notebook cell, the interface can display inline:

Python
demo.launch()

Inside a notebook, launch() does not block the kernel. It starts the server in a background thread and shows the app below the cell. In Google Colab, a public share link is created automatically because the notebook runs on a remote machine. To close the app and free the port, call demo.close(). If you forget, rerunning the cell may complain about the port or show an older version of the UI; close it first.

Notebook habit: define your app in one cell, launch it in another, and call demo.close() before editing the function.

Mounting inside another web app

When you have an existing FastAPI app you can mount Gradio at a path instead of starting a separate server:

Python
import gradio as gr
from fastapi import FastAPI

app = FastAPI()

@app.get("/health")
def health():
    return {"ok": True}

app = gr.mount_gradio_app(app, demo, path="/demo")

Run it with uvicorn main:app. The page at /demo is the Gradio UI, and /health is your own route. This is the usual route to add logins, databases and custom endpoints around a Gradio app (see the deployment chapter).

Stopping cleanly

  • In a terminal, Ctrl+C stops the server.
  • In a script or test, call demo.close().
  • With prevent_thread_lock=True, the call to launch() returns, and you must close the server yourself or the process may linger.

Stand-in 1: finding a free port

When the default port is busy, Gradio looks for a free one. This stand-in uses only the standard library to show the same idea.

Python Stand-in: pick the first free port

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

If you pass server_port=7860 explicitly and it is busy, real Gradio raises an error instead of choosing another one, so the port you printed in the docs is always the real one.

Stand-in 2: configuration from environment variables

Python Stand-in: launch settings from the environment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The settings you can change from the environment mean the same file works on your laptop and in a container.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which server_name makes the app reachable from other computers on your network?

    0.0.0.0 binds every network interface. 127.0.0.1 and localhost accept only local connections.
  2. 2. What does share=True do?

    It is a short-lived public link served through a tunnel. It does not move your app anywhere.
  3. 3. How do you get auto-reload on file save?

    The gradio command watches the file and reloads the app and the browser tab.
  4. 4. What should you do before re-running a notebook cell that launches an app?

    Closing the previous server avoids port conflicts and stale UIs.

Summary

  • launch() has options for host, port, sharing, auth, errors and threading.
  • 0.0.0.0 and share=True expose your app; protect them.
  • gradio app.py gives auto-reload while you develop.
  • Notebooks run the server in the background; close it with demo.close().
  • gr.mount_gradio_app adds Gradio to an existing FastAPI app.

Next: The Interface Class in Depth