Implementation:AUTOMATIC1111 Stable diffusion webui Shared Options
| Knowledge Sources | |
|---|---|
| Domains | Configuration, Options, Settings |
| Last Updated | 2025-05-15 00:00 GMT |
Overview
The shared options module defines the complete set of application configuration options as categorized OptionInfo entries organized into sections, serving as the authoritative schema for the Settings UI, config file persistence, and runtime option access.
Description
This module builds the options_templates dictionary by repeatedly calling options_section(), each call grouping related OptionInfo entries under a section tuple of (id, label, category). The sections cover:
- Saving images/grids: File format, filename patterns, quality settings, grid layout, notification sounds
- Paths for saving: Output directories for txt2img, img2img, extras, grids, and saved images
- Saving to a directory: Subdirectory patterns and prompt word limits
- Upscaling: ESRGAN and DAT tile sizes, model selections, img2img upscaler
- Face restoration: CodeFormer/GFPGAN model selection and weight
- System: Browser auto-launch, console output, memory monitoring, profiler, API settings
- Training: Optimizer state, dataset settings, tensorboard integration
- Stable Diffusion: Checkpoint, VAE, CLIP skip, emphasis mode, sampler seeds, tiling, hires fix refiner
- SDXL/SD3: Crop coordinates, aesthetic scores, T5 encoder toggle
- img2img: Inpainting mask strength, noise multiplier, color correction, editor settings
- Optimizations: Cross attention, token merging, FP8 storage, cond caching
- UI: Localization, quicksettings, tab order, themes, gallery settings, live previews
- Sampler parameters: Eta, sigma, UniPC settings, noise schedules
- Postprocessing: Operation order, caching, caption handling
- Extra Networks: Card display, tree view, hidden models, multiplier defaults
Each OptionInfo specifies a default value, label, optional Gradio component type and args, refresh callback, infotext key, and metadata methods like .needs_reload_ui() and .info(). The module also defines restricted_opts (settings that should not be remotely modifiable for security) and registers option categories via categories.register_category().
Usage
This module is loaded at startup to populate the options system. It is used by ui_settings.py to dynamically generate the Settings tab UI, by shared.py to provide runtime access to option values via shared.opts, and by the config file system to persist and load settings. Extension authors can add their own options by calling shared.opts.add_option() or using options_section() in callbacks.
Code Reference
Source Location
- Repository: AUTOMATIC1111_Stable_diffusion_webui
- File: modules/shared_options.py
- Lines: 1-425
Signature
options_templates = {}
restricted_opts = {"samples_filename_pattern", "directories_filename_pattern", ...}
categories.register_category("saving", "Saving images")
categories.register_category("sd", "Stable Diffusion")
categories.register_category("ui", "User Interface")
categories.register_category("system", "System")
categories.register_category("postprocessing", "Postprocessing")
categories.register_category("training", "Training")
options_templates.update(options_section((section_id, section_label, category), {
"option_key": OptionInfo(default, label, component, component_args, ...),
}))
Import
from modules.shared_options import options_templates, restricted_opts
from modules.options import options_section, OptionInfo, OptionHTML, categories
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| default | any | Yes | Default value for the option (determines type: str, int, float, bool, list) |
| label | str | Yes | Human-readable label displayed in the Settings UI |
| component | gr.Component | No | Gradio component class to use (gr.Slider, gr.Dropdown, gr.Radio, etc.) |
| component_args | dict or callable | No | Arguments passed to the Gradio component constructor |
| refresh | callable | No | Function to call when the refresh button is clicked (e.g. refresh model list) |
| infotext | str | No | Key name used when parsing/writing generation infotext parameters |
Outputs
| Name | Type | Description |
|---|---|---|
| options_templates | dict | Complete dictionary of all option sections and their OptionInfo entries |
| restricted_opts | set | Set of option keys that should not be modifiable via remote/API access |
Usage Examples
# Accessing an option value at runtime
from modules.shared import opts
save_format = opts.samples_format # Returns 'png' by default
clip_skip = opts.CLIP_stop_at_last_layers # Returns 1 by default
# Adding a custom option from an extension
from modules.options import options_section, OptionInfo
import gradio as gr
options_templates.update(options_section(('my_extension', "My Extension", "system"), {
"my_custom_setting": OptionInfo(True, "Enable my feature"),
"my_custom_strength": OptionInfo(0.5, "Feature strength", gr.Slider, {"minimum": 0, "maximum": 1, "step": 0.1}),
}))