foxglove-sdk 0.6.2__cp312-cp312-win32.whl → 0.16.6__cp312-cp312-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
foxglove/__init__.py CHANGED
@@ -1,121 +1,241 @@
1
- """
2
- This module provides interfaces for logging messages to Foxglove.
3
-
4
- See :py:mod:`foxglove.schemas` and :py:mod:`foxglove.channels` for working with well-known Foxglove
5
- schemas.
6
- """
7
-
8
- import atexit
9
- import logging
10
- from typing import List, Optional, Union
11
-
12
- from . import _foxglove_py as _foxglove
13
-
14
- # Re-export these imports
15
- from ._foxglove_py import (
16
- MCAPWriter,
17
- Schema,
18
- open_mcap,
19
- )
20
- from .channel import Channel, log
21
- from .websocket import (
22
- AssetHandler,
23
- Capability,
24
- ServerListener,
25
- Service,
26
- WebSocketServer,
27
- )
28
-
29
- atexit.register(_foxglove.shutdown)
30
-
31
-
32
- def start_server(
33
- *,
34
- name: Optional[str] = None,
35
- host: Optional[str] = "127.0.0.1",
36
- port: Optional[int] = 8765,
37
- capabilities: Optional[List[Capability]] = None,
38
- server_listener: Optional[ServerListener] = None,
39
- supported_encodings: Optional[List[str]] = None,
40
- services: Optional[List[Service]] = None,
41
- asset_handler: Optional[AssetHandler] = None,
42
- ) -> WebSocketServer:
43
- """
44
- Start a websocket server for live visualization.
45
-
46
- :param name: The name of the server.
47
- :param host: The host to bind to.
48
- :param port: The port to bind to.
49
- :param capabilities: A list of capabilities to advertise to clients.
50
- :param server_listener: A Python object that implements the :py:class:`websocket.ServerListener`
51
- protocol.
52
- :param supported_encodings: A list of encodings to advertise to clients.
53
- :param services: A list of services to advertise to clients.
54
- :param asset_handler: A callback function that returns the asset for a given URI, or None if
55
- it doesn't exist.
56
- """
57
- return _foxglove.start_server(
58
- name=name,
59
- host=host,
60
- port=port,
61
- capabilities=capabilities,
62
- server_listener=server_listener,
63
- supported_encodings=supported_encodings,
64
- services=services,
65
- asset_handler=asset_handler,
66
- )
67
-
68
-
69
- def set_log_level(level: Union[int, str] = "INFO") -> None:
70
- """
71
- Enable SDK logging.
72
-
73
- This function will call logging.basicConfig() for convenience in scripts, but in general you
74
- should configure logging yourself before calling this function:
75
- https://docs.python.org/3/library/logging.html
76
-
77
- :param level: The logging level to set. This accepts the same values as `logging.setLevel` and
78
- defaults to "INFO". The SDK will not log at levels "CRITICAL" or higher.
79
- """
80
- # This will raise a ValueError for invalid levels if the user has not already configured
81
- logging.basicConfig(level=level, format="%(asctime)s [%(levelname)s] %(message)s")
82
-
83
- if isinstance(level, str):
84
- level_map = (
85
- logging.getLevelNamesMapping()
86
- if hasattr(logging, "getLevelNamesMapping")
87
- else _level_names()
88
- )
89
- try:
90
- level = level_map[level]
91
- except KeyError:
92
- raise ValueError(f"Unknown log level: {level}")
93
- else:
94
- level = max(0, min(2**32 - 1, level))
95
-
96
- _foxglove.enable_logging(level)
97
-
98
-
99
- def _level_names() -> dict[str, int]:
100
- # Fallback for Python <3.11; no support for custom levels
101
- return {
102
- "CRITICAL": logging.CRITICAL,
103
- "FATAL": logging.FATAL,
104
- "ERROR": logging.ERROR,
105
- "WARN": logging.WARNING,
106
- "WARNING": logging.WARNING,
107
- "INFO": logging.INFO,
108
- "DEBUG": logging.DEBUG,
109
- "NOTSET": logging.NOTSET,
110
- }
111
-
112
-
113
- __all__ = [
114
- "Channel",
115
- "MCAPWriter",
116
- "Schema",
117
- "log",
118
- "open_mcap",
119
- "set_log_level",
120
- "start_server",
121
- ]
1
+ """
2
+ This module provides interfaces for logging messages to Foxglove.
3
+
4
+ See :py:mod:`foxglove.schemas` and :py:mod:`foxglove.channels` for working with well-known Foxglove
5
+ schemas.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import atexit
11
+ import logging
12
+ from typing import TYPE_CHECKING
13
+
14
+ from . import _foxglove_py as _foxglove
15
+
16
+ # Re-export these imports
17
+ from ._foxglove_py import (
18
+ ChannelDescriptor,
19
+ Context,
20
+ Schema,
21
+ SinkChannelFilter,
22
+ open_mcap,
23
+ )
24
+ from .channel import Channel, log
25
+
26
+ # Deprecated. Use foxglove.mcap.MCAPWriter instead.
27
+ from .mcap import MCAPWriter
28
+
29
+ if TYPE_CHECKING:
30
+ from .notebook.notebook_buffer import NotebookBuffer
31
+
32
+ atexit.register(_foxglove.shutdown)
33
+
34
+
35
+ try:
36
+ from ._foxglove_py.cloud import CloudSink
37
+
38
+ # from ._foxglove_py.cloud import start_cloud_sink as _start_cloud_sink
39
+ from .cloud import CloudSinkListener
40
+ from .websocket import (
41
+ AssetHandler,
42
+ Capability,
43
+ ServerListener,
44
+ Service,
45
+ WebSocketServer,
46
+ )
47
+
48
+ def start_server(
49
+ *,
50
+ name: str | None = None,
51
+ host: str | None = "127.0.0.1",
52
+ port: int | None = 8765,
53
+ capabilities: list[Capability] | None = None,
54
+ server_listener: ServerListener | None = None,
55
+ supported_encodings: list[str] | None = None,
56
+ services: list[Service] | None = None,
57
+ asset_handler: AssetHandler | None = None,
58
+ context: Context | None = None,
59
+ session_id: str | None = None,
60
+ channel_filter: SinkChannelFilter | None = None,
61
+ playback_time_range: tuple[int, int] | None = None,
62
+ ) -> WebSocketServer:
63
+ """
64
+ Start a websocket server for live visualization.
65
+
66
+ :param name: The name of the server.
67
+ :param host: The host to bind to.
68
+ :param port: The port to bind to.
69
+ :param capabilities: A list of capabilities to advertise to clients.
70
+ :param server_listener: A Python object that implements the
71
+ :py:class:`websocket.ServerListener` protocol.
72
+ :param supported_encodings: A list of encodings to advertise to clients.
73
+ :param services: A list of services to advertise to clients.
74
+ :param asset_handler: A callback function that returns the asset for a given URI, or None if
75
+ it doesn't exist.
76
+ :param context: The context to use for logging. If None, the global context is used.
77
+ :param session_id: An ID which allows the client to understand if the connection is a
78
+ re-connection or a new server instance. If None, then an ID is generated based on the
79
+ current time.
80
+ :param channel_filter: A `Callable` that determines whether a channel should be logged to.
81
+ Return `True` to log the channel, or `False` to skip it. By default, all channels
82
+ will be logged.
83
+ :param playback_time_range: Time range of data being played back, in absolute nanoseconds.
84
+ Implies `Capability.RangedPlayback` if set.
85
+ """
86
+ return _foxglove.start_server(
87
+ name=name,
88
+ host=host,
89
+ port=port,
90
+ capabilities=capabilities,
91
+ server_listener=server_listener,
92
+ supported_encodings=supported_encodings,
93
+ services=services,
94
+ asset_handler=asset_handler,
95
+ context=context,
96
+ session_id=session_id,
97
+ channel_filter=channel_filter,
98
+ playback_time_range=playback_time_range,
99
+ )
100
+
101
+ def start_cloud_sink(
102
+ *,
103
+ listener: CloudSinkListener | None = None,
104
+ supported_encodings: list[str] | None = None,
105
+ context: Context | None = None,
106
+ session_id: str | None = None,
107
+ ) -> CloudSink:
108
+ """
109
+ Connect to Foxglove Agent for live visualization and teleop.
110
+
111
+ Foxglove Agent must be running on the same host for this to work.
112
+
113
+ :param capabilities: A list of capabilities to advertise to the agent.
114
+ :param listener: A Python object that implements the
115
+ :py:class:`cloud.CloudSinkListener` protocol.
116
+ :param supported_encodings: A list of encodings to advertise to the agent.
117
+ :param context: The context to use for logging. If None, the global context is used.
118
+ :param session_id: An ID which allows the agent to understand if the connection is a
119
+ re-connection or a new connection instance. If None, then an ID is generated based on
120
+ the current time.
121
+ """
122
+ return _foxglove.start_cloud_sink(
123
+ listener=listener,
124
+ supported_encodings=supported_encodings,
125
+ context=context,
126
+ session_id=session_id,
127
+ )
128
+
129
+ except ImportError:
130
+ pass
131
+
132
+
133
+ def set_log_level(level: int | str = "INFO") -> None:
134
+ """
135
+ Enable SDK logging.
136
+
137
+ This function will call logging.basicConfig() for convenience in scripts, but in general you
138
+ should configure logging yourself before calling this function:
139
+ https://docs.python.org/3/library/logging.html
140
+
141
+ :param level: The logging level to set. This accepts the same values as `logging.setLevel` and
142
+ defaults to "INFO". The SDK will not log at levels "CRITICAL" or higher.
143
+ """
144
+ # This will raise a ValueError for invalid levels if the user has not already configured
145
+ logging.basicConfig(level=level, format="%(asctime)s [%(levelname)s] %(message)s")
146
+
147
+ if isinstance(level, str):
148
+ level_map = (
149
+ logging.getLevelNamesMapping()
150
+ if hasattr(logging, "getLevelNamesMapping")
151
+ else _level_names()
152
+ )
153
+ try:
154
+ level = level_map[level]
155
+ except KeyError:
156
+ raise ValueError(f"Unknown log level: {level}")
157
+ else:
158
+ level = max(0, min(2**32 - 1, level))
159
+
160
+ _foxglove.enable_logging(level)
161
+
162
+
163
+ def _level_names() -> dict[str, int]:
164
+ # Fallback for Python <3.11; no support for custom levels
165
+ return {
166
+ "CRITICAL": logging.CRITICAL,
167
+ "FATAL": logging.FATAL,
168
+ "ERROR": logging.ERROR,
169
+ "WARN": logging.WARNING,
170
+ "WARNING": logging.WARNING,
171
+ "INFO": logging.INFO,
172
+ "DEBUG": logging.DEBUG,
173
+ "NOTSET": logging.NOTSET,
174
+ }
175
+
176
+
177
+ def init_notebook_buffer(context: Context | None = None) -> NotebookBuffer:
178
+ """
179
+ Create a NotebookBuffer object to manage data buffering and visualization in Jupyter notebooks.
180
+
181
+ The NotebookBuffer object will buffer all data logged to the provided context. When you are
182
+ ready to visualize the data, you can call the
183
+ :meth:`~notebook.notebook_buffer.NotebookBuffer.show` method to display an embedded Foxglove
184
+ visualization widget. The widget provides a fully-featured Foxglove interface directly within
185
+ your Jupyter notebook, allowing you to explore multi-modal robotics data including 3D scenes,
186
+ plots, images, and more.
187
+
188
+ :param context: The Context used to log the messages. If no Context is provided, the global
189
+ context will be used. Logged messages will be buffered.
190
+
191
+ :returns: A NotebookBuffer object that can be used to manage the data buffering and
192
+ visualization.
193
+
194
+ :raises Exception: If the notebook extra package is not installed. Install it with ``pip install
195
+ foxglove-sdk[notebook]``.
196
+
197
+ :note: This function is only available when the `notebook` extra package is installed. Install
198
+ it with ``pip install foxglove-sdk[notebook]``.
199
+
200
+ Example:
201
+ >>> import foxglove
202
+ >>>
203
+ >>> # Create a basic viewer using the default context
204
+ >>> nb_buffer = foxglove.init_notebook_buffer()
205
+ >>>
206
+ >>> # Or use a specific context
207
+ >>> nb_buffer = foxglove.init_notebook_buffer(context=my_ctx)
208
+ >>>
209
+ >>> # ... log data as usual ...
210
+ >>>
211
+ >>> # Display the widget in the notebook
212
+ >>> nb_buffer.show()
213
+ """
214
+ try:
215
+ from .notebook.notebook_buffer import NotebookBuffer
216
+
217
+ except ImportError:
218
+ raise Exception(
219
+ "NotebookBuffer is not installed. "
220
+ 'Please install it with `pip install "foxglove-sdk[notebook]"`'
221
+ )
222
+
223
+ return NotebookBuffer(context=context)
224
+
225
+
226
+ __all__ = [
227
+ "Channel",
228
+ "ChannelDescriptor",
229
+ "Context",
230
+ "MCAPWriter",
231
+ "Schema",
232
+ "SinkChannelFilter",
233
+ "CloudSink",
234
+ "CloudSinkListener",
235
+ "start_cloud_sink",
236
+ "log",
237
+ "open_mcap",
238
+ "set_log_level",
239
+ "start_server",
240
+ "init_notebook_buffer",
241
+ ]