How can I detect that my code is running in a Marimo notebook/app #8865
-
|
Is there a recommended way to detect that code is running in a Marimo environment? I have some code that needs to make a decision based on whether it runs in Marimo vs Jupyter vs desktop, etc. I found |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Or maybe (edited): import __main__ as main_module
def is_in_marimo():
try:
main_module.__marimo__
return True
except AttributeError:
passAdded later: Or even shorter, thanks to @brisvag: import __main__ as main_module
def is_in_marimo():
return hasattr(main_module, '__marimo__'): |
Beta Was this translation helpful? Give feedback.
-
|
Great question! marimo has a public API for this — If you don't want to take a dependency on marimo, you can guard the import: import sys
def is_in_marimo():
if "marimo" not in sys.modules:
return False
return __import__("marimo").running_in_notebook() |
Beta Was this translation helpful? Give feedback.
Great question! marimo has a public API for this —
marimo.running_in_notebook(). It returnsTrueonly when code is actually executing inside the marimo runtime (edit or run mode), not just when marimo happens to be imported.If you don't want to take a dependency on marimo, you can guard the import: