|
from __future__ import annotations |
|
|
|
import logging |
|
import sys |
|
import traceback |
|
from contextlib import contextmanager |
|
from contextvars import ContextVar |
|
|
|
LOGGER = logging.getLogger(__name__) |
|
|
|
|
|
IS_WASM = sys.platform == "emscripten" |
|
|
|
|
|
class WasmUnsupportedError(Exception): |
|
pass |
|
|
|
|
|
|
|
|
|
|
|
app_map = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_app_id_context_var: ContextVar[str | None] = ContextVar("app_id", default=None) |
|
|
|
|
|
@contextmanager |
|
def app_id_context(app_id: str): |
|
token = _app_id_context_var.set(app_id) |
|
yield |
|
_app_id_context_var.reset(token) |
|
|
|
|
|
|
|
|
|
|
|
def register_app(_app): |
|
app_id = _app_id_context_var.get() |
|
|
|
if app_id in app_map: |
|
app = app_map[app_id] |
|
app.blocks.close() |
|
|
|
app_map[app_id] = _app |
|
|
|
|
|
class GradioAppNotFoundError(Exception): |
|
pass |
|
|
|
|
|
def get_registered_app(app_id: str): |
|
try: |
|
return app_map[app_id] |
|
except KeyError as e: |
|
raise GradioAppNotFoundError( |
|
f"Gradio app not found (ID: {app_id}). Forgot to call demo.launch()?" |
|
) from e |
|
|
|
|
|
error_traceback_callback_map = {} |
|
|
|
|
|
def register_error_traceback_callback(app_id, callback): |
|
error_traceback_callback_map[app_id] = callback |
|
|
|
|
|
def send_error(error: Exception | None): |
|
|
|
|
|
|
|
if not IS_WASM: |
|
return |
|
if error is None: |
|
return |
|
|
|
app_id = _app_id_context_var.get() |
|
callback = error_traceback_callback_map.get(app_id) |
|
if not callback: |
|
LOGGER.warning( |
|
f"Error callback not found for the app ID {app_id}. The error will be ignored." |
|
) |
|
return |
|
|
|
tb = "".join(traceback.format_exception(type(error), error, error.__traceback__)) |
|
callback(tb) |
|
|