Skip to content

Commit 89bc49c

Browse files
authored
isolate the jupyter frontend inside an iframe (#411)
1 parent 9164765 commit 89bc49c

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

.github/workflows/check-types.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
run: python -m pip install --upgrade mypy
3030

3131
- name: Install packages
32-
run: python -m pip install pytest ./livekit-api ./livekit-protocol ./livekit-rtc pydantic numpy
32+
run: python -m pip install pytest ./livekit-api ./livekit-protocol ./livekit-rtc pydantic numpy ipython
3333

3434
- name: Check Types
3535
run: python -m mypy --install-type --non-interactive -p 'livekit-protocol' -p 'livekit-api' -p 'livekit-rtc'

livekit-rtc/livekit/rtc/jupyter.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# type: ignore
21
from __future__ import annotations
32

43
import atexit
54
import contextlib
5+
import html
66
from IPython.core.display import HTML
77
from IPython.display import display
88
from importlib.resources import as_file, files
@@ -11,7 +11,7 @@
1111
atexit.register(_resource_stack.close)
1212

1313

14-
def room_html(url: str, token: str) -> HTML:
14+
def room_html(url: str, token: str, *, width: str, height: str) -> HTML:
1515
"""
1616
Generate the HTML needed to embed a LiveKit room.
1717
@@ -35,10 +35,18 @@ def room_html(url: str, token: str) -> HTML:
3535
html_text = index_path.read_text()
3636
html_text = html_text.replace(token_placeholder, token)
3737
html_text = html_text.replace(url_placeholder, url)
38-
return HTML(html_text)
3938

39+
escaped_content = html.escape(html_text, quote=True)
40+
# the extra space in the iframe_html is to avoid IPython suggesting to use IFrame instead.
41+
# it isn't possible in our case, as we need to use srcdoc.
42+
iframe_html = (
43+
f' <iframe width="{width}" height="{height}" frameborder="0" '
44+
f'srcdoc="{escaped_content}"></iframe>'
45+
)
46+
return HTML(iframe_html)
4047

41-
def display_room(url: str, token: str) -> None:
48+
49+
def display_room(url: str, token: str, *, width: str = "100%", height: str = "110px") -> None:
4250
"""
4351
Display a LiveKit room in a Jupyter notebook or Google Colab.
4452
@@ -50,4 +58,4 @@ def display_room(url: str, token: str) -> None:
5058
The rendered HTML will include the provided `url` and `token` in plain text.
5159
Avoid using sensitive tokens in public notebooks (e.g., tokens with long expiration times).
5260
"""
53-
display(room_html(url, token))
61+
display(room_html(url, token, width=width, height=height))

0 commit comments

Comments
 (0)