A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
feat: mo.running_in_notebook() (#1155)
Akshay Agrawal8395c0028952
diff --git a/docs/api/index.md b/docs/api/index.mdindex 7a2cfe81ba..121d1832ea 100644--- a/docs/api/index.md+++ b/docs/api/index.md@@ -19,7 +19,7 @@cli_argsstatecell- debugging+ miscellaneous```Use the marimo library in marimo notebooks (`import marimo as mo`) to@@ -46,4 +46,4 @@ Use the marimo library in marimo notebooks (`import marimo as mo`) to| {doc}`cli_args` | Access command-line arguments with `mo.cli_args` || {doc}`state` | Synchronize multiple UI elements with `mo.state` || {doc}`cell` | Run cells defined in another notebook |-| {doc}`debugging` | Debugging utilities |+| {doc}`miscellaneous` | Miscellaneous utilities |diff --git a/docs/api/debugging.md b/docs/api/miscellaneous.mdsimilarity index 52%rename from docs/api/debugging.mdrename to docs/api/miscellaneous.mdindex 326e481747..dfb9262783 100644--- a/docs/api/debugging.md+++ b/docs/api/miscellaneous.md@@ -1,4 +1,11 @@-# Debugging+# Miscellaneous++Use `mo.running_in_notebook()` to tell if Python is running in a marimo+notebook.++```{eval-rst}+.. autofunction:: marimo.running_in_notebook+```Use `mo.defs` and `mo.refs` to get the global definitions and references ofa cell.diff --git a/marimo/__init__.py b/marimo/__init__.pyindex ba48035f75..8b25b92b58 100644--- a/marimo/__init__.py+++ b/marimo/__init__.py@@ -46,6 +46,7 @@ __all__ = ["redirect_stdout","refs","right",+ "running_in_notebook","stat","state","status",@@ -93,6 +94,12 @@ from marimo._runtime.capture import (redirect_stdout,)from marimo._runtime.control_flow import MarimoStopError, stop-from marimo._runtime.runtime import cli_args, defs, query_params, refs+from marimo._runtime.runtime import (+ cli_args,+ defs,+ query_params,+ refs,+ running_in_notebook,+)from marimo._runtime.state import statefrom marimo._server.asgi import create_asgi_appdiff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex a02f67b3dd..46cf1368a2 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -235,6 +235,19 @@ def cli_args() -> CLIArgs:return get_context().cli_args+@mddoc+def running_in_notebook() -> bool:+ """Returns True if running in a marimo notebook, False otherwise"""+ from marimo._runtime.context.kernel_context import KernelRuntimeContext++ try:+ ctx = get_context()+ except ContextNotInitializedError:+ return False+ else:+ return isinstance(ctx, KernelRuntimeContext)++@dataclasses.dataclassclass CellMetadata:"""CellMetadatadiff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.pyindex 60ecebdeec..49f4b8c04a 100644--- a/tests/_runtime/test_runtime.py+++ b/tests/_runtime/test_runtime.py@@ -24,7 +24,7 @@ from marimo._runtime.requests import (SetCellConfigRequest,SetUIElementValueRequest,)-from marimo._runtime.runtime import Kernel+from marimo._runtime.runtime import Kernel, running_in_notebookfrom tests.conftest import ExecReqProviderif TYPE_CHECKING:@@ -894,6 +894,19 @@ def test_sys_path_updated(tmp_path: pathlib.Path) -> None:sys.modules["__main__"] = main+async def test_running_in_notebook(+ k: Kernel, exec_req: ExecReqProvider+) -> None:+ await k.run(+ [exec_req.get("import marimo as mo; in_nb = mo.running_in_notebook()")]+ )+ assert k.globals["in_nb"]+++def test_not_running_in_notebook() -> None:+ assert not running_in_notebook()++class TestAsyncIO:@staticmethodasync def test_toplevel_await_allowed(