-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from edsaac/custom_vtkjs
Custom vtkjs for interactive view
- Loading branch information
Showing
15 changed files
with
269 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build" | |
|
||
[project] | ||
name = "stpyvista" | ||
version = "0.0.16" | ||
version = "0.0.17" | ||
authors = [ | ||
{ name="Edwin Saavedra C.", email="[email protected]" }, | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
streamlit | ||
pyvista | ||
bokeh | ||
panel | ||
panel<1.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from trame.app import get_server | ||
from trame.widgets import ( | ||
vuetify as vuetify, | ||
) | ||
|
||
from trame.widgets.vtk import VtkLocalView | ||
from trame.ui.vuetify import SinglePageLayout | ||
|
||
from pyvista.plotting import Plotter | ||
|
||
SERVER_NAME = "stpyvista_server" | ||
|
||
|
||
async def export_vtksz(plotter: Plotter): | ||
"""Export this plotter as a VTK.js OfflineLocalView file. | ||
Parameters | ||
---------- | ||
plotter : Plotter | ||
PyVista Plotter object. | ||
Returns | ||
------- | ||
bytes | ||
The exported plotter view. | ||
""" | ||
|
||
# Get a trame server and launch it | ||
server = get_server(name=SERVER_NAME, client_type="vue2") | ||
_, ctrl = server.state, server.controller | ||
|
||
with SinglePageLayout(server) as layout: | ||
with layout.content: | ||
view = VtkLocalView(plotter.ren_win) | ||
ctrl.view_update = view.update | ||
|
||
server.start( | ||
exec_mode="task", | ||
host="127.0.0.1", | ||
port="0", | ||
open_browser=False, | ||
show_connection_info=False, | ||
disable_logging=True, | ||
timeout=0, | ||
backend="tornado", | ||
) | ||
|
||
content = view.export(format=format) | ||
view.release_resources() | ||
|
||
return content | ||
|
||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,3 +46,4 @@ def start_xvfb(): | |
f"{is_xvfb_running.stdout}" | ||
f"{is_xvfb_running.stderr}" | ||
) | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
function post_camera_state() { | ||
const camera = renderWindow.getRenderers()[1].getActiveCamera(); | ||
|
||
var cameraProperties = { | ||
position: camera.getPosition(), | ||
focal_point: camera.getFocalPoint(), | ||
up: camera.getViewUp(), | ||
view_angle: camera.getViewAngle(), | ||
clipping_range: camera.getClippingRange(), | ||
parallel_projection: camera.getParallelProjection() | ||
}; | ||
|
||
Streamlit.setComponentValue(JSON.stringify(cameraProperties)); | ||
} | ||
|
||
function onRender(event) { | ||
|
||
// Only run the render code the first time the component is loaded. | ||
if (!window.rendered) { | ||
|
||
// You most likely want to get the data passed in like this | ||
const { plotter_data, key } = event.detail.args; | ||
|
||
var container = document.querySelector('.content'); | ||
var base64Str = plotter_data; | ||
OfflineLocalView.load(container, { base64Str }); | ||
|
||
const interactor = renderWindow.getInteractor(); | ||
|
||
interactor.onLeftButtonRelease((event) => { | ||
post_camera_state() | ||
}); | ||
|
||
interactor.onMouseWheel((event) => { | ||
post_camera_state() | ||
}); | ||
window.rendered = true; | ||
Streamlit.setFrameHeight('300'); | ||
} | ||
} | ||
|
||
Streamlit.events.addEventListener(Streamlit.RENDER_EVENT, onRender); | ||
Streamlit.setComponentReady(); |
35 changes: 35 additions & 0 deletions
35
stpyvista/src/stpyvista/vanilla_vtkjs/streamlit-component-lib.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
// Borrowed minimalistic Streamlit API from Thiago | ||
// https://discuss.streamlit.io/t/code-snippet-create-components-without-any-frontend-tooling-no-react-babel-webpack-etc/13064 | ||
function sendMessageToStreamlitClient(type, data) { | ||
console.log(type, data) | ||
const outData = Object.assign({ | ||
isStreamlitMessage: true, | ||
type: type, | ||
}, data); | ||
window.parent.postMessage(outData, "*"); | ||
} | ||
|
||
const Streamlit = { | ||
setComponentReady: function() { | ||
sendMessageToStreamlitClient("streamlit:componentReady", {apiVersion: 1}); | ||
}, | ||
setFrameHeight: function(height) { | ||
sendMessageToStreamlitClient("streamlit:setFrameHeight", {height: height}); | ||
}, | ||
setComponentValue: function(value) { | ||
sendMessageToStreamlitClient("streamlit:setComponentValue", {value: value}); | ||
// sendMessageToStreamlitClient("streamlit:setComponentValue", {value}); | ||
}, | ||
RENDER_EVENT: "streamlit:render", | ||
events: { | ||
addEventListener: function(type, callback) { | ||
window.addEventListener("message", function(event) { | ||
if (event.data.type === type) { | ||
event.detail = event.data | ||
callback(event); | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import streamlit as st | ||
import pyvista as pv | ||
from stpyvista import experimental_vtkjs, stpyvista | ||
from stpyvista.export import export_vtksz | ||
import asyncio | ||
|
||
@st.cache_resource | ||
def create_plotter(dummy:str = "sphere"): | ||
|
||
# Initialize a plotter object | ||
plotter = pv.Plotter(window_size=[400, 400]) | ||
mesh = pv.Sphere(radius=1.0, center=(0, 0, 0)) | ||
x, y, z = mesh.cell_centers().points.T | ||
mesh["My scalar"] = z | ||
|
||
# Scalar bar configuration | ||
# scalar_bar_kwargs = dict( | ||
# font_family='arial', | ||
# interactive=True, | ||
# position_x = 0.05, | ||
# position_y = 0.05, | ||
# vertical=False | ||
# ) | ||
|
||
## Add mesh to the plotter | ||
plotter.add_mesh( | ||
mesh, | ||
scalars="My scalar", | ||
cmap="prism", | ||
show_edges=True, | ||
edge_color="#001100", | ||
ambient=0.2, | ||
show_scalar_bar = False | ||
) | ||
|
||
## Some final touches | ||
plotter.background_color = "pink" | ||
plotter.view_isometric() | ||
|
||
return plotter | ||
|
||
async def main(): | ||
|
||
st.set_page_config(page_icon="🧊", layout="wide") | ||
st.title("🧊 `stpyvista`") | ||
st.write("*Show PyVista 3D visualizations in Streamlit*") | ||
|
||
plotter = create_plotter() | ||
|
||
if "data" not in st.session_state: | ||
st.session_state.data = await export_vtksz(plotter) | ||
|
||
lcol, rcol = st.columns(2) | ||
with rcol: | ||
"🌎 3D Model" | ||
camera = experimental_vtkjs(st.session_state.data, key="experimental-stpv") | ||
|
||
with lcol: | ||
"🎥 Camera" | ||
st.json(camera) | ||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |