File size: 6,976 Bytes
9ff4bea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from __future__ import annotations

from os import PathLike
from pathlib import Path
from typing import TYPE_CHECKING, Self

from typing_extensions import deprecated

from rerun.error_utils import _send_warning_or_raise
from rerun_bindings import _ServerInternal

from .catalog import CatalogClient

if TYPE_CHECKING:
    from collections.abc import Sequence
    from types import TracebackType

__all__ = ["Server"]


class Server:
    """
    A Rerun server instance.

    This class allows you to start and manage a Rerun server programmatically.
    The server hosts recordings and serves them via HTTP, and provides access to
    the catalog through a client connection.
    When the object goes out of scope the server is automatically shut down.

    The server can be used as a context manager, which will automatically shut down
    the server when exiting the context.

    Example
    -------
    ```python
    import rerun as rr

    # Start a server with some datasets
    with rr.server.Server(port=9876, datasets={"my_data": ["path/to/data.rrd"]}) as server:
        client = server.client()

        # Use the client to interact with the catalog
        dataset = client.get_dataset("my_data")
    ```

    """

    def __init__(
        self,
        *,
        # Binding all interfaces is the intended default: the viewer is normally opened
        # from another machine. Narrow it with `host=` when that is not wanted.
        host: str = "0.0.0.0",  # noqa: S104
        port: int | None = None,
        datasets: dict[str, str | PathLike[str] | Sequence[str | PathLike[str]]] | None = None,
        tables: dict[str, PathLike[str]] | None = None,
        addr: str = "0.0.0.0",  # noqa: S104
    ) -> None:
        """
        Create a new Rerun server instance and start it.

        The server will host recordings and serve them via HTTP. If datasets are provided, they will be loaded and made
        available when the server starts.

        Parameters
        ----------
        host:
            The IP address to bind the server to.
            The default, `"0.0.0.0"`, lets other machines on your network connect.
            Use `"127.0.0.1"` to allow connections from this machine only.
        port:
            The port to bind the server to, or `None` (or `0`) to let the OS select a random
            available port. Use `url()` to get the address of the running server.
        datasets:
            Optional dictionary specifying dataset to load in the server at startup. Values in the dictionary may be
            either of:
            - a single path: must be a directory, all the RRDs it contains will be registered
            - a sequence of paths: each path must be a RRD file, which will all be registered
        tables:
            Optional dictionary mapping table names to lance file paths which will be loaded and made available when the
            server starts.
        addr:
            Deprecated: Renamed to `host`

        """

        if host == "0.0.0.0" and addr != "0.0.0.0":  # noqa: S104
            host = addr
            _send_warning_or_raise(
                "The `addr` parameter is deprecated in Rerun 0.29, and has been renamed to `host`.",
                depth_to_user_code=1,
                warning_type=DeprecationWarning,
            )

        # Port 0 lets the OS pick a free port for us. `url()` reports the actual port.
        resolved_port = 0 if port is None else port

        all_datasets = {}
        all_dataset_prefixes = {}

        if datasets is not None:
            for name, path in datasets.items():
                if isinstance(path, str | PathLike):
                    path = Path(path)

                    if not path.is_dir():
                        raise ValueError(f"Prefix path '{path}' for dataset '{name}' must be a directory.")

                    all_dataset_prefixes[name] = str(path.absolute())
                else:
                    paths = [Path(p) for p in path]
                    for p in paths:
                        if not p.is_file():
                            raise ValueError(f"Path '{p}' for dataset '{name}' must be a RRD file.")

                    all_datasets[name] = [str(p.absolute()) for p in paths]

        self._internal = _ServerInternal(
            host=host,
            port=resolved_port,
            datasets=all_datasets,
            dataset_prefixes=all_dataset_prefixes,
            tables={name: str(path) for name, path in (tables or {}).items()},
        )

    def url(self) -> str:
        """Get the URL of the server to which clients can connect."""
        return self._internal.url()

    @deprecated("Renamed to `url`.")
    def address(self) -> str:
        return self.url()

    def host(self) -> str:
        """Get the host (IP) that we've bound the server to."""
        return self._internal.host()

    def client(self) -> CatalogClient:
        """
        Get a CatalogClient connected to this server.

        The client can be used to interact with the server's catalog, including
        querying datasets and tables.

        Note: the `datafusion` package is required to use the client. The client
        initialization will fail with an error if the package is not installed.

        Returns
        -------
        CatalogClient
            A client for interacting with the server's catalog.

        Raises
        ------
        RuntimeError
            If the server is not running.

        """
        if not self._internal.is_running():
            raise RuntimeError("Cannot create client: server is not running.")

        return CatalogClient(self._internal.url(), token=None)

    def is_running(self) -> bool:
        """
        Check if the server is currently running.

        Returns
        -------
        bool
            `True` if the server is running, `False` otherwise.

        """
        return self._internal.is_running()

    def shutdown(self) -> None:
        """
        Stop the server.

        After calling this method, the server will no longer be accessible.

        Raises
        ------
        RuntimeError
            If the server is not running.

        """
        self._internal.shutdown()

    def _inject_error(self, method: str) -> None:
        """Testing utility: make the given gRPC method fail with a ``NotFound`` error."""
        self._internal.inject_error(method)

    def _clear_injected_error(self, method: str) -> None:
        """Testing utility: stop failing a previously injected endpoint."""
        self._internal.clear_injected_error(method)

    def __enter__(self) -> Self:
        """Enter the context manager, returning the server instance."""
        return self

    def __exit__(
        self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None
    ) -> None:
        """Exit the context manager, shutting down the server."""
        self._internal.shutdown()