From 7801a08d77f8e35e213bc8c94f46cf239beac507 Mon Sep 17 00:00:00 2001 From: "Thomas S." <2565098+thomass-dev@users.noreply.github.com> Date: Mon, 27 Apr 2026 17:26:44 +0200 Subject: [PATCH] Feat Set `is_jupyter=True` on Jupyterlite --- rich/console.py | 8 ++++++-- tests/test_console.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/rich/console.py b/rich/console.py index 0bdce76987..6275207e69 100644 --- a/rich/console.py +++ b/rich/console.py @@ -508,14 +508,18 @@ def _is_jupyter() -> bool: # pragma: no cover get_ipython # type: ignore[name-defined] except NameError: return False + ipython = get_ipython() # type: ignore[name-defined] + ipython_cls_str = str(ipython.__class__) shell = ipython.__class__.__name__ + if ( - "google.colab" in str(ipython.__class__) + "google.colab" in ipython_cls_str + or "pyodide" in ipython_cls_str or os.getenv("DATABRICKS_RUNTIME_VERSION") or shell == "ZMQInteractiveShell" ): - return True # Jupyter notebook or qtconsole + return True # Jupyter notebook, Jupyterlite or qtconsole elif shell == "TerminalInteractiveShell": return False # Terminal running IPython else: diff --git a/tests/test_console.py b/tests/test_console.py index e52c1b3633..7f2970b5f1 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -1133,3 +1133,15 @@ def isatty(self) -> bool: assert not console.is_terminal # Should not have auto-detected assert not console.file.called_isatty + + +def test_is_jupyter_in_jupyterlite(monkeypatch): + monkeypatch.setattr( + "rich.console.get_ipython", + type("Interpreter", (object,), {"__module__": "pyodide_kernel.interpreter"}), + raising=False, + ) + + console = Console(file=io.StringIO(), force_jupyter=None) + + assert console.is_jupyter