Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add polling and reload callback for extensions. #8789

Merged
merged 6 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion modules/script_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,36 @@ def __init__(self, imgs, cols, rows):
callbacks_infotext_pasted=[],
callbacks_script_unloaded=[],
callbacks_before_ui=[],
callbacks_on_reload=[],
callbacks_on_polling=[],
)


def clear_callbacks():
for callback_list in callback_map.values():
callback_list.clear()


def app_started_callback(demo: Optional[Blocks], app: FastAPI):
for c in callback_map['callbacks_app_started']:
try:
c.callback(demo, app)
except Exception:
report_exception(c, 'app_started_callback')

def app_polling_callback(demo: Optional[Blocks], app: FastAPI):
for c in callback_map['callbacks_on_polling']:
try:
c.callback()
except Exception:
report_exception(c, 'callbacks_on_polling')

def app_reload_callback(demo: Optional[Blocks], app: FastAPI):
for c in callback_map['callbacks_on_reload']:
try:
c.callback()
except Exception:
report_exception(c, 'callbacks_on_reload')


def model_loaded_callback(sd_model):
for c in callback_map['callbacks_model_loaded']:
Expand Down Expand Up @@ -227,6 +242,14 @@ def on_app_started(callback):
add_callback(callback_map['callbacks_app_started'], callback)


def on_polling(callback):
"""register a function to be called on each polling of the server."""
add_callback(callback_map['callbacks_on_polling'], callback)

def on_before_reload(callback):
"""register a function to be called just before the server reloads."""
add_callback(callback_map['callbacks_on_reload'], callback)

def on_model_loaded(callback):
"""register a function to be called when the stable diffusion model is created; the model is
passed as an argument; this function is also called when the script is reloaded. """
Expand Down
2 changes: 2 additions & 0 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ def create_api(app):
def wait_on_server(demo=None):
while 1:
time.sleep(0.5)
modules.script_callbacks.app_polling_callback(None, demo)
if shared.state.need_restart:
modules.script_callbacks.app_reload_callback(None, demo)
shared.state.need_restart = False
time.sleep(0.5)
demo.close()
Expand Down