Skip to content

Commit f130a60

Browse files
committed
Update jupyter.py
1 parent 61c3db8 commit f130a60

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

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)