|
3 | 3 | import atexit |
4 | 4 | import sys |
5 | 5 | import contextlib |
6 | | -import os |
7 | 6 | from IPython.core.display import HTML, JSON |
8 | 7 | from IPython.display import display |
9 | 8 | from importlib.resources import as_file, files |
|
12 | 11 | atexit.register(_resource_stack.close) |
13 | 12 |
|
14 | 13 |
|
15 | | -def room_html(url: str | None = None, token: str | None = None) -> HTML: |
16 | | - """ |
17 | | - Display a LiveKit room in Jupyter or Google Colab. |
18 | | -
|
19 | | - Args: |
20 | | - url (str | None): The LiveKit room URL. If None, the function attempts |
21 | | - to use the LIVEKIT_JUPYTER_URL environment variable in a local or |
22 | | - Colab environment. |
23 | | - token (str | None): The LiveKit join token. If None, the function |
24 | | - attempts to use the LIVEKIT_JUPYTER_URL environment variable in a |
25 | | - local or Colab environment. |
26 | | -
|
27 | | - Returns: |
28 | | - IPython.core.display.HTML: The HTML object that embeds the LiveKit room. |
29 | | -
|
30 | | - Raises: |
31 | | - ValueError: If both `url` and `token` are None and |
32 | | - `LIVEKIT_JUPYTER_URL` is not set. |
33 | | - """ |
| 14 | +def room_html(url: str, token: str) -> HTML: |
34 | 15 | IN_COLAB = "google.colab" in sys.modules |
35 | 16 |
|
36 | | - if url is None and token is None: |
37 | | - if IN_COLAB: |
38 | | - from google.colab import userdata |
39 | | - |
40 | | - LIVEKIT_JUPYTER_URL = userdata.get("LIVEKIT_JUPYTER_URL") |
41 | | - else: |
42 | | - LIVEKIT_JUPYTER_URL = os.environ.get("LIVEKIT_JUPYTER_URL") |
43 | | - |
44 | | - if not LIVEKIT_JUPYTER_URL: |
45 | | - raise ValueError("LIVEKIT_JUPYTER_URL must be set (or url/token must be provided).") |
46 | | - |
47 | 17 | if IN_COLAB: |
48 | 18 | from google.colab import output |
49 | 19 |
|
50 | | - def create_join_token(): |
51 | | - return JSON({"url": url or "", "token": token or ""}) |
| 20 | + def get_join_token(): |
| 21 | + return JSON({"url": url, "token": token}) |
52 | 22 |
|
53 | | - output.register_callback("get_join_token", create_join_token) |
| 23 | + output.register_callback("get_join_token", get_join_token) |
54 | 24 |
|
55 | | - # Load the local HTML file that embeds the LiveKit client |
56 | 25 | index_path = files("livekit.rtc.resources") / "jupyter-html" / "index.html" |
57 | 26 | index_path = _resource_stack.enter_context(as_file(index_path)) |
58 | | - |
59 | 27 | return HTML(index_path.read_text()) |
60 | 28 |
|
61 | 29 |
|
62 | | -def display_room(url: str | None = None, token: str | None = None) -> None: |
| 30 | +def display_room(url: str, token: str) -> None: |
63 | 31 | """ |
64 | 32 | Display a LiveKit room in Jupyter or Google Colab. |
65 | 33 |
|
66 | 34 | Args: |
67 | | - url (str | None): The LiveKit room URL. If None, the function attempts |
68 | | - to use the LIVEKIT_JUPYTER_URL environment variable in a local or |
69 | | - Colab environment. |
70 | | - token (str | None): The LiveKit join token. If None, the function |
71 | | - attempts to use the LIVEKIT_JUPYTER_URL environment variable in a |
72 | | - local or Colab environment. |
| 35 | + url (str | None): The LiveKit room URL. |
| 36 | + token (str | None): The LiveKit join token. |
73 | 37 | """ |
74 | 38 | display(room_html(url, token)) |
0 commit comments