Skip to content

Commit 81cb02b

Browse files
committed
Improve embedding docs
1 parent 6b9c8c8 commit 81cb02b

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

docs/source/embedded_visualizations.rst

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ Step 1: Exporting Scene State
1414
----------------------------
1515

1616
You can export static or dynamic 3D data from a Viser scene using the scene
17-
serializer. :func:`ViserServer.get_scene_serializer` returns a serializer
17+
serializer. :meth:`ViserServer.get_scene_serializer` returns a serializer
1818
object that can serialize the current scene state to a binary format.
1919

2020
Static Scene Export
2121
~~~~~~~~~~~~~~~~~~~
2222

23-
For static 3D visualizations, use the following code to save the scene state:
23+
For static 3D visualizations, :meth:`StateSerializer.serialize` can be used to
24+
save the scene state:
2425

2526
.. code-block:: python
2627
@@ -40,7 +41,9 @@ For static 3D visualizations, use the following code to save the scene state:
4041
Path("recording.viser").write_bytes(data)
4142
4243
43-
As a suggestion, you can also add a button for exporting the scene state:
44+
As a suggestion, you can also add a button for exporting the scene state.
45+
Clicking the button in your web browser wil trigger a download of the
46+
``.viser`` file.
4447

4548
.. code-block:: python
4649
@@ -65,7 +68,8 @@ As a suggestion, you can also add a button for exporting the scene state:
6568
Dynamic Scene Export
6669
~~~~~~~~~~~~~~~~~~~~
6770

68-
For dynamic visualizations with animation, you can create a "3D video" by inserting sleep commands between frames:
71+
For dynamic visualizations with animation, you can create a "3D video" by
72+
calling :meth:`StateSerializer.insert_sleep` between frames:
6973

7074
.. code-block:: python
7175

docs/source/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ URL (default: `http://localhost:8080`).
5050
./server.md
5151
./scene_api.md
5252
./gui_api.md
53+
./state_serializer.md
5354

5455

5556
.. toctree::

docs/source/state_serializer.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# State Serializer
2+
3+
<!-- prettier-ignore-start -->
4+
5+
.. autoclass:: viser.infra.StateSerializer
6+
:members:
7+
:undoc-members:
8+
:inherited-members:
9+
10+
<!-- prettier-ignore-end -->

src/viser/infra/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"""
1313

1414
from ._infra import ClientId as ClientId
15+
from ._infra import StateSerializer as StateSerializer
1516
from ._infra import WebsockClientConnection as WebsockClientConnection
1617
from ._infra import WebsockMessageHandler as WebsockMessageHandler
1718
from ._infra import WebsockServer as WebsockServer

src/viser/infra/_infra.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class _ClientHandleState:
4343

4444
class StateSerializer:
4545
"""Handle for serializing messages. In Viser, this is used to save the
46-
scene state."""
46+
scene state so it can be shared/embedded in static webpages."""
4747

4848
def __init__(
4949
self, handler: WebsockMessageHandler, filter: Callable[[Message], bool]
@@ -63,14 +63,17 @@ def _insert_message(self, message: Message) -> None:
6363
self._messages.append((self._time, message.as_serializable_dict()))
6464

6565
def insert_sleep(self, duration: float) -> None:
66-
"""Insert a sleep into the recorded file."""
66+
"""Insert a sleep into the recorded file. This can be useful for
67+
dynamic 3D data."""
6768
assert (
6869
self._handler._record_handle is not None
6970
), "serialize() was already called!"
7071
self._time += duration
7172

7273
def serialize(self) -> bytes:
73-
"""Serialize saved messages. Should only be called once.
74+
"""Serialize saved messages. Should only be called once. Our convention
75+
is to write this binary format to a file with a ``.viser`` extension,
76+
for example via ``pathlib.Path("file.viser").write_bytes(...)``.
7477
7578
Returns:
7679
The recording as bytes.

0 commit comments

Comments
 (0)