Skip to content
Discussion options

You must be logged in to vote

Great question! marimo has a public API for this — marimo.running_in_notebook(). It returns True only 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:

import sys

def is_in_marimo():
    if "marimo" not in sys.modules:
        return False
    return __import__("marimo").running_in_notebook()

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@almarklein
Comment options

Answer selected by almarklein
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants