Skip to content

Commit

Permalink
add python example
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Feb 28, 2024
1 parent c3e67a1 commit db1bf62
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/python/log_file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!--[metadata]
title = "Log file example"
-->

Demonstrates how to log any file from the SDK using the [`DataLoader`](https://www.rerun.io/docs/howto/open-any-file) machinery.

Usage:
```bash
python examples/python/log_file/main.py examples/assets
```

46 changes: 46 additions & 0 deletions examples/python/log_file/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
"""
Demonstrates how to log any file from the SDK using the `DataLoader` machinery.
See <https://www.rerun.io/docs/howto/open-any-file> for more information.
Usage:
```
python examples/python/log_file/main.py -- examples/assets
```
"""
from __future__ import annotations

import argparse
from pathlib import Path

import rerun as rr # pip install rerun-sdk

parser = argparse.ArgumentParser(
description="Demonstrates how to log any file from the SDK using the `DataLoader` machinery."
)
rr.script_add_args(parser)
parser.add_argument(
"--from-contents",
action="store_true",
default=False,
help="Log the contents of the file directly (files only -- not supported by external loaders).",
)
parser.add_argument("filepaths", nargs="+", type=Path, help="The filepaths to be loaded and logged.")
args = parser.parse_args()

rr.script_setup(args, "rerun_example_log_file")

for filepath in args.filepaths:
if not args.from_contents:
# Either log the file using its path…
rr.log_file_from_path(filepath)
else:
# …or using its contents if you already have them loaded for some reason.
try:
with open(filepath, "rb") as file:
rr.log_file_from_contents(filepath, file.read())
except Exception:
pass

rr.script_teardown(args)
2 changes: 2 additions & 0 deletions examples/python/log_file/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy
rerun-sdk

0 comments on commit db1bf62

Please sign in to comment.