Implementation:AUTOMATIC1111 Stable diffusion webui UI Loadsave
| Knowledge Sources | |
|---|---|
| Domains | UI State Persistence, Configuration |
| Last Updated | 2025-05-15 00:00 GMT |
Overview
Implements the UiLoadsave class that saves and restores default values for Gradio UI components, enabling persistent UI state across application restarts.
Description
The UiLoadsave class manages a registry of tracked Gradio components and their default values, persisted to a JSON file. On initialization, it loads existing defaults from the specified file. Components are registered via add_component(path, x), which inspects the component type (Slider, Radio, Checkbox, Textbox, Number, Dropdown, InputAccordion, Tabs) and applies saved values to the appropriate fields (value, visible, minimum, maximum, step, selected). Components from custom scripts are namespaced with a customscript/ prefix. Components marked with do_not_save_to_config are excluded.
The add_block(x, path) method recursively walks a Gradio block hierarchy, registering all child components by their labels. The dump_defaults() method writes current defaults to the file (skipping if there was a loading error to avoid overwriting valid data). The iter_changes(current_ui_settings, values) method compares current UI values against stored defaults, yielding tuples of (path, old_value, new_value) for changed entries. The ui_view() method generates an HTML table showing pending changes, and ui_apply() writes those changes to the file. The create_ui() and setup_ui() methods build and wire up the defaults editing interface with View Changes and Apply buttons. Validation logic ensures dropdown values exist in their choices and handles type coercion for Textbox (to str) and Number (to float) fields.
Usage
Use this class to persist UI default values so that users can customize widget defaults (slider ranges, checkbox states, dropdown selections) and have those changes survive application restarts.
Code Reference
Source Location
- Repository: AUTOMATIC1111_Stable_diffusion_webui
- File: modules/ui_loadsave.py
- Lines: 1-238
Signature
def radio_choices(comp) -> list
class UiLoadsave:
def __init__(self, filename: str)
def add_component(self, path: str, x: gr.Component) -> None
def add_block(self, x: gr.Block, path: str = "") -> None
def read_from_file(self) -> dict
def write_to_file(self, current_ui_settings: dict) -> None
def dump_defaults(self) -> None
def iter_changes(self, current_ui_settings: dict, values) -> Iterator[tuple]
def ui_view(self, *values) -> str
def ui_apply(self, *values) -> str
def create_ui(self) -> None
def setup_ui(self) -> None
Import
from modules.ui_loadsave import UiLoadsave
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| filename | str | Yes | Path to the JSON file for storing UI defaults. |
| path | str | Yes | Hierarchical path identifier for a component (e.g., "txt2img/Sampling steps"). |
| x | gr.Component | Yes | The Gradio component to register in add_component or add_block. |
| *values | any | Yes | Current values from all tracked components, passed to ui_view and ui_apply. |
Outputs
| Name | Type | Description |
|---|---|---|
| html_table | str | HTML table showing changed defaults from ui_view. |
| status_message | str | Confirmation message like "Wrote N changes." from ui_apply. |
| iter_changes | Iterator[tuple] | Iterator of (path, old_value, new_value) tuples for changed components. |
Usage Examples
from modules.ui_loadsave import UiLoadsave
# Create a loadsave instance tied to a defaults file
loadsave = UiLoadsave("ui-config.json")
# After building the Gradio UI, register all blocks
loadsave.add_block(demo)
# Create the defaults editing UI
loadsave.create_ui()
# After all blocks registered, finalize
loadsave.setup_ui()
# Dump defaults for first-run
loadsave.dump_defaults()