|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Example of an executable data-loader plugin for the Rerun Viewer.""" |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import os |
| 7 | + |
| 8 | +import rerun as rr # pip install rerun-sdk |
| 9 | + |
| 10 | +# The Rerun Viewer will always pass these two pieces of information: |
| 11 | +# 1. The path to be loaded, as a positional arg. |
| 12 | +# 2. A shared recording ID, via the `--recording-id` flag. |
| 13 | +# |
| 14 | +# It is up to you whether you make use of that shared recording ID or not. |
| 15 | +# If you use it, the data will end up in the same recording as all other plugins interested in |
| 16 | +# that file, otherwise you can just create a dedicated recording for it. Or both. |
| 17 | +parser = argparse.ArgumentParser( |
| 18 | + description=""" |
| 19 | +This is an example executable data-loader plugin for the Rerun Viewer. |
| 20 | +
|
| 21 | +It will log Python source code files as markdown documents. |
| 22 | +To try it out, copy it in your $PATH as `rerun-loader-python-file`, then open a Python source file with Rerun (`rerun file.py`). |
| 23 | +""" |
| 24 | +) |
| 25 | +parser.add_argument("filepath", type=str) |
| 26 | +parser.add_argument("--recording-id", type=str) |
| 27 | +args = parser.parse_args() |
| 28 | + |
| 29 | + |
| 30 | +def main() -> None: |
| 31 | + is_file = os.path.isfile(args.filepath) |
| 32 | + is_rust_file = os.path.splitext(args.filepath)[1].lower() == ".py" |
| 33 | + |
| 34 | + # We're not interested: just exit silently. |
| 35 | + # Don't return an error, as that would show up to the end user in the Rerun Viewer! |
| 36 | + if not (is_file and is_rust_file): |
| 37 | + return |
| 38 | + |
| 39 | + rr.init("rerun_example_external_loader", recording_id=args.recording_id) |
| 40 | + # The most important part of this: log to standard output so the Rerun Viewer can ingest it! |
| 41 | + rr.stdout() |
| 42 | + |
| 43 | + with open(args.filepath) as file: |
| 44 | + body = file.read() |
| 45 | + text = f"""## Some Python code\n```python\n{body}\n```\n""" |
| 46 | + rr.log(args.filepath, rr.TextDocument(text, media_type=rr.MediaType.MARKDOWN), timeless=True) |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + main() |
0 commit comments