Skip to content

Commit

Permalink
Add support for active_tab (rerun-io#5499)
Browse files Browse the repository at this point in the history
### What
Referring to an object created in the same constructor is somewhat
awkward.

Forcing users to create the object out-of-line so they can assign it to
a variable and then refer to it is just a pain.
Specifying a name or index seems like a convenient middle ground.


Example
```python
            Tabs(
                BarChartView(name="Bar Chart", origin="/bar_chart", contents=["/bar_chart/**"]),
                TimeSeriesView(name="Curves", origin="/curves", contents=["/curves/**"]),
                TimeSeriesView(name="Trig", origin="/trig", contents=["/trig/**"]),
                TimeSeriesView(name="Classification", origin="/classification", contents=["/classification/**"]),
                active_tab="Curves",
            ),
```

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using newly built examples:
[app.rerun.io](https://app.rerun.io/pr/5499/index.html)
* Using examples from latest `main` build:
[app.rerun.io](https://app.rerun.io/pr/5499/index.html?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[app.rerun.io](https://app.rerun.io/pr/5499/index.html?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/5499)
- [Docs
preview](https://rerun.io/preview/4c45a10b77cb3dd4794ca2d2ac8267e3d9230606/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/4c45a10b77cb3dd4794ca2d2ac8267e3d9230606/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)
  • Loading branch information
jleibs authored Mar 14, 2024
1 parent 045923a commit 9977717
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
14 changes: 13 additions & 1 deletion rerun_py/rerun_sdk/rerun/blueprint/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def __init__(
column_shares: Optional[ColumnShareArrayLike] = None,
row_shares: Optional[RowShareArrayLike] = None,
grid_columns: Optional[int] = None,
active_tab: Optional[int | str] = None,
):
"""
Construct a new container.
Expand All @@ -155,6 +156,8 @@ def __init__(
This is only applicable to `Vertical` or `Grid` containers.
grid_columns
The number of columns in the grid. This is only applicable to `Grid` containers.
active_tab
The active tab in the container. This is only applicable to `Tabs` containers.
"""
self.id = uuid.uuid4()
Expand All @@ -163,6 +166,7 @@ def __init__(
self.column_shares = column_shares
self.row_shares = row_shares
self.grid_columns = grid_columns
self.active_tab = active_tab

def blueprint_path(self) -> str:
"""
Expand All @@ -183,8 +187,15 @@ def to_blueprint(self) -> Blueprint:

def _log_to_stream(self, stream: RecordingStream) -> None:
"""Internal method to convert to an archetype and log to the stream."""
for sub in self.contents:
active_tab_path = None

for i, sub in enumerate(self.contents):
sub._log_to_stream(stream)
if i == self.active_tab or (isinstance(sub, SpaceView) and sub.name == self.active_tab):
active_tab_path = sub.blueprint_path()

if self.active_tab is not None and active_tab_path is None:
raise ValueError(f"Active tab '{self.active_tab}' not found in the container contents.")

arch = ContainerBlueprint(
container_kind=self.kind,
Expand All @@ -193,6 +204,7 @@ def _log_to_stream(self, stream: RecordingStream) -> None:
row_shares=self.row_shares,
visible=True,
grid_columns=self.grid_columns,
active_tab=active_tab_path,
)

stream.log(self.blueprint_path(), arch) # type: ignore[attr-defined]
Expand Down
6 changes: 4 additions & 2 deletions rerun_py/rerun_sdk/rerun/blueprint/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,16 @@ def __init__(
class Tabs(Container):
"""A tab container."""

def __init__(self, *contents: Container | SpaceView):
def __init__(self, *contents: Container | SpaceView, active_tab: Optional[int | str] = None):
"""
Construct a new tab container.
Parameters
----------
*contents:
All positional arguments are the contents of the container, which may be either other containers or space views.
active_tab:
The index or name of the active tab.
"""
super().__init__(*contents, kind=ContainerKind.Tabs)
super().__init__(*contents, kind=ContainerKind.Tabs, active_tab=active_tab)
7 changes: 5 additions & 2 deletions rerun_py/rerun_sdk/rerun/script_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def script_setup(
args: Namespace,
application_id: str,
recording_id: str | UUID | None = None,
blueprint: rr.blueprint.Blueprint | None = None,
) -> RecordingStream:
"""
Run common Rerun script setup actions. Connect to the viewer if necessary.
Expand All @@ -86,6 +87,8 @@ def script_setup(
processes to log to the same Rerun instance (and be part of the same recording),
you will need to manually assign them all the same recording_id.
Any random UUIDv4 will work, or copy the recording id for the parent process.
blueprint : Optional[rr.blueprint.Blueprint]
An optional blueprint to use for the viewer.
"""
rr.init(
Expand All @@ -106,11 +109,11 @@ def script_setup(
# Send logging data to separate `rerun` process.
# You can omit the argument to connect to the default address,
# which is `127.0.0.1:9876`.
rec.connect(args.addr) # type: ignore[attr-defined]
rec.connect(args.addr, blueprint=blueprint) # type: ignore[attr-defined]
elif args.save is not None:
rec.save(args.save) # type: ignore[attr-defined]
elif not args.headless:
rec.spawn() # type: ignore[attr-defined]
rec.spawn(blueprint=blueprint) # type: ignore[attr-defined]

return rec

Expand Down

0 comments on commit 9977717

Please sign in to comment.