Skip to content

Commit 2d3e9cf

Browse files
authored
Improve rerun.notebook.Viewer constructor (#9495)
For a while now, the `use_global_recording` param of `rr.notebook.Viewer` didn't actually do as advertised. Setting it to `False` would make the `Viewer` constructor ignore (almost) all other parameters. The original intent for that parameter was to allow creating a completely empty Viewer. The new behavior is: - `use_global_recording` defaults to `False` if a `url` is provided. - `use_global_recording` defaults to `True` otherwise. - If no explicit recording is provided, and `use_global_recording` is `True`, then we use `get_data_recording` to pick up either the thread-local or global recording. This means it is now possible to create a completely empty Viewer, and that is the default behavior given no params, and no global recording: ```python import rerun as rr rr.notebook.Viewer() ``` If a `url` is provided, then by default we only show that `url`: ```python import rerun as rr rr.notebook.Viewer(url="https://app.rerun.io/version/nightly/examples/arkit_scenes.rrd") ``` Using both a `url` and either an explicit/global `recording` is still possible: ```python import rerun as rr rec = rr.RecordingStream("rerun_example_dual_recording") rr.notebook.Viewer( url="https://app.rerun.io/version/nightly/examples/arkit_scenes.rrd", recording=rec, ) # alternatively, call `add_recording` later: rec = rr.RecordingStream("rerun_example_dual_recording") viewer = rr.notebook.Viewer( url="https://app.rerun.io/version/nightly/examples/arkit_scenes.rrd", ) viewer.add_recording(rec) viewer.display() # or just let it pick up the global recording: rr.init("rerun_example_global_recording_notebook") rr.notebook.Viewer( url="https://app.rerun.io/version/nightly/examples/arkit_scenes.rrd", use_global_recording=True, ) ``` If no `url` is provided, we pick up the global recording by default, but it can be turned off: ```python import rerun as rr rr.init("rerun_example_global_recording_notebook") rr.notebook.Viewer() # uses global recording rr.init("rerun_example_global_recording_notebook") rr.notebook.Viewer(use_global_recording=False) # will be empty ```
1 parent 60aca5f commit 2d3e9cf

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

examples/python/notebook/cube.ipynb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@
191191
"source": [
192192
"## Starting a new recording\n",
193193
"\n",
194-
"You can always start another recording by calling `rr.init(...)` again to reset the global stream, or alternatively creating a separate recording stream using `rr.new_recording` (discussed more below)"
194+
"You can always start another recording by calling `rr.init(...)` again to reset the global stream, or alternatively creating a separate recording stream using the `rr.RecordingStream` constructor (discussed more below)"
195195
]
196196
},
197197
{
@@ -316,7 +316,7 @@
316316
"STEPS = 100\n",
317317
"twists = math.pi * np.sin(np.linspace(0, math.tau, STEPS)) / 4\n",
318318
"for t in range(STEPS):\n",
319-
" rr.set_time_sequence(\"step\", t)\n",
319+
" rr.set_time(\"step\", sequence=t)\n",
320320
" h_grid = build_color_grid(10, 3, 3, twist=twists[t])\n",
321321
" rr.log(\"h_grid\", rr.Points3D(h_grid.positions, colors=h_grid.colors, radii=0.5))\n",
322322
" v_grid = build_color_grid(3, 3, 10, twist=twists[t])\n",
@@ -347,7 +347,7 @@
347347
"STEPS = 100\n",
348348
"twists = math.pi * np.sin(np.linspace(0, math.tau, STEPS)) / 4\n",
349349
"for t in range(STEPS):\n",
350-
" rr.set_time_sequence(\"step\", t)\n",
350+
" rr.set_time(\"step\", sequence=t)\n",
351351
" h_grid = build_color_grid(10, 3, 3, twist=twists[t])\n",
352352
" rr.log(\"h_grid\", rr.Points3D(h_grid.positions, colors=h_grid.colors, radii=0.5))\n",
353353
" v_grid = build_color_grid(3, 3, 10, twist=twists[t])\n",
@@ -363,7 +363,7 @@
363363
"source": [
364364
"## Working with non-global streams\n",
365365
"\n",
366-
"Sometimes it can be more explicit to work with specific (non-global recording) streams via the `new_recording` method.\n",
366+
"Sometimes it can be more explicit to work with specific (non-global recording) streams via `rr.RecordingStream` constructor.\n",
367367
"\n",
368368
"In this case, remember to call `notebook_show` directly on the recording stream. As noted above, there is no way to use a bare Blueprint object in conjunction with a non-global recording."
369369
]
@@ -375,7 +375,7 @@
375375
"metadata": {},
376376
"outputs": [],
377377
"source": [
378-
"rec = rr.new_recording(\"rerun_example_cube_flat\")\n",
378+
"rec = rr.RecordingStream(\"rerun_example_cube_flat\")\n",
379379
"\n",
380380
"bp = rrb.Blueprint(collapse_panels=True)\n",
381381
"\n",
@@ -404,7 +404,7 @@
404404
"metadata": {},
405405
"outputs": [],
406406
"source": [
407-
"rec = rr.new_recording(\"rerun_example_multi_recording\", recording_id=uuid.uuid4())\n",
407+
"rec = rr.RecordingStream(\"rerun_example_multi_recording\", recording_id=uuid.uuid4())\n",
408408
"\n",
409409
"flat_grid = build_color_grid(20, 20, 1, twist=0)\n",
410410
"rec.log(\"flat_grid\", rr.Points3D(flat_grid.positions, colors=flat_grid.colors, radii=0.5))\n",
@@ -416,19 +416,19 @@
416416
},
417417
{
418418
"cell_type": "code",
419-
"execution_count": null,
419+
"execution_count": 15,
420420
"id": "40354733-7feb-45fe-8ba6-dbbdbc070983",
421421
"metadata": {},
422422
"outputs": [],
423423
"source": [
424-
"rec = rr.new_recording(\"rerun_example_multi_recording\", recording_id=uuid.uuid4())\n",
424+
"rec = rr.RecordingStream(\"rerun_example_multi_recording\", recording_id=uuid.uuid4())\n",
425425
"\n",
426426
"viewer.add_recording(rec)\n",
427427
"\n",
428428
"STEPS = 100\n",
429429
"twists = math.pi * np.sin(np.linspace(0, math.tau, STEPS)) / 4\n",
430430
"for t in range(STEPS):\n",
431-
" rr.set_time(\"step\", sequence=t)\n",
431+
" rec.set_time(\"step\", sequence=t)\n",
432432
" cube = build_color_grid(10, 10, 10, twist=twists[t])\n",
433433
" rec.log(\"cube\", rr.Points3D(cube.positions, colors=cube.colors, radii=0.5))"
434434
]
@@ -450,12 +450,12 @@
450450
"metadata": {},
451451
"outputs": [],
452452
"source": [
453-
"viewer = rr.notebook.Viewer(use_global_recording=False)\n",
453+
"viewer = rr.notebook.Viewer()\n",
454454
"viewer.display()\n",
455455
"\n",
456456
"recordings = [\n",
457-
" rr.new_recording(\"rerun_example_time_ctrl\", recording_id=\"example_a\"),\n",
458-
" rr.new_recording(\"rerun_example_time_ctrl\", recording_id=\"example_b\"),\n",
457+
" rr.RecordingStream(\"rerun_example_time_ctrl\", recording_id=\"example_a\"),\n",
458+
" rr.RecordingStream(\"rerun_example_time_ctrl\", recording_id=\"example_b\"),\n",
459459
"]\n",
460460
"\n",
461461
"rec_colors = {\"example_a\": [0, 255, 0], \"example_b\": [255, 0, 0]}\n",
@@ -465,10 +465,10 @@
465465
"\n",
466466
"STEPS = 100\n",
467467
"twists = math.pi * np.sin(np.linspace(0, math.tau, STEPS)) / 4\n",
468-
"for t in range(STEPS):\n",
469-
" cube = build_color_grid(10, 10, 10, twist=twists[t])\n",
470-
" for rec in recordings:\n",
471-
" rr.set_time(\"step\", sequence=t)\n",
468+
"for rec in recordings:\n",
469+
" for t in range(STEPS):\n",
470+
" cube = build_color_grid(10, 10, 10, twist=twists[t])\n",
471+
" rec.set_time(\"step\", sequence=t)\n",
472472
" rec.log(\"cube\", rr.Points3D(cube.positions, colors=rec_colors[rec.get_recording_id()], radii=0.5))"
473473
]
474474
},
@@ -482,7 +482,7 @@
482482
},
483483
{
484484
"cell_type": "code",
485-
"execution_count": 27,
485+
"execution_count": 22,
486486
"id": "3d6804d6",
487487
"metadata": {},
488488
"outputs": [],
@@ -500,7 +500,7 @@
500500
},
501501
{
502502
"cell_type": "code",
503-
"execution_count": 28,
503+
"execution_count": 23,
504504
"id": "30d0107b",
505505
"metadata": {},
506506
"outputs": [],

rerun_py/rerun_sdk/rerun/notebook.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __init__(
8787
url: str | None = None,
8888
blueprint: BlueprintLike | None = None,
8989
recording: RecordingStream | None = None,
90-
use_global_recording: bool = True,
90+
use_global_recording: bool | None = None,
9191
) -> None:
9292
"""
9393
Create a new Rerun viewer widget for use in a notebook.
@@ -115,10 +115,12 @@ def __init__(
115115
116116
Setting this is equivalent to calling [`rerun.send_blueprint`][] before initializing the viewer.
117117
use_global_recording:
118-
Whether or not the Viewer should default to the global recording in case no explicit `recording`
119-
is specified.
118+
If no explicit `recording` is provided, the Viewer uses the thread-local/global recording created by `rr.init`
119+
or set explicitly via `rr.set_thread_local_data_recording`/`rr.set_global_data_recording`.
120120
121-
If this is set to `False`, then `blueprint` is ignored.
121+
Settings this to `False` causes the Viewer to not pick up the global recording.
122+
123+
Defaults to `False` if `url` is provided, and `True` otherwise.
122124
123125
"""
124126

@@ -150,24 +152,27 @@ def __init__(
150152
url=url,
151153
)
152154

155+
# By default, we use the global recording only if no `url` is provided.
156+
if use_global_recording is None:
157+
use_global_recording = url is None
158+
153159
if use_global_recording:
154160
recording = get_data_recording(recording)
155-
if recording is None:
156-
if url is None:
157-
raise ValueError("No recording or url specified and no active recording found")
158-
elif blueprint is not None:
159-
raise ValueError(
160-
"Can only set a blueprint if there's either an active recording or a recording passed in"
161-
)
161+
162+
if recording is not None:
163+
bindings.set_callback_sink(
164+
recording=recording.to_native(),
165+
callback=self._flush_hook,
166+
)
167+
168+
if blueprint is not None:
169+
if recording is not None:
170+
recording.send_blueprint(blueprint)
162171
else:
163-
bindings.set_callback_sink(
164-
recording=recording.to_native(),
165-
callback=self._flush_hook,
172+
raise ValueError(
173+
"Can only set a blueprint if there's either an active recording or a recording passed in"
166174
)
167175

168-
if blueprint is not None:
169-
recording.send_blueprint(blueprint)
170-
171176
def add_recording(
172177
self,
173178
recording: RecordingStream | None = None,

0 commit comments

Comments
 (0)