Skip to content

Commit d936de4

Browse files
committed
add local kernel support
1 parent 628c61b commit d936de4

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,38 @@ export async function fetchJoinInfo(): Promise<{ url: string; token: string }> {
2020
if (invoke) {
2121
const res = await invoke("get_join_token", []);
2222
return res.data["application/json"];
23-
} else if ((window as any).jupyterFetchJoinToken) {
24-
return await (window as any).jupyterFetchJoinToken();
25-
} else if (import.meta.env.MODE === "development") {
26-
// use env variables
23+
}
24+
25+
26+
// This requires that JupyterLab was started with --LabApp.expose_app_in_browser,
27+
if ((window as any).jupyterapp && (window as any).jupyterapp.shell) {
28+
const currentWidget = (window as any).jupyterapp.shell.currentWidget;
29+
if (currentWidget && currentWidget.context?.sessionContext) {
30+
const session = currentWidget.context.sessionContext.session;
31+
if (session && session.kernel) {
32+
try {
33+
const comm = session.kernel.createComm("get_join_token_comm");
34+
comm.open();
35+
comm.send({ request: "token" });
36+
return new Promise((resolve) => {
37+
comm.onMsg = (msg: any) => {
38+
resolve(msg.content.data);
39+
};
40+
});
41+
} catch (error) {
42+
throw new Error("Error creating comm channel: " + error);
43+
}
44+
}
45+
}
46+
}
47+
48+
if (import.meta.env.MODE === "development") {
2749
const url = import.meta.env.VITE_LIVEKIT_URL;
2850
const token = import.meta.env.VITE_LIVEKIT_TOKEN;
29-
return { url: url, token: token };
30-
} else {
31-
throw new Error("No Colab or Jupyter kernel function available");
51+
return { url, token };
3252
}
53+
54+
throw new Error("No suitable kernel connection available");
3355
}
3456

3557

livekit-rtc/livekit/rtc/jupyter.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# type: ignore
12
from __future__ import annotations
23

34
import atexit
@@ -13,6 +14,7 @@
1314

1415
def room_html(url: str, token: str) -> HTML:
1516
IN_COLAB = "google.colab" in sys.modules
17+
IN_JUPYTER = "ipykernel" in sys.modules
1618

1719
if IN_COLAB:
1820
from google.colab import output
@@ -21,6 +23,18 @@ def get_join_token():
2123
return JSON({"url": url, "token": token})
2224

2325
output.register_callback("get_join_token", get_join_token)
26+
elif IN_JUPYTER:
27+
from IPython import get_ipython
28+
29+
ip = get_ipython()
30+
if ip and hasattr(ip, "kernel"):
31+
32+
def token_comm_target(comm, open_msg):
33+
@comm.on_msg
34+
def handle_message(msg):
35+
comm.send({"url": url, "token": token})
36+
37+
ip.kernel.comm_manager.register_target("get_join_token_comm", token_comm_target)
2438

2539
index_path = files("livekit.rtc.resources") / "jupyter-html" / "index.html"
2640
index_path = _resource_stack.enter_context(as_file(index_path))

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)