stpyvista: Show PyVista 3D visualizations in Streamlit.
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 Panel’s VTK implementation and it basically takes the PyVista plotter, exports it to HTML and displays that within an iframe.
Installation:
pip install stpyvista
Demos:
Display your own STL file on Streamlit:
Physically Based Rendering (PBR):
Usage example:
import pyvista as pv
import streamlit as st
from stpyvista import stpyvista
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')
## Final touches
plotter.view_isometric()
plotter.background_color = 'white'
## Send to streamlit
stpyvista(plotter, key="pv_cube")
Yes, all the formats supported by pyvista should be rendered by stpyvista. For creating a StructuredGrid from scratch:
Code:
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")
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:
Code:
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)
p.view_isometric()
p.set_background("white")
stpyvista(p)
import streamlit as st
import pyvista as pv
from stpyvista import stpyvista
## Initialize a plotter object
plotter = pv.Plotter(window_size=[300,600])
## Create a mesh with a cube
for i in range(1, 11):
cube = pv.Cube(center=(0, 0, i), x_length=2, y_length=2, z_length=0.8)
plotter.add_mesh(cube, edge_color='black', color='purple', opacity=i/10, show_edges=True)
## Final touches
plotter.background_color = '#dddddd'
plotter.view_isometric()
## Send to streamlit
"# đź—Ľ Opacity tower"
stpyvista(plotter, horizontal_align='left')
Panning is done by dragging while pressing the shift key. I am not aware of a way to reset to the initial view, other than re-rendering the element, although there is the option to set an interactive_orientation_widget = True to reset the view to a plane, i.e., xy, yz, etc.
That is one of the things that panel does not support. Axes must be passed directly as a dictionary of tickers. Check the docs here VTK — Panel v1.1.0 for details.
That’s a bug on panel’s side so will have to look for a workaround, like exporting the colors and limits of the pyvista colorbar and put them in an independent image.
Hi there! In the initial post it was explicitely mentioned that visualization state is not returned. Is there a way to still get it?
At the moment, I am working on an app that generates aircraft configurations based on certain inputs and displays them via stpyvista in the app. I would like the visualization state to be returned such that I can store it in session_state.
In this way, I am hoping that the last user defined camera position is shown again once the inputs are changed. Thanks in advance for your help!
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.