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

Update my_pages/multiverse.py

Browse files
Files changed (1) hide show
  1. 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 render():
6
- st.title("Multiverse of Developer Decisions")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- # Extract stage labels
9
- stage_labels = [stage["label"] for stage in development_stages]
10
 
11
- # Generate options from each stage's questions
12
- stage_options = {stage["label"]: stage["questions"] for stage in development_stages}
13
 
14
- # Let user pick one option from each stage
 
 
 
 
 
15
  selected_path = []
16
- st.subheader("Choose your path")
17
- for stage_label, options in stage_options.items():
18
- choice = st.selectbox(f"{stage_label}:", options, key=stage_label)
19
  selected_path.append(choice)
20
 
21
- # Prepare Sankey diagram labels
22
- labels = []
23
- for stage_label in stage_labels:
24
- labels.extend(stage_options[stage_label])
25
-
26
- # Create links between options in consecutive stages
27
- source = []
28
- target = []
29
- value = []
30
- colors = []
31
-
32
- options_per_stage = [stage_options[stage] for stage in stage_labels]
33
-
34
- for i in range(len(stage_labels) - 1):
35
- start_idx = sum(len(opts) for opts in options_per_stage[:i])
36
- next_start_idx = sum(len(opts) for opts in options_per_stage[:i+1])
37
-
38
- for s in range(start_idx, start_idx + len(options_per_stage[i])):
39
- for t in range(next_start_idx, next_start_idx + len(options_per_stage[i+1])):
40
- source.append(s)
41
- target.append(t)
42
- value.append(1) # constant thickness for now
43
-
44
- # Highlight chosen path
45
- if labels[s] == selected_path[i] and labels[t] == selected_path[i+1]:
46
- colors.append("rgba(0, 150, 0, 0.8)")
47
- else:
48
- colors.append("rgba(200, 200, 200, 0.3)")
49
-
50
- # Create Sankey diagram
51
- fig = go.Figure(data=[go.Sankey(
52
- arrangement="snap",
53
- node=dict(
54
- pad=15,
55
- thickness=20,
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)