Skip to content

edsaac/stpyvista

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧊 stpyvista

Show PyVista visualizations in Streamlit.

stpyvista examples and documentation

Github Repo Check it at PyPI Github tests repo

Streamlit Cloud

Take a PyVista plotter object and show it on Streamlit as an interactive-ish component (as in it can be zoomed in/out, moved and rotated, but the visualization state is not returned). It uses Panel to render PyVista plotter objects within an iframe.


πŸ“¦ Installation

pip install stpyvista

πŸ“š Demo and documentation

stpyvista examples and documentation


✨ Minimal example

Render a cube
import streamlit as st
import pyvista as pv
from stpyvista import stpyvista

## Initialize a plotter object
plotter = pv.Plotter(window_size=[400,400])

## Create a mesh with a cube 
mesh = pv.Cube(center=(0,0,0))

## Add some scalar field associated to the mesh
mesh['my_scalar'] = mesh.points[:, 2] * mesh.points[:, 0]

## Add mesh to the plotter
plotter.add_mesh(mesh, scalars='my_scalar', cmap='bwr')

## Final touches
plotter.view_isometric()
plotter.add_scalar_bar()
plotter.background_color = 'white'

## Pass a key to avoid re-rendering at each page change
stpyvista(plotter, key="pv_cube")

🎈 Deploying to Streamlit Community Cloud

  • By default, Community Cloud will run Python 3.8, make sure to deploy using Python 3.10 or 3.11 (New App β†’ Advanced settings... β†’ Python version).

  • Add stpyvista to the requirements.txt file.

  • Install procps, libgl1-mesa-glx and xvfb by adding them to the packages.txt file.

  • The Community Cloud is a headless machine and Pyvista requires a virtual framebuffer to work. stpyvista.utils.start_xvfb checks if Xvfb is running and starts it if it was not.

    from stpyvista.utils import start_xvfb
    
    if "IS_XVFB_RUNNING" not in st.session_state:
      start_xvfb()
      st.session_state.IS_XVFB_RUNNING = True 

🚩 Known issues

  • NSInternalInconsistencyException thrown when running on macOS. Current solution is to deploy using a VM.
  • RuntimeError thrown when running Python 3.9. Upgrading to panel>=1.4.0 fixes this issue. Alternatively, run using Python 3.10 or higher.
  • Buggy colormaps when using panel==1.4.0. This issue was fixed with panel==1.4.1.
  • cmocean==4.0.1 will raise an exception while trying to register its colormaps to matplotlib, making PyVista to crash. This issue was fixed in cmocean==4.0.3.

🍏 Also check