Create workspace grid layouts#

This example demonstrates how to create and manage workspace layouts with various grid configurations in PyResultExplorer:

  • Workspace creation with custom grid dimensions using rows and columns.

  • Grid layout configurations from 1x1 to 3x3 grid arrangements.

  • Layout descriptions showing where each viewport is positioned.

  • Viewport positions with row and column grid coordinates.

This example showcases the flexibility of Result Explorer’s workspace layout system for organizing and visualizing multiple result sets simultaneously.

Import the Result Explorer dependencies.

from ansys.result_explorer.core import (
    Component,
    Field,
    Location,
    PlotDefinition,
    ResultFieldName,
    ResultType,
    ViewportPlacement,
    launch_result_explorer,
)
from ansys.result_explorer.core.examples import ExampleKeys, get_example_file
from ansys.result_explorer.core.models import ViewportDirection

Launch Result Explorer#

Start a Result Explorer instance for this example.

rx = launch_result_explorer()

# Create a solution
rst_path = get_example_file(ExampleKeys.RST_MULTIPLE_CONNECTIONS)
sol = rx.create_solution(
    name="Example Solution",
    file_path=rst_path,
)

Create workspace grid layouts#

Create workspaces with various grid configurations from 1x1 to 3x3 and inspect the viewports in each layout.

for r in range(1, 4):
    for c in range(1, 4):
        workspace = rx.create_workspace(name=f"{c}x{r} Grid Workspace", rows=r, cols=c)
        print(f"Created workspace: {workspace.name} (id: {workspace.id})")
        viewports = workspace.viewports
        print(f"Viewport count: {len(viewports)}")
        print(workspace.layout)
        print("-" * 40)
Created workspace: 1x1 Grid Workspace (id: 86953726-8b32-4c97-8090-2618293993c3)
Viewport count: 1
+---------------------+
| [0] r0c0            |
|                     |
|                     |
+---------------------+
[0] 592996de-fd8d-4625-9924-9b2ed9c0665c (row 0, column 0, 1x1 cells)
----------------------------------------
Created workspace: 2x1 Grid Workspace (id: 65ddec93-1048-4375-b31a-4c09e0a15f53)
Viewport count: 2
+---------------------+---------------------+
| [0] r0c0            | [1] r0c1            |
|                     |                     |
|                     |                     |
+---------------------+---------------------+
[0] 01b68ee3-a982-43b9-a9a3-5607f020123a (row 0, column 0, 1x1 cells)
[1] 69716fa5-e21d-4509-b897-4bf777fd8b91 (row 0, column 1, 1x1 cells)
----------------------------------------
Created workspace: 3x1 Grid Workspace (id: c9deb359-f16e-4b0c-9a04-7acb01e246c9)
Viewport count: 3
+---------------------+---------------------+---------------------+
| [0] r0c0            | [1] r0c1            | [2] r0c2            |
|                     |                     |                     |
|                     |                     |                     |
+---------------------+---------------------+---------------------+
[0] 5d01c6e3-8974-40a5-99f5-f46228acc748 (row 0, column 0, 1x1 cells)
[1] 5093b4a3-4007-458f-ba60-54fd4bd0b246 (row 0, column 1, 1x1 cells)
[2] 66cf3256-6216-41d8-9f27-1c08d1df7ae3 (row 0, column 2, 1x1 cells)
----------------------------------------
Created workspace: 1x2 Grid Workspace (id: 290a25d7-947e-4ed3-82c6-eae09a8c5064)
Viewport count: 2
+---------------------+
| [0] r0c0            |
|                     |
|                     |
+---------------------+
| [1] r1c0            |
|                     |
|                     |
+---------------------+
[0] 13a1a9cb-fbae-4418-ad07-e45581f9668f (row 0, column 0, 1x1 cells)
[1] a573a479-6155-490d-b33d-4b988cb0eefc (row 1, column 0, 1x1 cells)
----------------------------------------
Created workspace: 2x2 Grid Workspace (id: 5e269a76-25f9-4a6a-895a-d4da05c3988f)
Viewport count: 4
+---------------------+---------------------+
| [0] r0c0            | [1] r0c1            |
|                     |                     |
|                     |                     |
+---------------------+---------------------+
| [2] r1c0            | [3] r1c1            |
|                     |                     |
|                     |                     |
+---------------------+---------------------+
[0] a60a79ad-3dd7-4c74-885b-48906c437c96 (row 0, column 0, 1x1 cells)
[1] 4bf930a1-b51d-44a1-bef6-b59c29311e80 (row 0, column 1, 1x1 cells)
[2] 23a51c06-2d57-4f06-a84d-4ed155b1ea18 (row 1, column 0, 1x1 cells)
[3] ad573c50-b4b3-4abd-aa3f-3118e291f35a (row 1, column 1, 1x1 cells)
----------------------------------------
Created workspace: 3x2 Grid Workspace (id: 596925dc-5ed9-41c1-b77d-314294a0f9e6)
Viewport count: 6
+---------------------+---------------------+---------------------+
| [0] r0c0            | [1] r0c1            | [2] r0c2            |
|                     |                     |                     |
|                     |                     |                     |
+---------------------+---------------------+---------------------+
| [3] r1c0            | [4] r1c1            | [5] r1c2            |
|                     |                     |                     |
|                     |                     |                     |
+---------------------+---------------------+---------------------+
[0] 82714cc9-9382-412b-9745-46ab9de286ed (row 0, column 0, 1x1 cells)
[1] dad3aa39-f4d3-4f92-ade0-a74266f2a329 (row 0, column 1, 1x1 cells)
[2] 8acf3d12-f52a-4e7f-bd8c-ba9880552309 (row 0, column 2, 1x1 cells)
[3] a4b8b3e9-9ceb-4c90-bc12-772c61b05e82 (row 1, column 0, 1x1 cells)
[4] c78e2cac-d1b0-4737-87df-5b9536de9fc3 (row 1, column 1, 1x1 cells)
[5] f6e25d00-2e6f-4ad1-adde-54f8cad22714 (row 1, column 2, 1x1 cells)
----------------------------------------
Created workspace: 1x3 Grid Workspace (id: 301955bd-2d6f-47c3-bac2-736ab6e2abf4)
Viewport count: 3
+---------------------+
| [0] r0c0            |
|                     |
|                     |
+---------------------+
| [1] r1c0            |
|                     |
|                     |
+---------------------+
| [2] r2c0            |
|                     |
|                     |
+---------------------+
[0] 2504dc05-0b2e-46c5-94f5-2ae7f574482b (row 0, column 0, 1x1 cells)
[1] b5fa2365-8750-4c9f-9053-128c6bd09555 (row 1, column 0, 1x1 cells)
[2] e6db97c2-cacf-4862-acad-6b77c2dd8e59 (row 2, column 0, 1x1 cells)
----------------------------------------
Created workspace: 2x3 Grid Workspace (id: 14a2f534-f788-4310-b8c6-01c6de9b90b7)
Viewport count: 6
+---------------------+---------------------+
| [0] r0c0            | [1] r0c1            |
|                     |                     |
|                     |                     |
+---------------------+---------------------+
| [2] r1c0            | [3] r1c1            |
|                     |                     |
|                     |                     |
+---------------------+---------------------+
| [4] r2c0            | [5] r2c1            |
|                     |                     |
|                     |                     |
+---------------------+---------------------+
[0] c10906b3-301c-4ffa-8131-6f7ff66af756 (row 0, column 0, 1x1 cells)
[1] 5040c5de-e45a-4a59-8f8b-7bd837ff8d7a (row 0, column 1, 1x1 cells)
[2] 34d5cc36-4b90-424d-95d9-061cd045fffa (row 1, column 0, 1x1 cells)
[3] 9aed30ba-e641-4592-be8b-5eaeb58aa49a (row 1, column 1, 1x1 cells)
[4] e1fe2ac4-422f-4e68-a12f-8de2b8f09c1f (row 2, column 0, 1x1 cells)
[5] 451ed16d-3a72-4417-abbc-fae35912d102 (row 2, column 1, 1x1 cells)
----------------------------------------
Created workspace: 3x3 Grid Workspace (id: 7ed12b06-0dfa-4339-9589-7eb45ab50b28)
Viewport count: 9
+---------------------+---------------------+---------------------+
| [0] r0c0            | [1] r0c1            | [2] r0c2            |
|                     |                     |                     |
|                     |                     |                     |
+---------------------+---------------------+---------------------+
| [3] r1c0            | [4] r1c1            | [5] r1c2            |
|                     |                     |                     |
|                     |                     |                     |
+---------------------+---------------------+---------------------+
| [6] r2c0            | [7] r2c1            | [8] r2c2            |
|                     |                     |                     |
|                     |                     |                     |
+---------------------+---------------------+---------------------+
[0] 52aa5bd2-6747-4163-9744-fd208f509a2f (row 0, column 0, 1x1 cells)
[1] 0d4b8f8b-0127-4fa6-9fe4-79147aed596e (row 0, column 1, 1x1 cells)
[2] d7a29441-cea5-46cb-9d8c-6f177c694360 (row 0, column 2, 1x1 cells)
[3] b1ed338f-caf9-46db-a0f6-f4acb73b49fd (row 1, column 0, 1x1 cells)
[4] 6e810cfe-ed6d-4196-b333-119b4cbd9945 (row 1, column 1, 1x1 cells)
[5] 49dab86c-17df-4e31-844b-f16df8e43b55 (row 1, column 2, 1x1 cells)
[6] 59cf4fa9-c388-449f-b561-1233b5038275 (row 2, column 0, 1x1 cells)
[7] d26d7ebe-0d35-42f5-b53f-e2cc848cece2 (row 2, column 1, 1x1 cells)
[8] 84707a98-fce8-4a31-bcaf-da87aca7c0c5 (row 2, column 2, 1x1 cells)
----------------------------------------

Describe where the viewports are#

workspace.layout describes the arrangement as a grid of rows and columns. Printing it shows a diagram of the current positions.

workspace = rx.create_workspace(name="Comparison Workspace", rows=2, cols=3)
print(workspace.layout)
+---------------------+---------------------+---------------------+
| [0] r0c0            | [1] r0c1            | [2] r0c2            |
|                     |                     |                     |
|                     |                     |                     |
+---------------------+---------------------+---------------------+
| [3] r1c0            | [4] r1c1            | [5] r1c2            |
|                     |                     |                     |
|                     |                     |                     |
+---------------------+---------------------+---------------------+
[0] ed2f198d-6dd9-4ec2-b773-40058725f10f (row 0, column 0, 1x1 cells)
[1] 3593f41c-b67f-4fc2-9e48-9dd5d1c9b2ab (row 0, column 1, 1x1 cells)
[2] b367b13d-e27c-41cc-862e-ebf77a439dbd (row 0, column 2, 1x1 cells)
[3] 3fad14f2-52b9-412b-8be8-7a05ad240b8a (row 1, column 0, 1x1 cells)
[4] b4e9857a-d116-4252-b25f-0687eae16f38 (row 1, column 1, 1x1 cells)
[5] 49608012-e311-437b-92e4-7d27455b3a66 (row 1, column 2, 1x1 cells)

Assign plots by grid position#

Create a plot named after each grid position, then use viewport_at() to assign it to the matching viewport. Rows are ordered from top to bottom and columns from left to right.

def create_named_plot(name: str):
    """Create a displacement plot with the given name."""
    return sol.create_plot(
        PlotDefinition(
            name=name,
            result_type=ResultType.displacement,
            location=Location.nodal,
            fields=[
                Field(
                    name=ResultFieldName.displacement,
                    components=[Component.X, Component.Y, Component.Z],
                )
            ],
        )
    )


rows, cols = workspace.layout.grid_shape
for row_index in range(rows):
    for column_index in range(cols):
        plot_view = create_named_plot(f"Row {row_index}, Column {column_index}")
        viewport = workspace.viewport_at(row=row_index, column=column_index)
        viewport.set_view(plot_view, wait=True)
        print(f"Assigned {plot_view.name} to viewport {viewport.id}")
Assigned Row 0, Column 0 to viewport ed2f198d-6dd9-4ec2-b773-40058725f10f
Assigned Row 0, Column 1 to viewport 3593f41c-b67f-4fc2-9e48-9dd5d1c9b2ab
Assigned Row 0, Column 2 to viewport b367b13d-e27c-41cc-862e-ebf77a439dbd
Assigned Row 1, Column 0 to viewport 3fad14f2-52b9-412b-8be8-7a05ad240b8a
Assigned Row 1, Column 1 to viewport b4e9857a-d116-4252-b25f-0687eae16f38
Assigned Row 1, Column 2 to viewport 49608012-e311-437b-92e4-7d27455b3a66

Describe a layout that is not a grid#

Viewports can also be split freely. A viewport that is not part of a regular grid spans several rows or columns, which the layout reports through row_span and column_span.

split_workspace = rx.create_workspace(name="Split Workspace")
left = split_workspace.viewports[0]
right_top = split_workspace.create_viewport(left, ViewportDirection.VIEWPORT_DIRECTION_RIGHT)
split_workspace.create_viewport(right_top, ViewportDirection.VIEWPORT_DIRECTION_BOTTOM)

layout = split_workspace.layout
print(layout)
print(f"Grid shape: {layout.grid_shape}, regular grid: {layout.is_grid}")

left_placement = layout.placement_of(left)
print(f"The left viewport spans {left_placement.row_span} rows.")
+---------------------+---------------------+
| [0] r0c0            | [1] r0c1            |
|                     |                     |
|                     |                     |
|                     +---------------------+
|                     | [2] r1c1            |
|                     |                     |
|                     |                     |
+---------------------+---------------------+
[0] 7ea079d0-353f-4a65-9c26-b57dd9c58ab3 (row 0, column 0, 2x1 cells)
[1] 18b3a06d-8b7e-4744-ab67-b44697edd65d (row 0, column 1, 1x1 cells)
[2] 614d9bd7-e3aa-439e-9169-f0955988e2c7 (row 1, column 1, 1x1 cells)
Grid shape: (2, 2), regular grid: False
The left viewport spans 2 rows.

Assign a view named after the cells that each viewport occupies, so that the reported positions can be checked against the application window.

def span_text(label: str, start: int, span: int) -> str:
    """Describe the cells occupied along one direction."""
    if span == 1:
        return f"{label} {start}"
    return f"{label}s {start}-{start + span - 1}"


def position_name(placement: ViewportPlacement) -> str:
    """Describe the position of a placement."""
    return (
        f"{span_text('Row', placement.row, placement.row_span)}, "
        f"{span_text('Column', placement.column, placement.column_span)}"
    )


for placement in layout.placements:
    plot_view = create_named_plot(position_name(placement))
    placement.viewport.set_view(plot_view, wait=True)
    print(f"Assigned {plot_view.name} to viewport {placement.viewport.id}")

rx.stop()
Assigned Rows 0-1, Column 0 to viewport 7ea079d0-353f-4a65-9c26-b57dd9c58ab3
Assigned Row 0, Column 1 to viewport 18b3a06d-8b7e-4744-ab67-b44697edd65d
Assigned Row 1, Column 1 to viewport 614d9bd7-e3aa-439e-9169-f0955988e2c7

Total running time of the script: (0 minutes 25.785 seconds)

Gallery generated by Sphinx-Gallery