Skip to content

Commit a3df5a8

Browse files
committed
wip
1 parent d9e2e0a commit a3df5a8

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

livekit-rtc/jupyter-html/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { ConnectionState } from 'livekit-client';
1818
export async function fetchJoinInfo(): Promise<{ url: string; token: string }> {
1919
const invoke = (window as any).google?.colab?.kernel?.invokeFunction;
2020
if (invoke) {
21-
const res = await invoke("create_join_token", []);
21+
const res = await invoke("get_join_token", []);
2222
return res.data["application/json"];
2323
} else if ((window as any).jupyterFetchJoinToken) {
2424
return await (window as any).jupyterFetchJoinToken();

livekit-rtc/livekit/rtc/jupyter.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,58 @@
1+
from __future__ import annotations
2+
13
import atexit
4+
import sys
25
import contextlib
36
import os
4-
from IPython.core.display import HTML
7+
from IPython.core.display import HTML, JSON
58
from importlib.resources import as_file, files
69

710
_resource_stack = contextlib.ExitStack()
811
atexit.register(_resource_stack.close)
912

1013

11-
def display_room() -> HTML:
12-
try:
13-
from google.colab import secrets
14+
def display_room(url: str | None = None, token: str | None = None) -> HTML:
15+
"""
16+
Display a LiveKit room in Jupyter or Google Colab.
17+
18+
Args:
19+
url (str | None): The LiveKit room URL. If None, the function attempts
20+
to use the LIVEKIT_JUPYTER_URL environment variable in a local or
21+
Colab environment.
22+
token (str | None): The LiveKit join token. If None, the function
23+
attempts to use the LIVEKIT_JUPYTER_URL environment variable in a
24+
local or Colab environment.
25+
26+
Returns:
27+
IPython.core.display.HTML: The HTML object that embeds the LiveKit room.
28+
29+
Raises:
30+
ValueError: If both `url` and `token` are None and
31+
`LIVEKIT_JUPYTER_URL` is not set.
32+
"""
33+
IN_COLAB = "google.colab" in sys.modules
34+
35+
if url is None and token is None:
36+
if IN_COLAB:
37+
from google.colab import userdata
38+
39+
LIVEKIT_JUPYTER_URL = userdata.get("LIVEKIT_JUPYTER_URL")
40+
else:
41+
LIVEKIT_JUPYTER_URL = os.environ.get("LIVEKIT_JUPYTER_URL")
42+
43+
if not LIVEKIT_JUPYTER_URL:
44+
raise ValueError("LIVEKIT_JUPYTER_URL must be set (or url/token must be provided).")
45+
46+
if IN_COLAB:
47+
from google.colab import output
1448

15-
LIVEKIT_JUPYTER_URL = secrets.get("LIVEKIT_JUPYTER_URL")
16-
except ImportError:
17-
LIVEKIT_JUPYTER_URL = os.environ.get("LIVEKIT_JUPYTER_URL")
49+
def create_join_token():
50+
return JSON({"url": url or "", "token": token or ""})
1851

19-
if not LIVEKIT_JUPYTER_URL:
20-
raise ValueError(
21-
"LIVEKIT_JUPYTER_URL must be set via Google Colab secrets or as an environment variable."
22-
)
52+
output.register_callback("get_join_token", create_join_token)
2353

54+
# Load the local HTML file that embeds the LiveKit client
2455
index_path = files("livekit.rtc.resources") / "jupyter-html" / "index.html"
2556
index_path = _resource_stack.enter_context(as_file(index_path))
26-
html_text = index_path.read_text()
2757

28-
return HTML(html_text)
58+
return HTML(index_path.read_text())

livekit-rtc/livekit/rtc/resources/jupyter-html/index.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)