Update my_pages/multiverse.py
Browse files- my_pages/multiverse.py +27 -29
my_pages/multiverse.py
CHANGED
|
@@ -2,49 +2,48 @@ import streamlit as st
|
|
| 2 |
import plotly.graph_objects as go
|
| 3 |
from utils import development_stages
|
| 4 |
|
| 5 |
-
def
|
| 6 |
edges = []
|
| 7 |
node_labels = ["Start"]
|
| 8 |
node_positions = [(0, 0)]
|
| 9 |
-
node_stage = [0]
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 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 |
-
|
| 32 |
-
|
| 33 |
edges.append((parent_idx, node_index))
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
def render():
|
| 50 |
st.title("Multiverse of Developer Decisions - Tree View")
|
|
@@ -55,21 +54,20 @@ def render():
|
|
| 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 =
|
| 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=
|
| 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'
|
|
|
|
| 2 |
import plotly.graph_objects as go
|
| 3 |
from utils import development_stages
|
| 4 |
|
| 5 |
+
def build_tree_edges_and_path(selected_path):
|
| 6 |
edges = []
|
| 7 |
node_labels = ["Start"]
|
| 8 |
node_positions = [(0, 0)]
|
| 9 |
+
node_stage = [0]
|
| 10 |
+
node_index_map = {(0, "Start"): 0} # (stage_idx, label) → index
|
| 11 |
|
| 12 |
+
prev_nodes = [0] # start node
|
| 13 |
+
stage_nodes_map = {} # stage_idx → [node indices]
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Build all nodes and edges
|
| 16 |
for stage_idx, stage in enumerate(development_stages, start=1):
|
|
|
|
| 17 |
options = stage["questions"]
|
| 18 |
+
stage_nodes_map[stage_idx] = []
|
| 19 |
for parent_idx in prev_nodes:
|
| 20 |
px, py = node_positions[parent_idx]
|
| 21 |
for opt_idx, opt in enumerate(options):
|
|
|
|
| 22 |
child_x = stage_idx
|
| 23 |
child_y = py + (opt_idx - len(options) / 2) * (1.5 / (stage_idx))
|
| 24 |
node_index = len(node_labels)
|
| 25 |
node_labels.append(opt)
|
| 26 |
node_positions.append((child_x, child_y))
|
| 27 |
node_stage.append(stage_idx)
|
| 28 |
+
node_index_map[(stage_idx, opt)] = node_index
|
|
|
|
| 29 |
edges.append((parent_idx, node_index))
|
| 30 |
+
stage_nodes_map[stage_idx].append(node_index)
|
| 31 |
+
prev_nodes = stage_nodes_map[stage_idx]
|
| 32 |
|
| 33 |
+
# Determine the exact path
|
| 34 |
+
highlight_edges = set()
|
| 35 |
+
highlight_nodes = set([0]) # start node always highlighted
|
| 36 |
+
current_node = 0
|
| 37 |
+
for stage_idx, choice in enumerate(selected_path, start=1):
|
| 38 |
+
if (stage_idx, choice) in node_index_map:
|
| 39 |
+
child_node = node_index_map[(stage_idx, choice)]
|
| 40 |
+
highlight_edges.add((current_node, child_node))
|
| 41 |
+
highlight_nodes.add(child_node)
|
| 42 |
+
current_node = child_node
|
| 43 |
+
else:
|
| 44 |
+
break # choice not found — stop path
|
| 45 |
+
|
| 46 |
+
return node_labels, node_positions, edges, highlight_edges, highlight_nodes
|
| 47 |
|
| 48 |
def render():
|
| 49 |
st.title("Multiverse of Developer Decisions - Tree View")
|
|
|
|
| 54 |
choice = st.selectbox(f"{stage['icon']} {stage['label']}", stage["questions"], key=stage["label"])
|
| 55 |
selected_path.append(choice)
|
| 56 |
|
| 57 |
+
labels, positions, edges, highlight_edges, highlight_nodes = build_tree_edges_and_path(selected_path)
|
| 58 |
|
|
|
|
| 59 |
x_vals = [pos[0] for pos in positions]
|
| 60 |
y_vals = [pos[1] for pos in positions]
|
| 61 |
+
node_colors = ["green" if i in highlight_nodes else "skyblue" for i in range(len(labels))]
|
| 62 |
|
| 63 |
node_trace = go.Scatter(
|
| 64 |
x=x_vals, y=y_vals,
|
| 65 |
mode='markers+text',
|
| 66 |
text=labels,
|
| 67 |
textposition="top center",
|
| 68 |
+
marker=dict(size=20, color=node_colors, line=dict(width=2, color='black'))
|
| 69 |
)
|
| 70 |
|
|
|
|
| 71 |
edge_traces = []
|
| 72 |
for src, dst in edges:
|
| 73 |
color = 'green' if (src, dst) in highlight_edges else 'gray'
|