I see its possible to add a custom plot in Taipy.
import plotly.graph_objects as go import numpy as np
# Function to create rotation matrix
def create_rotation_matrix(A, B, C):
Rz = np.array([[np.cos(A), -np.sin(A), 0], [np.sin(A), np.cos(A), 0], [0, 0, 1]])
Ry = np.array([[np.cos(B), 0, np.sin(B)], [0, 1, 0], [-np.sin(B), 0, np.cos(B)]])
Rx = np.array([[1, 0, 0], [0, np.cos(C), -np.sin(C)], [0, np.sin(C), np.cos(C)]])
return Rz @ Ry @ Rx
# Data points
BASE_DATA = {
1: {"X": 205.204498, "Y": -1396.70898, "Z": 383.235413, "A": -87.5785, "B": 0.539600, "C": -13.9059},
2: {"X": 298.000, "Y": -1475.49597, "Z": 335.000, "A": -88.8150, "B": 0.130000, "C": -0.178000}
}
# Axis length for orientation lines
axis_length = 100
# Plotly figure
fig = go.Figure()
for key, point in BASE_DATA.items():
# Convert angles from degrees to radians
A, B, C = np.radians(point["A"]), np.radians(point["B"]), np.radians(point["C"])
# Creating rotation matrix for A, B, C
R = create_rotation_matrix(A, B, C)
# Orientation vectors for the reference frame
ref_frame_vectors = R @ np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).T
# Adding the point
fig.add_trace(go.Scatter3d(x=[point["X"]], y=[point["Y"]], z=[point["Z"]],
mode='markers', marker=dict(size=4, color=['blue', 'orange'][key-1])))
# Adding orientation vectors for the reference frame as lines
for i, color in zip(range(3), ['red', 'green', 'blue']):
fig.add_trace(go.Scatter3d(x=[point["X"], point["X"] + axis_length * ref_frame_vectors[i, 0]],
y=[point["Y"], point["Y"] + axis_length * ref_frame_vectors[i, 1]],
z=[point["Z"], point["Z"] + axis_length * ref_frame_vectors[i, 2]],
mode='lines', line=dict(color=color, width=2)))
# Optionally, connecting the two points
fig.add_trace(go.Scatter3d(x=[BASE_DATA[1]["X"], BASE_DATA[2]["X"]],
y=[BASE_DATA[1]["Y"], BASE_DATA[2]["Y"]],
z=[BASE_DATA[1]["Z"], BASE_DATA[2]["Z"]],
mode='lines', line=dict(color='grey', width=2, dash='dash')))
# Layout settings
fig.update_layout(scene=dict(
xaxis_title='X Axis',
yaxis_title='Y Axis',
zaxis_title='Z Axis'),
margin=dict(r=0, b=0, l=0, t=0))
# Show plot
fig.show()
# Function to convert Plotly figure to HTML
def expose_plotly(fig):
buffer = io.StringIO()
fig.write_html(buffer)
return buffer.getvalue()
# Taipy GUI setup
def on_user_content(state, path: str, query: dict):
return expose_plotly(fig) # Use the fig object you created
def on_init(state):
state.uc_url = get_user_content_url(state, "hello", {"t": "a"})
I tried adding this to existing page like this and markdown code as : <|part|page={uc_url}|height=800px|>
this isn't quiet working for me. Also, how can I update the plot on the go when I change values?
What does not work here? Yes, you can change the
figon the fly. You just have to create a callback that will regenerate the URL:state.uc_url = get_user_content_url(state, "hello", {"t": "a"})And the function creating your
figcan also use the state parameter to get the values from the user interface:Also, it is possible in the develop version of Taipy to directly put a Plotly graph in Taipy.
For example:
This will be out with Taipy 3.1 in a month. To have the develop branch and this feature now, you need git installed in your Python environment and node.js on your computer. Then:
pip install taipy==3.1.0.dev0