Metadata-Version: 2.4
Name: streamlit-pyvista
Version: 0.0.26
Summary: A Streamlit component that allow support for new PyVista viewer backend: Trame.
Author-email: Maxime Rochat <rochat.max@gmail.com>
License-Expression: GPL-3.0-or-later
Project-URL: Homepage, https://gitlab.com/groups/dcsm
Project-URL: Repository, https://gitlab.com/dcsm/streamlit-pyvista
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: aiohttp>=3.9.3
Requires-Dist: pyvista>=0.44.2
Requires-Dist: vtk>=9.4.0
Requires-Dist: streamlit~=1.45.0
Requires-Dist: wslink>=2.5.6
Requires-Dist: trame<4,>=3.13.2
Requires-Dist: trame-client>=3.12.2
Requires-Dist: trame-plotly>=3.1.0
Requires-Dist: trame-server<4,>=3.12.2
Requires-Dist: trame-vtk>=2.11.15
Requires-Dist: trame-vuetify>=2.7.2
Requires-Dist: trame-pyvista>=0.2.1
Requires-Dist: protobuf<=4.25.3
Requires-Dist: pytest>=7.2.1
Requires-Dist: requests>=2.31.0
Requires-Dist: validators>=0.34.0
Requires-Dist: Flask>=3.0.3
Requires-Dist: APScheduler>=3.10.4
Requires-Dist: flask-sock>=0.7.0
Requires-Dist: websocket-client>=1.8.0
Requires-Dist: tqdm>=4.67.1
Provides-Extra: test
Requires-Dist: pytest>=7.4.3; extra == "test"
Requires-Dist: playwright>=1.43.0; extra == "test"
Requires-Dist: pytest-playwright>=0.4.4; extra == "test"
Requires-Dist: pixelmatch>=0.3.0; extra == "test"
Dynamic: license-file

# Streamlit PyVista
[![PyPI version](https://badge.fury.io/py/streamlit-pyvista.svg)](https://badge.fury.io/py/streamlit-pyvista)
[![Read the Docs](https://readthedocs.org/projects/streamlit_pyvista/badge/?version=latest)](http://streamlit-pyvista.readthedocs.io/)

A Streamlit component that allow support for new pyvista viewer backend : Trame.

# Installation instructions
````sh
pip install streamlit-pyvista
````
# Usage Instructions

````python
import streamlit as st
from streamlit_pyvista.mesh_viewer_component import MeshViewerComponent


def main():
    st.title("Simple Streamlit App With Pyvista Viewer")

    mesh_viewer = MeshViewerComponent("assets/plate_hole.vtu")
    mesh_viewer2 = MeshViewerComponent("assets/plate_hole.vtu")

    mesh_viewer.show()
    mesh_viewer2.show()


if __name__ == "__main__":
    main()
````
Note that instead of a file path it's also possible to pass an array of paths to display a sequence
of files. Only local file paths are supported.

This viewer leverage the capabilities of the new pyvista backend `Trame`, beyond  the nice design revamp,
it also supports remote rendering through trame's websocket based rendering.

# Mesh sequences and memory bounds

When an array of paths is passed, the viewer loads the sequence lazily to keep the server memory
bounded, whatever the number of frames:

- Only `PRELOADED_FRAMES` frames (20 by default, see the `STREAMLIT_PYVISTA_PRELOADED_FRAMES`
  environment variable) are loaded upfront, spread evenly over the whole sequence. The progress bar
  covers this pre-loading (e.g. "Loading mesh 3/20").
- The play button cycles through the pre-loaded frames only, so the animation is a set of jumps
  between evenly spread frames and never stalls on a load.
- Every frame stays reachable: navigating to a non pre-loaded frame (slider drag, stepping) loads it
  in the background while a spinner is shown, then displays it. Decimated versions are cached on
  disk and shared between users, so a frame is only decimated once per machine.
- At most `PRELOADED_FRAMES + 1` meshes are kept in memory: frames loaded on demand are unloaded
  least-recently-used first. Unloaded frames are read back from the cache (without re-decimation)
  when visited again, which keeps memory roughly `O(PRELOADED_FRAMES)` per viewer.

The scalar bar bounds are computed over the loaded frames only; with the default pre-loading they
cover 20 frames spread evenly over the sequence.

# Decimation

For large meshes (more than 6000 cells by default, see the `STREAMLIT_PYVISTA_DECIMATION_THRESHOLD` environment
variable), the viewer automatically computes a decimated version of the mesh ("thumbnail") that is displayed by
default to keep the interface fast. A checkbox in the viewer lets you switch to the original resolution: the full
mesh is then loaded in the background while a spinner is shown, so the interface never freezes.

Decimation relies on `pyvista.PolyData.decimate_pro` (vtkDecimatePro), which is fast and robust on all mesh types,
including large flat (planar) surface meshes. The deprecated `pyvista.PolyData.decimate` (vtkQuadricDecimation) is
intentionally not used: as of vtk 9.7.1 it can hang forever on flat meshes while flooding the log with "Unable to
factor linear system" warnings. vtk warnings are additionally muted while a mesh is being processed so that no
filter can flood the logs.

While the server is busy (loading meshes, decimating, computing scalar bounds) a spinner is displayed on top of the
plotter. During the pre-loading of a sequence of files, this spinner turns into a progress bar showing e.g.
"Loading mesh 3/20" (see "Mesh sequences and memory bounds" above).

# Composite meshes

Some formats (e.g. Exodus) are read by pyvista as a `pv.MultiBlock` composite holding the element block(s) plus empty
groups for the various sets (side sets, node sets, ...). Mesh files are therefore normalized on read: composites are
flattened into a single `UnstructuredGrid` containing the data of all non-empty blocks, so the rest of the package
(decimation, field selectors, scalar bounds) can treat every mesh uniformly.

# Field selectors

The scalar field dropdown (and the warp field dropdown) of the viewer list only the fields a user would want to look
at: internal vtk arrays and the bookkeeping arrays generated by file format readers (e.g. the Exodus reader's
`ObjectId`, `ElementBlockIds`, `QA_Records`, `Title`, `Info_Records`) are filtered out. The filter lives in
`filter_field_options` (`streamlit_pyvista/trame_viewers/trame_backend.py`), backed by the
`READER_METADATA_ARRAY_NAMES` set.

# Package documentation
You can find the automatic documentation generated with sphinx [here](https://streamlit-pyvista.readthedocs.io/)

# Workflow overview

This package has 3 different components:
- The Streamlit component
- The Trame viewers
- A "Server manager"

## Streamlit component
This pyvista new backend requires now to have its own server for each instance of a pyvista plotter. This means that the
streamlit component can be as simple as an iframe and displayed in the app and can use a simple api to communicate with
the trame server and the server manager

## Trame viewers
The Trame viewers are the servers that are embedded in the streamlit app, it has exposed api endpoints that allow the
component to communicate with the server for action such as loading a mesh in the viewer.

## Server manager
As said earlier, the trame viewers can only display one plotter at a time which means that if we want to display
multiple plotters we need multiple servers. The job of the Server manager is to control how many trame viewers exists,
if they need to killed or if they can be reused for other viewers

## Workflow
These 3 components interact the following manner:
1) A `MeshViewerComponent` is created and notify a `ServerManager` that it needs a viewer.
2) The `ServerManager` look if he have idling trame viewer anf if not, it start a new server.
3) The `ServerManager` specify the endpoints of the server that can be used to the `MeshViewerComponent`
4) The `MeshViewerComponent` directly communicate with the api of the trame viewer via the endpoints
specified by the `ServerManager`
