Extensions Development
Common Mistakes
- Execution on import:
Keep extension modules import-safe. Do not run work at import time (except trivial and allowed
install.pysetup). Put logic inside functions/classes and run it from app callbacks. Import-time execution can slow startup or crash the server. - Keeping all Python code in
/scripts: Use/scriptsas the entry point only. Split implementation into other modules outside/scriptsand import them from the entry file. Otherwise, server startup may load and execute files repeatedly. - Browser namespace collisions:
JavaScript files are loaded into a shared global namespace.
Use unique prefixes for functions and variables instead of generic names such as
log(). - Incorrect callback usage:
Choose callbacks carefully.
onUiLoadedruns once, whileonUiUpdatedcan run many times during startup. Running heavy initialization in a frequently called callback causes slow page loads and repeated work. This applies to both Python and JavaScript. - Executing code when disabled: If an extension is disabled, callbacks should early-exit and do no work. Otherwise, you can add avoidable delays before, during, or after generation.
- Unsafe references:
Treat callback data and server variables as optional unless guaranteed.
For example,
image.infomay not exist for every image. Use safe access helpers such asget()orgetattr(). The same applies to settings: do not assume they never change. - Unsafe patching:
Extensions can patch server methods, but patch carefully.
For example, replacing
forwardshould avoid global behavior that breaks other extensions. - Running platform-specific code:
Avoid hard-coding platform/backend behavior.
Do not force values such as
torch.to('cuda')ortorch.float16. Use server variables such asdevices.deviceanddevices.dtype. - Assuming values:
Do not assume install paths or runtime values.
For example, an extension may not be located under
/extensions.
Many popular extensions still hit several of these issues. Remember that installing an extension grants it broad access, so implementation quality and safety matter.
Extension vs Script
- Script is a single module that implements script callbacks.
- Script objects inherit from
Scriptand implement required methods plus optional callbacks. - Extension is a larger implementation with its own folder structure.
- Extension can contain multiple scripts or operate through its own hooks.
Extension Folder Structure
Note: all files listed below are optional.
/preload.pyLoaded early during startup to add command line arguments. Definepreload(parser: argparser)and avoid other work at this stage./install.pyLoaded early during startup to install optional requirements. Use supported helpers such aslaunch.run_pip. Avoid direct OS calls or unrelated work./javascript/*.jsJavaScript files are injected as-is. Keep files limited to defining functions/variables and registering callbacks./style.cssStyle is loaded as-is./scripts/*.pyMain extension entry points loaded by the server during startup. Keep files focused on definitions and callback registration.