Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notebook_show, notebook_run for Jupyter notebook environments #615

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mesop/colab/colab_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ def colab_run(*, port: int = 32123, prod_mode: bool = False):
if not colab_utils.is_running_in_colab():
print("Not running Colab: `colab_run` is a no-op")
return
notebook_run(port=port, prod_mode=prod_mode)


def notebook_run(*, port: int = 32123, prod_mode: bool = False):
"""
When running in a notebook environment, this will launch the web server.

Otherwise, this is a no-op.

Use this for non-Colab notebook environments like Jupyter/JupyterLab.
"""
if not colab_utils.is_running_ipython():
print("Not running in a notebook environment: `notebook_run` is a no-op")
return
# Ensures the flags are marked as parsed before creating the app otherwise you will
# get UnparsedFlagAccessError.
#
Expand Down
1 change: 1 addition & 0 deletions mesop/colab/colab_run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def start(self) -> None:
def setup(monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr(threading, "Thread", FakeThread)
monkeypatch.setattr(colab_utils, "is_running_in_colab", lambda: True)
monkeypatch.setattr(colab_utils, "is_running_ipython", lambda: True)


def test_colab_run_prod_mode():
Expand Down
25 changes: 25 additions & 0 deletions mesop/colab/colab_show.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from mesop.colab import colab_utils


def colab_show(
port: int = 32123, path: str = "/", width: str = "100%", height: str = "400"
):
"""
Displays Mesop app in a Colab cell output.
"""
if not colab_utils.is_running_in_colab():
print("Not running Colab: `colab_show` is a no-op")
return

# Intentionally import inside the function because the colab
# package is only available in some environments that Mesop runs in.
from google.colab import output # type: ignore
Expand All @@ -14,3 +21,21 @@ def colab_show(
width=width,
height=height,
)


def notebook_show(
port: int = 32123, path: str = "/", width: str = "100%", height: str = "400"
):
"""Displays the Mesop app in a notebook cell output as an IFrame.

Use this for non-Colab notebook environments like Jupyter/JupyterLab.
"""
if not colab_utils.is_running_ipython():
print("Not running in a notebook environment: `notebook_show` is a no-op")
return

from IPython.display import IFrame, display # type: ignore

display(
IFrame(src=f"http://localhost:{port}{path}", width=width, height=height)
)
6 changes: 5 additions & 1 deletion mesop/colab/colab_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@


def is_running_in_colab() -> bool:
return "google.colab" in sys.modules and hasattr(builtins, "get_ipython")
return "google.colab" in sys.modules and is_running_ipython()


def is_running_ipython():
return hasattr(builtins, "get_ipython")
Loading