Skip to content

Commit 0874404

Browse files
Merge pull request #3982 from MaikoTan/on-started-callback
feat: add app started callback
2 parents 40b3a7e + 081df45 commit 0874404

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

modules/script_callbacks.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from collections import namedtuple
44
import inspect
55

6+
from fastapi import FastAPI
7+
from gradio import Blocks
68

79
def report_exception(c, job):
810
print(f"Error executing callback {job} for {c.script}", file=sys.stderr)
@@ -25,6 +27,7 @@ def __init__(self, image, p, filename, pnginfo):
2527

2628

2729
ScriptCallback = namedtuple("ScriptCallback", ["script", "callback"])
30+
callbacks_app_started = []
2831
callbacks_model_loaded = []
2932
callbacks_ui_tabs = []
3033
callbacks_ui_settings = []
@@ -40,6 +43,14 @@ def clear_callbacks():
4043
callbacks_image_saved.clear()
4144

4245

46+
def app_started_callback(demo: Blocks, app: FastAPI):
47+
for c in callbacks_app_started:
48+
try:
49+
c.callback(demo, app)
50+
except Exception:
51+
report_exception(c, 'app_started_callback')
52+
53+
4354
def model_loaded_callback(sd_model):
4455
for c in callbacks_model_loaded:
4556
try:
@@ -91,6 +102,12 @@ def add_callback(callbacks, fun):
91102
callbacks.append(ScriptCallback(filename, fun))
92103

93104

105+
def on_app_started(callback):
106+
"""register a function to be called when the webui started, the gradio `Block` component and
107+
fastapi `FastAPI` object are passed as the arguments"""
108+
add_callback(callbacks_app_started, callback)
109+
110+
94111
def on_model_loaded(callback):
95112
"""register a function to be called when the stable diffusion model is created; the model is
96113
passed as an argument"""

webui.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import modules.sd_models
2424
import modules.shared as shared
2525
import modules.txt2img
26+
import modules.script_callbacks
2627

2728
import modules.ui
2829
from modules import devices
@@ -140,6 +141,8 @@ def webui():
140141
if launch_api:
141142
create_api(app)
142143

144+
modules.script_callbacks.app_started_callback(demo, app)
145+
143146
wait_on_server(demo)
144147

145148
sd_samplers.set_samplers()

0 commit comments

Comments
 (0)