🧊 stpyvista: Show PyVista 3D visualizations in Streamlit

:ice_cube: stpyvista: Show PyVista 3D visualizations in Streamlit.

Github Repo PyPi version Github tests repo

Streamlit App

This is a simple component that takes a PyVista plotter object and shows it on Streamlit as an interactive element (as in it can be zoomed in/out, moved and rotated, but the visualization state is not returned). It uses PyVista’s pythreejs backend and it basically takes the plotter, exports it to HTML (https://docs.pyvista.org/api/plotting/_autosummary/pyvista.Plotter.export_html.html) and displays that within an iframe.


pip install stpyvista 


:outbox_tray: Display your own STL file on Streamlit:


Streamlit App

:bread: Physically Based Rendering (PBR):


Streamlit App

Usage example:

import pyvista as pv
import streamlit as st
from stpyvista import stpyvista

# ipythreejs does not support scalar bars :(
pv.global_theme.show_scalar_bar = False

st.title("A cube")
st.info("""Code adapted from https://docs.pyvista.org/user-guide/jupyter/pythreejs.html#scalars-support""")

## 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['myscalar'] = mesh.points[:, 2]*mesh.points[:, 0]

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

## Final touches
plotter.background_color = 'white'

## Send to streamlit
stpyvista(plotter, key="pv_cube")


Also check:


Great job @edsaac !

Feel free to add it to the Components Tracker: Streamlit Components - Community Tracker so we don’t lose track of it!

Have a nice day,

Really cool! Well done!

Very nice work.

I have an curved sufrace object. Can I use the stpyvista to show curvsurf object on streamlit?

curvsurf = pv.StructuredGrid(xx, yy, zz)

Have a nice day,

Yes, all the formats supported by pyvista should be rendered by stpyvista. For creating a StructuredGrid from scratch:



import streamlit as st
import pyvista as pv
import numpy as np
from stpyvista import stpyvista

"# 🧱 Structured grid"

## Create coordinate data
x = np.arange(-10, 10, 0.25)
y = np.arange(-10, 10, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))

## Set up plotter
plotter = pv.Plotter(window_size=[600,600])
surface = pv.StructuredGrid(x, y, z)
plotter.add_mesh(surface, color='teal', show_edges=True)

## Pass the plotter (not the mesh) to stpyvista
stpyvista(plotter, key="surface")
1 Like

Thanks so much. Your stpyvista is really awesome!

really love it. For some reason the Slider widget is not working in streamlit, i used the example from Slider Bar Widget — PyVista 0.38.5 documentation and passed it into stpyvista. I just get the Plot but without the slider.

stpyvista takes the pyvista plotter and converts it to a panel object that is embeded within streamlit. My guess is that those sliders are not supported by panel and get stripped away, You can use regular streamlit sliders to achieve a similar behavior:



import streamlit as st
import pyvista as pv
from stpyvista import stpyvista

res = st.slider("Resolution", 5, 100, 20, 5)

p = pv.Plotter(window_size=[300,300])
sphere = pv.Sphere(phi_resolution=res, theta_resolution=res)
p.add_mesh(sphere, name='sphere', show_edges=True)


love it, thank you!