marimo-team / marimo

A reactive notebook for Python — run reproducible experiments, execute as a script, deploy as an app, and version with git.
https://marimo.io
Apache License 2.0
8k stars 284 forks source link

How to determine if your python code is running in Marimo? #2906

Closed paddymul closed 5 days ago

paddymul commented 5 days ago

Documentation is

Explain in Detail

I'm a library author and I want to determine if my code is running in Marimo. In particular I want to register displayers for the appropriate context of my library.

Some environments have different error conditions and understanding the environment lets me send users better error messages, especially around the first install/use.

Your Suggestion for Changes

Here is my current utility function for determining the notebook environment

def determine_jupter_env():
    try:
        import psutil
    except ImportError:
        return "jupyterlite"
    parent_process = psutil.Process().parent().cmdline()[-1]

    if 'jupyter-lab' in parent_process:
        return "jupyter-lab"
    elif 'jupyter-notebook' in parent_process:
        return "jupyter-notebook"
    elif '__vsc_ipynb_file__' in globals():
        return "vscode"
    else:
        try:
            from IPython.core import getipython
            if 'google.colab' in str(getipython.get_ipython()):
                return "google-colab"
        except:
            pass
    return "unknown"
mscolnick commented 5 days ago

Something like this: https://docs.marimo.io/api/miscellaneous.html#marimo.running_in_notebook

The docs for this are here: https://docs.marimo.io/guides/integrating_with_marimo/index.html#integrating-with-marimo

Let us know if this can be improved

paddymul commented 5 days ago

Great. I did some searching, but that looks like exactly what I needed.

Thank you