How to dynamically change the color of plotted 3D image using streamlit and pyvista

Summary

I am able to build a 3D image using Streamlit, but when i try to change the color , it is not getting changed in the webpage.

Below is the code I tried.

import streamlit as st
import matplotlib.pyplot as plt
import pyvista as pv
from stpyvista import stpyvista
import mimetypes


# Define the initial color of the mesh
mesh_color = 'red'

# Define the colors that the user can choose from
color_options = ['purple','red', 'blue', 'green', 'yellow']

# Display a dropdown menu to allow the user to select a color
new_color = st.selectbox('Select a mesh color', color_options)

# Update the color of the mesh based on the user's selection
mesh_color = new_color

pv.global_theme.show_scalar_bar = False 
plotter = pv.Plotter(
        border=True,
        window_size=[580,400]) 
plotter.background_color = "white"

reader = pv.STLReader("D:projects//cub.stl")

mesh = reader.read()
plotter.add_mesh(mesh,color=mesh_color)

plotter.background_color = "white"
plotter.view_isometric()

# Pass a key to avoid re-rendering at each time something changes in the page
stpyvista(plotter, key="pv_cube")
st.pyvista(pv.show())

Try not passing a key to the component :wink:

Also, check the Pass_a_key page in the demo:
https://stpyvista.streamlit.app

1 Like

Thank you very much @edsaac for the reply. The solution you have provided helped.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.