diff options
author | AUTOMATIC <16777216c@gmail.com> | 2022-11-19 16:10:17 +0000 |
---|---|---|
committer | AUTOMATIC <16777216c@gmail.com> | 2022-11-19 16:10:28 +0000 |
commit | 3596af07493ab7981ef92074f979eeee8fa624c4 (patch) | |
tree | c04bcc2b18dcb385dbc2698e01094b1808503f91 /modules/script_callbacks.py | |
parent | ccd73fc186603b626d996b888d628ebb6b1a38d8 (diff) | |
download | stable-diffusion-webui-gfx803-3596af07493ab7981ef92074f979eeee8fa624c4.tar.gz stable-diffusion-webui-gfx803-3596af07493ab7981ef92074f979eeee8fa624c4.tar.bz2 stable-diffusion-webui-gfx803-3596af07493ab7981ef92074f979eeee8fa624c4.zip |
Add API for scripts to add elements anywhere in UI.
Diffstat (limited to 'modules/script_callbacks.py')
-rw-r--r-- | modules/script_callbacks.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/modules/script_callbacks.py b/modules/script_callbacks.py index f19e164c..8e22f875 100644 --- a/modules/script_callbacks.py +++ b/modules/script_callbacks.py @@ -61,6 +61,8 @@ callback_map = dict( callbacks_before_image_saved=[],
callbacks_image_saved=[],
callbacks_cfg_denoiser=[],
+ callbacks_before_component=[],
+ callbacks_after_component=[],
)
@@ -137,6 +139,22 @@ def cfg_denoiser_callback(params: CFGDenoiserParams): report_exception(c, 'cfg_denoiser_callback')
+def before_component_callback(component, **kwargs):
+ for c in callback_map['callbacks_before_component']:
+ try:
+ c.callback(component, **kwargs)
+ except Exception:
+ report_exception(c, 'before_component_callback')
+
+
+def after_component_callback(component, **kwargs):
+ for c in callback_map['callbacks_after_component']:
+ try:
+ c.callback(component, **kwargs)
+ except Exception:
+ report_exception(c, 'after_component_callback')
+
+
def add_callback(callbacks, fun):
stack = [x for x in inspect.stack() if x.filename != __file__]
filename = stack[0].filename if len(stack) > 0 else 'unknown file'
@@ -220,3 +238,20 @@ def on_cfg_denoiser(callback): - params: CFGDenoiserParams - parameters to be passed to the inner model and sampling state details.
"""
add_callback(callback_map['callbacks_cfg_denoiser'], callback)
+
+
+def on_before_component(callback):
+ """register a function to be called before a component is created.
+ The callback is called with arguments:
+ - component - gradio component that is about to be created.
+ - **kwargs - args to gradio.components.IOComponent.__init__ function
+
+ Use elem_id/label fields of kwargs to figure out which component it is.
+ This can be useful to inject your own components somewhere in the middle of vanilla UI.
+ """
+ add_callback(callback_map['callbacks_before_component'], callback)
+
+
+def on_after_component(callback):
+ """register a function to be called after a component is created. See on_before_component for more."""
+ add_callback(callback_map['callbacks_after_component'], callback)
|