Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/check-types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
run: python -m pip install --upgrade mypy

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

- name: Check Types
run: python -m mypy --install-type --non-interactive -p 'livekit-protocol' -p 'livekit-api' -p 'livekit-rtc'
18 changes: 13 additions & 5 deletions livekit-rtc/livekit/rtc/jupyter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# type: ignore
from __future__ import annotations

import atexit
import contextlib
import html
from IPython.core.display import HTML
from IPython.display import display
from importlib.resources import as_file, files
Expand All @@ -11,7 +11,7 @@
atexit.register(_resource_stack.close)


def room_html(url: str, token: str) -> HTML:
def room_html(url: str, token: str, *, width: str, height: str) -> HTML:
"""
Generate the HTML needed to embed a LiveKit room.

Expand All @@ -35,10 +35,18 @@ def room_html(url: str, token: str) -> HTML:
html_text = index_path.read_text()
html_text = html_text.replace(token_placeholder, token)
html_text = html_text.replace(url_placeholder, url)
return HTML(html_text)

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

def display_room(url: str, token: str) -> None:

def display_room(url: str, token: str, *, width: str = "100%", height: str = "110px") -> None:
"""
Display a LiveKit room in a Jupyter notebook or Google Colab.

Expand All @@ -50,4 +58,4 @@ def display_room(url: str, token: str) -> None:
The rendered HTML will include the provided `url` and `token` in plain text.
Avoid using sensitive tokens in public notebooks (e.g., tokens with long expiration times).
"""
display(room_html(url, token))
display(room_html(url, token, width=width, height=height))
Loading