From 45df1b1978e597546a208ecbf5b3aca56137edc5 Mon Sep 17 00:00:00 2001 From: "Yuichiro Tachibana (Tsuchiya)" Date: Mon, 23 Dec 2024 14:46:26 +0900 Subject: [PATCH] Lite: Capture stdout and stderr from the main thread (#9984) * Add stdout and stderr events * add changeset * Refactoring * Format App.tsx * add changeset * Add python-error event to capture Python errors occurring in the running event loop after the initial app launch * Fix 's close button * Fix * Propagate python-error and initialization-error events to the controller * Add init-code|file-run-error events --------- Co-authored-by: gradio-pr-bot --- .changeset/pink-signs-fall.md | 7 ++++ gradio/queueing.py | 4 +- gradio/wasm_utils.py | 32 +++++++++++++++ js/lite/lite.html | 2 +- js/lite/src/ErrorDisplay.svelte | 66 ++++++++++++++++++++---------- js/lite/src/LiteIndex.svelte | 60 ++++++++++++++++++++------- js/lite/src/dev/App.svelte | 51 +++++++++++++++++++++++ js/lite/src/index.ts | 26 ++++++++++++ js/wasm/src/message-types.ts | 26 +++++++++++- js/wasm/src/webworker/file.test.ts | 4 +- js/wasm/src/webworker/file.ts | 1 - js/wasm/src/webworker/index.ts | 50 ++++++++++++++++++---- js/wasm/src/worker-proxy.ts | 35 ++++++++++++---- 13 files changed, 308 insertions(+), 56 deletions(-) create mode 100644 .changeset/pink-signs-fall.md diff --git a/.changeset/pink-signs-fall.md b/.changeset/pink-signs-fall.md new file mode 100644 index 0000000000000..ea94952877020 --- /dev/null +++ b/.changeset/pink-signs-fall.md @@ -0,0 +1,7 @@ +--- +"@gradio/lite": minor +"@gradio/wasm": minor +"gradio": minor +--- + +feat:Lite: Capture stdout and stderr from the main thread diff --git a/gradio/queueing.py b/gradio/queueing.py index 63df1bfa285cf..82ad5568d6fe9 100644 --- a/gradio/queueing.py +++ b/gradio/queueing.py @@ -13,7 +13,7 @@ import fastapi -from gradio import route_utils, routes +from gradio import route_utils, routes, wasm_utils from gradio.data_classes import ( PredictBodyInternal, ) @@ -644,6 +644,7 @@ async def process_events( err = e for event in awake_events: content = error_payload(err, app.get_blocks().show_error) + wasm_utils.send_error(err) self.send_message( event, ProcessCompletedMessage( @@ -736,6 +737,7 @@ async def process_events( success = False error = err or old_err output = error_payload(error, app.get_blocks().show_error) + wasm_utils.send_error(error) for event in awake_events: self.send_message( event, ProcessCompletedMessage(output=output, success=success) diff --git a/gradio/wasm_utils.py b/gradio/wasm_utils.py index bd78667629a12..360f8e579c5c7 100644 --- a/gradio/wasm_utils.py +++ b/gradio/wasm_utils.py @@ -1,9 +1,13 @@ from __future__ import annotations +import logging import sys +import traceback from contextlib import contextmanager from contextvars import ContextVar +LOGGER = logging.getLogger(__name__) + # See https://pyodide.org/en/stable/usage/faq.html#how-to-detect-that-code-is-run-with-pyodide IS_WASM = sys.platform == "emscripten" @@ -57,3 +61,31 @@ def get_registered_app(app_id: str): 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): + # The callback registered by the JS process is called with the error traceback + # for the WebWorker process to read the traceback. + + 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) diff --git a/js/lite/lite.html b/js/lite/lite.html index 843ed059eadfb..12c62912409b9 100644 --- a/js/lite/lite.html +++ b/js/lite/lite.html @@ -30,6 +30,6 @@
-
+
diff --git a/js/lite/src/ErrorDisplay.svelte b/js/lite/src/ErrorDisplay.svelte index f65ad22a37f88..a8ac1082e3534 100644 --- a/js/lite/src/ErrorDisplay.svelte +++ b/js/lite/src/ErrorDisplay.svelte @@ -1,38 +1,61 @@ - -
- {#if error} - {#if error.message} -

- {error.message} -

+ dispatch("clear_error")} + > +
+ {#if error} + {#if error.message} +

+ {error.message} +

+ {/if} + {#if error.stack} +
{error.stack}
+ {/if} {/if} - {#if error.stack} -
{error.stack}
- {/if} - {/if} -
-
+
+
+