Update my_pages/multiverse.py
Browse files- my_pages/multiverse.py +9 -12
my_pages/multiverse.py
CHANGED
|
@@ -36,26 +36,27 @@ def build_tree_and_trace_path(selected_path):
|
|
| 36 |
|
| 37 |
prev_nodes = [0] # nodes at previous stage (start)
|
| 38 |
y_spacing_base = 1.0
|
|
|
|
| 39 |
|
| 40 |
# Build nodes and edges stage by stage
|
| 41 |
for stage_idx, stage in enumerate(choices_list, start=1):
|
| 42 |
options = stage["options"]
|
| 43 |
next_nodes = []
|
| 44 |
|
| 45 |
-
#
|
|
|
|
|
|
|
| 46 |
for parent_order, parent_idx in enumerate(prev_nodes):
|
| 47 |
px, py = node_positions[parent_idx]
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
parent_block_size = len(options) * y_spacing_base *
|
| 51 |
base_y = py + (parent_order - (len(prev_nodes) - 1) / 2.0) * parent_block_size
|
| 52 |
|
| 53 |
for opt_idx, opt in enumerate(options):
|
| 54 |
child_x = float(stage_idx)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
# (3) nonlinear spreading with exponential factor
|
| 58 |
-
offset = (opt_idx - (len(options) - 1) / 2.0) * (y_spacing_base * (1.5 ** stage_idx))
|
| 59 |
|
| 60 |
child_y = base_y + offset
|
| 61 |
node_index = len(node_labels)
|
|
@@ -65,7 +66,6 @@ def build_tree_and_trace_path(selected_path):
|
|
| 65 |
edges.append((parent_idx, node_index))
|
| 66 |
next_nodes.append(node_index)
|
| 67 |
|
| 68 |
-
# After all parents, the next stage's parents are all next_nodes
|
| 69 |
prev_nodes = next_nodes
|
| 70 |
|
| 71 |
# Trace the single chosen path by walking children
|
|
@@ -74,16 +74,13 @@ def build_tree_and_trace_path(selected_path):
|
|
| 74 |
current_node = 0
|
| 75 |
|
| 76 |
for stage_idx, chosen_label in enumerate(selected_path, start=1):
|
| 77 |
-
# children of current_node
|
| 78 |
children = [dst for (src, dst) in edges if src == current_node]
|
| 79 |
-
# find a child among these whose label equals chosen_label
|
| 80 |
found_child = None
|
| 81 |
for c in children:
|
| 82 |
if node_labels[c] == chosen_label:
|
| 83 |
found_child = c
|
| 84 |
break
|
| 85 |
if found_child is None:
|
| 86 |
-
# chosen label not found under this parent — stop tracing
|
| 87 |
break
|
| 88 |
highlight_edges.add((current_node, found_child))
|
| 89 |
highlight_nodes.add(found_child)
|
|
@@ -131,9 +128,9 @@ def render():
|
|
| 131 |
|
| 132 |
node_trace = go.Scatter(
|
| 133 |
x=x_vals, y=y_vals,
|
| 134 |
-
mode='markers
|
| 135 |
text=labels,
|
| 136 |
-
textposition="top center",
|
| 137 |
marker=dict(size=18, color=node_colors, line=dict(width=1, color='black')),
|
| 138 |
hoverinfo="text"
|
| 139 |
)
|
|
|
|
| 36 |
|
| 37 |
prev_nodes = [0] # nodes at previous stage (start)
|
| 38 |
y_spacing_base = 1.0
|
| 39 |
+
spread_factor = 1.5 # tune this to taste
|
| 40 |
|
| 41 |
# Build nodes and edges stage by stage
|
| 42 |
for stage_idx, stage in enumerate(choices_list, start=1):
|
| 43 |
options = stage["options"]
|
| 44 |
next_nodes = []
|
| 45 |
|
| 46 |
+
# scaling: large spacing early, smaller spacing deeper
|
| 47 |
+
scale = spread_factor ** (1.0 / stage_idx)
|
| 48 |
+
|
| 49 |
for parent_order, parent_idx in enumerate(prev_nodes):
|
| 50 |
px, py = node_positions[parent_idx]
|
| 51 |
|
| 52 |
+
# each parent gets its own block of vertical space
|
| 53 |
+
parent_block_size = len(options) * y_spacing_base * scale
|
| 54 |
base_y = py + (parent_order - (len(prev_nodes) - 1) / 2.0) * parent_block_size
|
| 55 |
|
| 56 |
for opt_idx, opt in enumerate(options):
|
| 57 |
child_x = float(stage_idx)
|
| 58 |
|
| 59 |
+
offset = (opt_idx - (len(options) - 1) / 2.0) * (y_spacing_base * scale)
|
|
|
|
|
|
|
| 60 |
|
| 61 |
child_y = base_y + offset
|
| 62 |
node_index = len(node_labels)
|
|
|
|
| 66 |
edges.append((parent_idx, node_index))
|
| 67 |
next_nodes.append(node_index)
|
| 68 |
|
|
|
|
| 69 |
prev_nodes = next_nodes
|
| 70 |
|
| 71 |
# Trace the single chosen path by walking children
|
|
|
|
| 74 |
current_node = 0
|
| 75 |
|
| 76 |
for stage_idx, chosen_label in enumerate(selected_path, start=1):
|
|
|
|
| 77 |
children = [dst for (src, dst) in edges if src == current_node]
|
|
|
|
| 78 |
found_child = None
|
| 79 |
for c in children:
|
| 80 |
if node_labels[c] == chosen_label:
|
| 81 |
found_child = c
|
| 82 |
break
|
| 83 |
if found_child is None:
|
|
|
|
| 84 |
break
|
| 85 |
highlight_edges.add((current_node, found_child))
|
| 86 |
highlight_nodes.add(found_child)
|
|
|
|
| 128 |
|
| 129 |
node_trace = go.Scatter(
|
| 130 |
x=x_vals, y=y_vals,
|
| 131 |
+
mode='markers',
|
| 132 |
text=labels,
|
| 133 |
+
# textposition="top center",
|
| 134 |
marker=dict(size=18, color=node_colors, line=dict(width=1, color='black')),
|
| 135 |
hoverinfo="text"
|
| 136 |
)
|