Skip to content

Commit a35a9d0

Browse files
committed
add python example
1 parent 5dd9685 commit a35a9d0

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

examples/python/log_file/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!--[metadata]
2+
title = "Log file example"
3+
-->
4+
5+
Demonstrates how to log any file from the SDK using the [`DataLoader`](https://www.rerun.io/docs/howto/open-any-file) machinery.
6+
7+
Usage:
8+
```bash
9+
python examples/python/log_file/main.py examples/assets
10+
```
11+

examples/python/log_file/main.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Demonstrates how to log any file from the SDK using the `DataLoader` machinery.
4+
5+
See <https://www.rerun.io/docs/howto/open-any-file> for more information.
6+
7+
Usage:
8+
```
9+
python examples/python/log_file/main.py -- examples/assets
10+
```
11+
"""
12+
from __future__ import annotations
13+
14+
import argparse
15+
from pathlib import Path
16+
17+
import rerun as rr # pip install rerun-sdk
18+
19+
parser = argparse.ArgumentParser(
20+
description="Demonstrates how to log any file from the SDK using the `DataLoader` machinery."
21+
)
22+
rr.script_add_args(parser)
23+
parser.add_argument(
24+
"--from-contents",
25+
action="store_true",
26+
default=False,
27+
help="Log the contents of the file directly (files only -- not supported by external loaders).",
28+
)
29+
parser.add_argument("filepaths", nargs="+", type=Path, help="The filepaths to be loaded and logged.")
30+
args = parser.parse_args()
31+
32+
rr.script_setup(args, "rerun_example_log_file")
33+
34+
for filepath in args.filepaths:
35+
if not args.from_contents:
36+
# Either log the file using its path…
37+
rr.log_file_from_path(filepath)
38+
else:
39+
# …or using its contents if you already have them loaded for some reason.
40+
try:
41+
with open(filepath, "rb") as file:
42+
rr.log_file_from_contents(filepath, file.read())
43+
except Exception:
44+
pass
45+
46+
rr.script_teardown(args)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
numpy
2+
rerun-sdk

0 commit comments

Comments
 (0)