Implementation:AUTOMATIC1111 Stable diffusion webui Webui Entry Point
| Knowledge Sources | |
|---|---|
| Domains | Application Entry Point, Server Lifecycle |
| Last Updated | 2025-05-15 00:00 GMT |
Overview
Serves as the main entry point for the Stable Diffusion web UI application, orchestrating initialization, server launch, API setup, and the restart/stop lifecycle loop.
Description
The webui.py file is the primary entry point invoked to start the application. It begins by recording launcher timing, running imports and version checks through the initialize module, and then dispatches to either api_only() or webui() mode based on the --nowebui flag.
api_only() mode: Creates a bare FastAPI application without the Gradio UI. It initializes the application, sets up middleware, creates the API via create_api(), fires before_ui_callback and app_started_callback script callbacks, and launches the API server on the configured host/port.
webui() mode: Runs the full web UI in a restart-capable loop. Each iteration:
- Optionally cleans the temp directory.
- Fires
before_ui_callbackscript callbacks. - Creates the Gradio UI via
ui.create_ui(). - Configures Gradio queue (default concurrency 64).
- Launches the Gradio server with SSL, auth, auto-launch browser, and CORS security settings.
- Removes the default overly-permissive CORS middleware as a security measure (suggested by RyotaK).
- Sets up middleware, progress API, UI API, optional REST API, and extra networks pages.
- Fires
app_started_callbackand records startup timing. - Enters a command wait loop, polling
shared.state.wait_for_server_command()every 5 seconds. - On "stop", closes Gradio and exits. On "restart", closes Gradio, resets timers, fires reload/unload callbacks, and reinitializes with reloaded script modules before looping.
The create_api(app) helper creates an Api instance with the shared queue lock.
Usage
This file is the application entry point. Run it directly with python webui.py or via the launcher scripts. It should not typically be imported by other modules.
Code Reference
Source Location
- Repository: AUTOMATIC1111_Stable_diffusion_webui
- File: webui.py
- Lines: 1-162
Signature
def create_api(app: FastAPI) -> Api
def api_only() -> None
def webui() -> None
# Entry point
if __name__ == "__main__":
if cmd_opts.nowebui:
api_only()
else:
webui()
Import
# This is an entry point script, not typically imported.
# To run:
# python webui.py
# python webui.py --nowebui # API-only mode
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| cmd_opts.nowebui | bool | No | If True, runs in API-only mode without the Gradio UI. |
| cmd_opts.api | bool | No | If True, enables the REST API alongside the Gradio UI. |
| cmd_opts.port | int | No | Server port; defaults to 7861 for API-only mode. |
| cmd_opts.share | bool | No | Whether to create a public Gradio share link. |
| cmd_opts.tls_keyfile | str | No | Path to TLS key file for HTTPS. |
| cmd_opts.tls_certfile | str | No | Path to TLS certificate file for HTTPS. |
| cmd_opts.gradio_auth | str | No | Gradio authentication credentials. |
| cmd_opts.subpath | str | No | URL subpath for reverse proxy configurations. |
Outputs
| Name | Type | Description |
|---|---|---|
| (server) | None | Launches the HTTP server; blocks until stop or keyboard interrupt. |
| startup_timer.summary() | str | Printed startup timing summary. |
Usage Examples
# Standard launch (from command line)
# python webui.py
# API-only mode
# python webui.py --nowebui --port 7861
# With API enabled alongside UI
# python webui.py --api
# The webui() function loop handles restarts internally:
# - User clicks "Reload UI" in settings
# - shared.state.server_command is set to "restart"
# - Loop closes Gradio, reloads scripts, and relaunches