prakharg24 commited on
Commit
2a198b1
·
verified ·
1 Parent(s): 1bb56b0

Update my_pages/multiverse.py

Browse files
Files changed (1) hide show
  1. 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 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")
@@ -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 = 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'
 
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'