Skip to content

Commit 5dd9685

Browse files
committed
expose dataloaders to python SDK
1 parent ffe0a30 commit 5dd9685

3 files changed

Lines changed: 122 additions & 1 deletion

File tree

rerun_py/rerun_sdk/rerun/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@
7777
"get_recording_id",
7878
"get_thread_local_data_recording",
7979
"is_enabled",
80-
"log_components",
8180
"log",
81+
"log_components",
82+
"log_file_from_contents",
83+
"log_file_from_path",
8284
"memory_recording",
8385
"new_entity_path",
8486
"reset_time",
@@ -105,6 +107,8 @@
105107
escape_entity_path_part,
106108
log,
107109
log_components,
110+
log_file_from_contents,
111+
log_file_from_path,
108112
new_entity_path,
109113
)
110114
from .any_value import AnyValues

rerun_py/rerun_sdk/rerun/_log.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from pathlib import Path
34
from typing import Any, Iterable
45

56
import pyarrow as pa
@@ -282,6 +283,72 @@ def log_components(
282283
)
283284

284285

286+
@catch_and_log_exceptions()
287+
def log_file_from_path(
288+
file_path: str | Path,
289+
*,
290+
recording: RecordingStream | None = None,
291+
) -> None:
292+
r"""
293+
Logs the file at the given `path` using all `re_data_source::DataLoader`s available.
294+
295+
A single `path` might be handled by more than one loader.
296+
297+
This method blocks until either at least one `re_data_source::DataLoader` starts
298+
streaming data in or all of them fail.
299+
300+
See <https://www.rerun.io/docs/howto/open-any-file> for more information.
301+
302+
Parameters
303+
----------
304+
file_path:
305+
Path to the file to be logged.
306+
307+
recording:
308+
Specifies the [`rerun.RecordingStream`][] to use. If left unspecified,
309+
defaults to the current active data recording, if there is one. See
310+
also: [`rerun.init`][], [`rerun.set_global_data_recording`][].
311+
312+
"""
313+
314+
bindings.log_file_from_path(Path(file_path), recording=recording)
315+
316+
317+
@catch_and_log_exceptions()
318+
def log_file_from_contents(
319+
file_path: str | Path,
320+
file_contents: bytes,
321+
*,
322+
recording: RecordingStream | None = None,
323+
) -> None:
324+
r"""
325+
Logs the given `file_contents` using all `crate::DataLoader`s available.
326+
327+
A single `path` might be handled by more than one loader.
328+
329+
This method blocks until either at least one `re_data_source::DataLoader` starts
330+
streaming data in or all of them fail.
331+
332+
See <https://www.rerun.io/docs/howto/open-any-file> for more information.
333+
334+
Parameters
335+
----------
336+
file_path:
337+
Path to the file that the `file_contents` belong to.
338+
339+
file_contents:
340+
Contents to be logged.
341+
342+
recording:
343+
Specifies the [`rerun.RecordingStream`][] to use. If left unspecified,
344+
defaults to the current active data recording, if there is one. See
345+
also: [`rerun.init`][], [`rerun.set_global_data_recording`][].
346+
347+
"""
348+
349+
bindings.log_file_from_contents(Path(file_path), file_contents, recording=recording)
350+
351+
285352
def escape_entity_path_part(part: str) -> str:
286353
r"""
287354
Escape an individual part of an entity path.

rerun_py/src/python_bridge.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ fn rerun_bindings(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
180180

181181
// log any
182182
m.add_function(wrap_pyfunction!(log_arrow_msg, m)?)?;
183+
m.add_function(wrap_pyfunction!(log_file_from_path, m)?)?;
184+
m.add_function(wrap_pyfunction!(log_file_from_contents, m)?)?;
183185

184186
// misc
185187
m.add_function(wrap_pyfunction!(version, m)?)?;
@@ -962,6 +964,54 @@ fn log_arrow_msg(
962964
Ok(())
963965
}
964966

967+
#[pyfunction]
968+
#[pyo3(signature = (
969+
file_path,
970+
recording=None,
971+
))]
972+
fn log_file_from_path(
973+
py: Python<'_>,
974+
file_path: std::path::PathBuf,
975+
recording: Option<&PyRecordingStream>,
976+
) -> PyResult<()> {
977+
let Some(recording) = get_data_recording(recording) else {
978+
return Ok(());
979+
};
980+
981+
recording
982+
.log_file_from_path(file_path)
983+
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?;
984+
985+
py.allow_threads(flush_garbage_queue);
986+
987+
Ok(())
988+
}
989+
990+
#[pyfunction]
991+
#[pyo3(signature = (
992+
file_path,
993+
file_contents,
994+
recording=None,
995+
))]
996+
fn log_file_from_contents(
997+
py: Python<'_>,
998+
file_path: std::path::PathBuf,
999+
file_contents: &[u8],
1000+
recording: Option<&PyRecordingStream>,
1001+
) -> PyResult<()> {
1002+
let Some(recording) = get_data_recording(recording) else {
1003+
return Ok(());
1004+
};
1005+
1006+
recording
1007+
.log_file_from_contents(file_path, std::borrow::Cow::Borrowed(file_contents))
1008+
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?;
1009+
1010+
py.allow_threads(flush_garbage_queue);
1011+
1012+
Ok(())
1013+
}
1014+
9651015
// --- Misc ---
9661016

9671017
/// Return a verbose version string

0 commit comments

Comments
 (0)