From 6b2671dae55d5a9dbb2d267ead7c01eabc2180d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Proch=C3=A1zka?= Date: Tue, 9 Jul 2024 19:29:36 +0200 Subject: [PATCH] Make default notebook widget size smaller and configurable (#6827) --- examples/python/notebook/cube.ipynb | 39 ++++++++++++++++++++++++++++ rerun_py/rerun_sdk/rerun/notebook.py | 38 +++++++++++++++++++++------ 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/examples/python/notebook/cube.ipynb b/examples/python/notebook/cube.ipynb index 07c8829fc69e..258e7aaec8e9 100644 --- a/examples/python/notebook/cube.ipynb +++ b/examples/python/notebook/cube.ipynb @@ -243,6 +243,45 @@ "rr.notebook_show(width=400, height=400)\n" ] }, + { + "cell_type": "markdown", + "id": "ff84c840", + "metadata": {}, + "source": [ + "To update the default width and height, you can use the `rerun.notebook.set_default_size` function.\n", + "\n", + "Note that you do not need to initialize a recording to use this function." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "81157021", + "metadata": {}, + "outputs": [], + "source": [ + "from rerun.notebook import set_default_size\n", + "\n", + "set_default_size(width=400, height=400)\n", + "\n", + "rr.init(\"rerun_example_cube\")\n", + "\n", + "small_cube = build_color_grid(3, 3, 3, twist=0)\n", + "rr.log(\"small_cube\", rr.Points3D(small_cube.positions, colors=small_cube.colors, radii=0.5))\n", + "\n", + "rr.notebook_show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "755957e0", + "metadata": {}, + "outputs": [], + "source": [ + "set_default_size(width=640, height=480)" + ] + }, { "cell_type": "markdown", "id": "a9812634-067f-4e07-95fb-cb9a506c42d3", diff --git a/rerun_py/rerun_sdk/rerun/notebook.py b/rerun_py/rerun_sdk/rerun/notebook.py index 23fc740e2df1..7aee3db9d81e 100644 --- a/rerun_py/rerun_sdk/rerun/notebook.py +++ b/rerun_py/rerun_sdk/rerun/notebook.py @@ -12,8 +12,30 @@ from .recording_stream import RecordingStream, get_data_recording -DEFAULT_WIDTH = 950 -DEFAULT_HEIGHT = 712 +_default_width = 640 +_default_height = 480 + + +def set_default_size(*, width: int | None, height: int | None) -> None: + """ + Set the default size for the viewer. + + This will be used for any viewers created after this call. + + Parameters + ---------- + width : int + The width of the viewer in pixels. + height : int + The height of the viewer in pixels. + + """ + + global _default_width, _default_height + if width is not None: + _default_width = width + if height is not None: + _default_height = height class Viewer: @@ -26,8 +48,8 @@ class Viewer: def __init__( self, *, - width: int = DEFAULT_WIDTH, - height: int = DEFAULT_HEIGHT, + width: int | None = None, + height: int | None = None, blueprint: BlueprintLike | None = None, recording: RecordingStream | None = None, ): @@ -74,8 +96,8 @@ def __init__( self._recording.send_blueprint(blueprint) # type: ignore[attr-defined] self._viewer = _Viewer( - width=width, - height=height, + width=width if width is not None else _default_width, + height=height if height is not None else _default_height, ) bindings.set_callback_sink( @@ -115,8 +137,8 @@ def _repr_keys(self): # type: ignore[no-untyped-def] def notebook_show( *, - width: int = DEFAULT_WIDTH, - height: int = DEFAULT_HEIGHT, + width: int | None = None, + height: int | None = None, blueprint: BlueprintLike | None = None, recording: RecordingStream | None = None, ) -> None: