foxglove-sdk 0.15.1__cp39-cp39-win_amd64.whl → 0.15.2__cp39-cp39-win_amd64.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.

Potentially problematic release.


This version of foxglove-sdk might be problematic. Click here for more details.

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