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

notebook integration: %burr_ui can show the web app in an iframe #430

Merged
merged 3 commits into from
Nov 21, 2024
Merged
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
42 changes: 33 additions & 9 deletions burr/integrations/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import subprocess

from IPython.core.magic import Magics, line_magic, magics_class
from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring
from IPython.core.shellapp import InteractiveShellApp
from IPython.display import IFrame, display


class NotebookEnvironment(enum.Enum):
Expand All @@ -15,9 +17,6 @@ class NotebookEnvironment(enum.Enum):


def identify_notebook_environment(ipython: InteractiveShellApp) -> NotebookEnvironment:
if "IPKernelApp" in ipython.config:
return NotebookEnvironment.JUPYTER

if os.environ.get("VSCODE_PID"):
return NotebookEnvironment.VSCODE

Expand All @@ -44,6 +43,10 @@ def identify_notebook_environment(ipython: InteractiveShellApp) -> NotebookEnvir
except ModuleNotFoundError:
pass

# this is the base case. IPKernelApp should always be available
if "IPKernelApp" in ipython.config:
return NotebookEnvironment.JUPYTER

raise RuntimeError(
f"Unknown notebook environment. Known environments: {list(NotebookEnvironment)}"
)
Expand Down Expand Up @@ -115,15 +118,36 @@ def __init__(self, notebook_env: NotebookEnvironment, **kwargs):
self.process = None
self.url = None

@magic_arguments()
@argument(
"--height",
"-h",
default=400,
type=int,
help="Height of the Burr UI iframe specified as a number of pixels",
)
@argument(
"--no-iframe",
action="store_true",
help="Passing this flag prints the URL of the launched Burr UI instead of displaying an iframe.",
)
@line_magic
def burr_ui(self, line):
"""Launch the Burr UI from a notebook cell"""
if self.process is not None:
print(f"Burr UI already running at {self.url}")
return

self.process, self.url = launch_ui(self.notebook_env)
print(f"Burr UI: {self.url}")
args = parse_argstring(self.burr_ui, line)

if self.process is None:
self.process, self.url = launch_ui(self.notebook_env)
else:
# if .poll() is not None, then subprocess exited. Try launching the server again
# TODO investigate `.returncode` for better failure/retry handling
if self.process.poll() is not None:
self.process, self.url = launch_ui(self.notebook_env)

if args.no_iframe is True:
print(f"Burr UI: {self.url}")
else:
display(IFrame(self.url, width="100%", height=args.height))
Comment on lines +147 to +150
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the default is to show it, but you can disable it?



def load_ipython_extension(ipython: InteractiveShellApp):
Expand Down
Loading