Skip to content

Commit

Permalink
Add --stdout/-o to our CLI helper library (#4544)
Browse files Browse the repository at this point in the history
You can now pipe all Python & Rust examples into a Rerun Viewer.


![image](https://github.com/rerun-io/rerun/assets/2910679/ff65e6f9-a5e6-497f-a108-577fcd0c6e57)


Checks:
- [x] python examples/python/minimal_options/main.py -o | rerun -
- [x] cargo r -p minimal_options -- -o | rerun -
  • Loading branch information
teh-cmc authored Dec 15, 2023
1 parent 32a468c commit ef32593
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
15 changes: 15 additions & 0 deletions crates/rerun/src/clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ enum RerunBehavior {

Save(PathBuf),

Stdout,

#[cfg(feature = "web_viewer")]
Serve,

Expand Down Expand Up @@ -47,6 +49,10 @@ pub struct RerunArgs {
#[clap(long)]
save: Option<PathBuf>,

/// Log data to standard output, to be piped into a Rerun Viewer.
#[clap(long, short = 'o')]
stdout: bool,

/// Connects and sends the logged data to a remote Rerun viewer.
///
/// Optionally takes an `ip:port`.
Expand Down Expand Up @@ -89,6 +95,11 @@ impl RerunArgs {
#[track_caller] // track_caller so that we can see if we are being called from an official example.
pub fn init(&self, application_id: &str) -> anyhow::Result<(RecordingStream, ServeGuard)> {
match self.to_behavior()? {
RerunBehavior::Stdout => Ok((
RecordingStreamBuilder::new(application_id).stdout()?,
Default::default(),
)),

RerunBehavior::Connect(addr) => Ok((
RecordingStreamBuilder::new(application_id)
.connect_opts(addr, crate::default_flush_timeout())?,
Expand Down Expand Up @@ -144,6 +155,10 @@ impl RerunArgs {

#[allow(clippy::unnecessary_wraps)] // False positive on some feature flags
fn to_behavior(&self) -> anyhow::Result<RerunBehavior> {
if self.stdout {
return Ok(RerunBehavior::Stdout);
}

if let Some(path) = self.save.as_ref() {
return Ok(RerunBehavior::Save(path.clone()));
}
Expand Down
11 changes: 10 additions & 1 deletion rerun_py/rerun_sdk/rerun/script_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def script_add_args(parser: ArgumentParser) -> None:
)
parser.add_argument("--addr", type=str, default=None, help="Connect to this ip:port")
parser.add_argument("--save", type=str, default=None, help="Save data to a .rrd file at this path")
parser.add_argument(
"-o",
"--stdout",
dest="stdout",
action="store_true",
help="Log data to standard output, to be piped into a Rerun Viewer",
)


def script_setup(
Expand All @@ -77,7 +84,9 @@ def script_setup(
rec: RecordingStream = rr.get_global_data_recording() # type: ignore[assignment]

# NOTE: mypy thinks these methods don't exist because they're monkey-patched.
if args.serve:
if args.stdout:
rec.stdout() # type: ignore[attr-defined]
elif args.serve:
rec.serve() # type: ignore[attr-defined]
elif args.connect:
# Send logging data to separate `rerun` process.
Expand Down

0 comments on commit ef32593

Please sign in to comment.