Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:AUTOMATIC1111 Stable diffusion webui Webui Entry Point

From Leeroopedia


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:

  1. Optionally cleans the temp directory.
  2. Fires before_ui_callback script callbacks.
  3. Creates the Gradio UI via ui.create_ui().
  4. Configures Gradio queue (default concurrency 64).
  5. Launches the Gradio server with SSL, auth, auto-launch browser, and CORS security settings.
  6. Removes the default overly-permissive CORS middleware as a security measure (suggested by RyotaK).
  7. Sets up middleware, progress API, UI API, optional REST API, and extra networks pages.
  8. Fires app_started_callback and records startup timing.
  9. Enters a command wait loop, polling shared.state.wait_for_server_command() every 5 seconds.
  10. 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

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

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment