Skip to content

Improve rerun.notebook.Viewer constructor #9495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions examples/python/notebook/cube.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
"source": [
"## Starting a new recording\n",
"\n",
"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)"
"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)"
]
},
{
Expand Down Expand Up @@ -316,7 +316,7 @@
"STEPS = 100\n",
"twists = math.pi * np.sin(np.linspace(0, math.tau, STEPS)) / 4\n",
"for t in range(STEPS):\n",
" rr.set_time_sequence(\"step\", t)\n",
" rr.set_time(\"step\", sequence=t)\n",
" h_grid = build_color_grid(10, 3, 3, twist=twists[t])\n",
" rr.log(\"h_grid\", rr.Points3D(h_grid.positions, colors=h_grid.colors, radii=0.5))\n",
" v_grid = build_color_grid(3, 3, 10, twist=twists[t])\n",
Expand Down Expand Up @@ -347,7 +347,7 @@
"STEPS = 100\n",
"twists = math.pi * np.sin(np.linspace(0, math.tau, STEPS)) / 4\n",
"for t in range(STEPS):\n",
" rr.set_time_sequence(\"step\", t)\n",
" rr.set_time(\"step\", sequence=t)\n",
" h_grid = build_color_grid(10, 3, 3, twist=twists[t])\n",
" rr.log(\"h_grid\", rr.Points3D(h_grid.positions, colors=h_grid.colors, radii=0.5))\n",
" v_grid = build_color_grid(3, 3, 10, twist=twists[t])\n",
Expand All @@ -363,7 +363,7 @@
"source": [
"## Working with non-global streams\n",
"\n",
"Sometimes it can be more explicit to work with specific (non-global recording) streams via the `new_recording` method.\n",
"Sometimes it can be more explicit to work with specific (non-global recording) streams via `rr.RecordingStream` constructor.\n",
"\n",
"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."
]
Expand All @@ -375,7 +375,7 @@
"metadata": {},
"outputs": [],
"source": [
"rec = rr.new_recording(\"rerun_example_cube_flat\")\n",
"rec = rr.RecordingStream(\"rerun_example_cube_flat\")\n",
"\n",
"bp = rrb.Blueprint(collapse_panels=True)\n",
"\n",
Expand Down Expand Up @@ -404,7 +404,7 @@
"metadata": {},
"outputs": [],
"source": [
"rec = rr.new_recording(\"rerun_example_multi_recording\", recording_id=uuid.uuid4())\n",
"rec = rr.RecordingStream(\"rerun_example_multi_recording\", recording_id=uuid.uuid4())\n",
"\n",
"flat_grid = build_color_grid(20, 20, 1, twist=0)\n",
"rec.log(\"flat_grid\", rr.Points3D(flat_grid.positions, colors=flat_grid.colors, radii=0.5))\n",
Expand All @@ -416,19 +416,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "40354733-7feb-45fe-8ba6-dbbdbc070983",
"metadata": {},
"outputs": [],
"source": [
"rec = rr.new_recording(\"rerun_example_multi_recording\", recording_id=uuid.uuid4())\n",
"rec = rr.RecordingStream(\"rerun_example_multi_recording\", recording_id=uuid.uuid4())\n",
"\n",
"viewer.add_recording(rec)\n",
"\n",
"STEPS = 100\n",
"twists = math.pi * np.sin(np.linspace(0, math.tau, STEPS)) / 4\n",
"for t in range(STEPS):\n",
" rr.set_time(\"step\", sequence=t)\n",
" rec.set_time(\"step\", sequence=t)\n",
" cube = build_color_grid(10, 10, 10, twist=twists[t])\n",
" rec.log(\"cube\", rr.Points3D(cube.positions, colors=cube.colors, radii=0.5))"
]
Expand All @@ -450,12 +450,12 @@
"metadata": {},
"outputs": [],
"source": [
"viewer = rr.notebook.Viewer(use_global_recording=False)\n",
"viewer = rr.notebook.Viewer()\n",
"viewer.display()\n",
"\n",
"recordings = [\n",
" rr.new_recording(\"rerun_example_time_ctrl\", recording_id=\"example_a\"),\n",
" rr.new_recording(\"rerun_example_time_ctrl\", recording_id=\"example_b\"),\n",
" rr.RecordingStream(\"rerun_example_time_ctrl\", recording_id=\"example_a\"),\n",
" rr.RecordingStream(\"rerun_example_time_ctrl\", recording_id=\"example_b\"),\n",
"]\n",
"\n",
"rec_colors = {\"example_a\": [0, 255, 0], \"example_b\": [255, 0, 0]}\n",
Expand All @@ -465,10 +465,10 @@
"\n",
"STEPS = 100\n",
"twists = math.pi * np.sin(np.linspace(0, math.tau, STEPS)) / 4\n",
"for t in range(STEPS):\n",
" cube = build_color_grid(10, 10, 10, twist=twists[t])\n",
" for rec in recordings:\n",
" rr.set_time(\"step\", sequence=t)\n",
"for rec in recordings:\n",
" for t in range(STEPS):\n",
" cube = build_color_grid(10, 10, 10, twist=twists[t])\n",
" rec.set_time(\"step\", sequence=t)\n",
" rec.log(\"cube\", rr.Points3D(cube.positions, colors=rec_colors[rec.get_recording_id()], radii=0.5))"
]
},
Expand All @@ -482,7 +482,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 22,
"id": "3d6804d6",
"metadata": {},
"outputs": [],
Expand All @@ -500,7 +500,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 23,
"id": "30d0107b",
"metadata": {},
"outputs": [],
Expand Down
39 changes: 22 additions & 17 deletions rerun_py/rerun_sdk/rerun/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __init__(
url: str | None = None,
blueprint: BlueprintLike | None = None,
recording: RecordingStream | None = None,
use_global_recording: bool = True,
use_global_recording: bool | None = None,
) -> None:
"""
Create a new Rerun viewer widget for use in a notebook.
Expand Down Expand Up @@ -115,10 +115,12 @@ def __init__(
Setting this is equivalent to calling [`rerun.send_blueprint`][] before initializing the viewer.
use_global_recording:
Whether or not the Viewer should default to the global recording in case no explicit `recording`
is specified.
If no explicit `recording` is provided, the Viewer uses the thread-local/global recording created by `rr.init`
or set explicitly via `rr.set_thread_local_data_recording`/`rr.set_global_data_recording`.
If this is set to `False`, then `blueprint` is ignored.
Settings this to `False` causes the Viewer to not pick up the global recording.
Defaults to `False` if `url` is provided, and `True` otherwise.
"""

Expand Down Expand Up @@ -150,24 +152,27 @@ def __init__(
url=url,
)

# By default, we use the global recording only if no `url` is provided.
if use_global_recording is None:
use_global_recording = url is None

if use_global_recording:
recording = get_data_recording(recording)
if recording is None:
if url is None:
raise ValueError("No recording or url specified and no active recording found")
elif blueprint is not None:
raise ValueError(
"Can only set a blueprint if there's either an active recording or a recording passed in"
)

if recording is not None:
bindings.set_callback_sink(
recording=recording.to_native(),
callback=self._flush_hook,
)

if blueprint is not None:
if recording is not None:
recording.send_blueprint(blueprint)
else:
bindings.set_callback_sink(
recording=recording.to_native(),
callback=self._flush_hook,
raise ValueError(
"Can only set a blueprint if there's either an active recording or a recording passed in"
)

if blueprint is not None:
recording.send_blueprint(blueprint)

def add_recording(
self,
recording: RecordingStream | None = None,
Expand Down
Loading