Update my_pages/multiverse.py
Browse files- my_pages/multiverse.py +82 -56
my_pages/multiverse.py
CHANGED
|
@@ -2,67 +2,93 @@ import streamlit as st
|
|
| 2 |
import plotly.graph_objects as go
|
| 3 |
from utils import development_stages
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
stage_labels = [stage["label"] for stage in development_stages]
|
| 10 |
|
| 11 |
-
|
| 12 |
-
stage_options = {stage["label"]: stage["questions"] for stage in development_stages}
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
selected_path = []
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
choice = st.selectbox(f"{stage_label}:", options, key=stage_label)
|
| 19 |
selected_path.append(choice)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
line=dict(color="black", width=0.5),
|
| 57 |
-
label=labels,
|
| 58 |
-
color="rgba(100, 100, 200, 0.8)"
|
| 59 |
-
),
|
| 60 |
-
link=dict(
|
| 61 |
-
source=source,
|
| 62 |
-
target=target,
|
| 63 |
-
value=value,
|
| 64 |
-
color=colors
|
| 65 |
-
)
|
| 66 |
-
)])
|
| 67 |
|
| 68 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
| 2 |
import plotly.graph_objects as go
|
| 3 |
from utils import development_stages
|
| 4 |
|
| 5 |
+
def build_tree_edges(selected_path=None):
|
| 6 |
+
edges = []
|
| 7 |
+
node_labels = ["Start"]
|
| 8 |
+
node_positions = [(0, 0)]
|
| 9 |
+
node_stage = [0] # stage index for each node
|
| 10 |
+
node_id_map = {("Start", 0): 0}
|
| 11 |
+
|
| 12 |
+
# Keep track of path highlighting
|
| 13 |
+
highlight_edges = set()
|
| 14 |
+
|
| 15 |
+
prev_nodes = [0] # Start node index
|
| 16 |
+
|
| 17 |
+
for stage_idx, stage in enumerate(development_stages, start=1):
|
| 18 |
+
next_nodes = []
|
| 19 |
+
options = stage["questions"]
|
| 20 |
+
|
| 21 |
+
for parent_idx in prev_nodes:
|
| 22 |
+
px, py = node_positions[parent_idx]
|
| 23 |
+
for opt_idx, opt in enumerate(options):
|
| 24 |
+
# Position children
|
| 25 |
+
child_x = stage_idx
|
| 26 |
+
child_y = py + (opt_idx - len(options) / 2) * (1.5 / (stage_idx))
|
| 27 |
+
node_index = len(node_labels)
|
| 28 |
+
node_labels.append(opt)
|
| 29 |
+
node_positions.append((child_x, child_y))
|
| 30 |
+
node_stage.append(stage_idx)
|
| 31 |
+
node_id_map[(opt, stage_idx)] = node_index
|
| 32 |
+
|
| 33 |
+
edges.append((parent_idx, node_index))
|
| 34 |
+
|
| 35 |
+
# Highlight check
|
| 36 |
+
if selected_path and selected_path[stage_idx - 1] == opt and (
|
| 37 |
+
(stage_idx == 1 and parent_idx == 0)
|
| 38 |
+
or parent_idx in next_nodes
|
| 39 |
+
or any(e[1] == parent_idx for e in edges)
|
| 40 |
+
):
|
| 41 |
+
highlight_edges.add((parent_idx, node_index))
|
| 42 |
|
| 43 |
+
next_nodes.append(node_index)
|
|
|
|
| 44 |
|
| 45 |
+
prev_nodes = [n for n in next_nodes if n not in prev_nodes]
|
|
|
|
| 46 |
|
| 47 |
+
return node_labels, node_positions, edges, highlight_edges
|
| 48 |
+
|
| 49 |
+
def render():
|
| 50 |
+
st.title("Multiverse of Developer Decisions - Tree View")
|
| 51 |
+
|
| 52 |
+
# User selection for each stage
|
| 53 |
selected_path = []
|
| 54 |
+
for stage in development_stages:
|
| 55 |
+
choice = st.selectbox(f"{stage['icon']} {stage['label']}", stage["questions"], key=stage["label"])
|
|
|
|
| 56 |
selected_path.append(choice)
|
| 57 |
|
| 58 |
+
labels, positions, edges, highlight_edges = build_tree_edges(selected_path)
|
| 59 |
+
|
| 60 |
+
# Draw nodes
|
| 61 |
+
x_vals = [pos[0] for pos in positions]
|
| 62 |
+
y_vals = [pos[1] for pos in positions]
|
| 63 |
+
|
| 64 |
+
node_trace = go.Scatter(
|
| 65 |
+
x=x_vals, y=y_vals,
|
| 66 |
+
mode='markers+text',
|
| 67 |
+
text=labels,
|
| 68 |
+
textposition="top center",
|
| 69 |
+
marker=dict(size=20, color='skyblue', line=dict(width=2, color='black'))
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Draw edges with highlighting
|
| 73 |
+
edge_traces = []
|
| 74 |
+
for src, dst in edges:
|
| 75 |
+
color = 'green' if (src, dst) in highlight_edges else 'gray'
|
| 76 |
+
width = 3 if (src, dst) in highlight_edges else 1.5
|
| 77 |
+
edge_traces.append(go.Scatter(
|
| 78 |
+
x=[positions[src][0], positions[dst][0]],
|
| 79 |
+
y=[positions[src][1], positions[dst][1]],
|
| 80 |
+
mode='lines',
|
| 81 |
+
line=dict(width=width, color=color),
|
| 82 |
+
hoverinfo='none'
|
| 83 |
+
))
|
| 84 |
+
|
| 85 |
+
fig = go.Figure(data=edge_traces + [node_trace])
|
| 86 |
+
fig.update_layout(
|
| 87 |
+
showlegend=False,
|
| 88 |
+
xaxis=dict(visible=False),
|
| 89 |
+
yaxis=dict(visible=False),
|
| 90 |
+
plot_bgcolor='white',
|
| 91 |
+
margin=dict(l=0, r=0, t=0, b=0)
|
| 92 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
st.plotly_chart(fig, use_container_width=True)
|