Update app.py
Browse files
app.py
CHANGED
|
@@ -44,6 +44,7 @@ def get_root(node):
|
|
| 44 |
|
| 45 |
return root
|
| 46 |
|
|
|
|
| 47 |
def get_tree(imgpath):
|
| 48 |
|
| 49 |
for foldername in os.listdir(imgpath):
|
|
@@ -56,6 +57,7 @@ def get_tree(imgpath):
|
|
| 56 |
node = name_to_node[node_name]
|
| 57 |
else:
|
| 58 |
node = Node(node_name)
|
|
|
|
| 59 |
|
| 60 |
child_nodes = []
|
| 61 |
for child_name in child_names:
|
|
@@ -71,22 +73,81 @@ def get_tree(imgpath):
|
|
| 71 |
|
| 72 |
# To be finished
|
| 73 |
return get_root(node)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
-
def display_tree():
|
| 80 |
-
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
# Create traces for nodes and edges
|
| 90 |
edge_x = []
|
| 91 |
edge_y = []
|
| 92 |
for edge in edges:
|
|
@@ -101,28 +162,27 @@ def display_tree():
|
|
| 101 |
hoverinfo='none',
|
| 102 |
mode='lines')
|
| 103 |
|
| 104 |
-
node_x = [pos[0] for pos in positions]
|
| 105 |
-
node_y = [pos[1] for pos in positions]
|
| 106 |
|
| 107 |
node_trace = go.Scatter(
|
| 108 |
x=node_x, y=node_y,
|
| 109 |
mode='markers+text',
|
| 110 |
hoverinfo='text',
|
| 111 |
marker=dict(showscale=False, size=10, color='Goldenrod'),
|
| 112 |
-
text=nodes,
|
| 113 |
textposition="top center"
|
| 114 |
)
|
| 115 |
|
| 116 |
-
# Define the layout of the graph
|
| 117 |
layout = go.Layout(
|
|
|
|
| 118 |
showlegend=False,
|
| 119 |
hovermode='closest',
|
| 120 |
-
margin=dict(b=0, l=0, r=0, t=
|
| 121 |
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
| 122 |
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
|
| 123 |
)
|
| 124 |
|
| 125 |
-
# Create the figure
|
| 126 |
fig = go.Figure(data=[edge_trace, node_trace], layout=layout)
|
| 127 |
return fig
|
| 128 |
|
|
|
|
| 44 |
|
| 45 |
return root
|
| 46 |
|
| 47 |
+
|
| 48 |
def get_tree(imgpath):
|
| 49 |
|
| 50 |
for foldername in os.listdir(imgpath):
|
|
|
|
| 57 |
node = name_to_node[node_name]
|
| 58 |
else:
|
| 59 |
node = Node(node_name)
|
| 60 |
+
name_to_node[node_name] = node
|
| 61 |
|
| 62 |
child_nodes = []
|
| 63 |
for child_name in child_names:
|
|
|
|
| 73 |
|
| 74 |
# To be finished
|
| 75 |
return get_root(node)
|
| 76 |
+
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
# def display_tree():
|
| 79 |
+
# # This function should create and return a Plotly figure of the tree
|
| 80 |
|
| 81 |
+
# # Define the nodes and edges for the graph
|
| 82 |
+
# nodes = ['Node 1', 'Node 2', 'Node 3', 'Node 4']
|
| 83 |
+
# edges = [(0, 1), (0, 2), (2, 3)] # Edges are tuples of node indices
|
| 84 |
+
|
| 85 |
+
# # Define positions for the nodes (you can use a layout algorithm for more complex graphs)
|
| 86 |
+
# positions = [(0, 0), (1, 2), (1, -2), (2, 0)]
|
| 87 |
+
|
| 88 |
+
# # Create traces for nodes and edges
|
| 89 |
+
# edge_x = []
|
| 90 |
+
# edge_y = []
|
| 91 |
+
# for edge in edges:
|
| 92 |
+
# x0, y0 = positions[edge[0]]
|
| 93 |
+
# x1, y1 = positions[edge[1]]
|
| 94 |
+
# edge_x.extend([x0, x1, None])
|
| 95 |
+
# edge_y.extend([y0, y1, None])
|
| 96 |
+
|
| 97 |
+
# edge_trace = go.Scatter(
|
| 98 |
+
# x=edge_x, y=edge_y,
|
| 99 |
+
# line=dict(width=2, color='Black'),
|
| 100 |
+
# hoverinfo='none',
|
| 101 |
+
# mode='lines')
|
| 102 |
+
|
| 103 |
+
# node_x = [pos[0] for pos in positions]
|
| 104 |
+
# node_y = [pos[1] for pos in positions]
|
| 105 |
+
|
| 106 |
+
# node_trace = go.Scatter(
|
| 107 |
+
# x=node_x, y=node_y,
|
| 108 |
+
# mode='markers+text',
|
| 109 |
+
# hoverinfo='text',
|
| 110 |
+
# marker=dict(showscale=False, size=10, color='Goldenrod'),
|
| 111 |
+
# text=nodes,
|
| 112 |
+
# textposition="top center"
|
| 113 |
+
# )
|
| 114 |
+
|
| 115 |
+
# # Define the layout of the graph
|
| 116 |
+
# layout = go.Layout(
|
| 117 |
+
# showlegend=False,
|
| 118 |
+
# hovermode='closest',
|
| 119 |
+
# margin=dict(b=0, l=0, r=0, t=0),
|
| 120 |
+
# xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
| 121 |
+
# yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
|
| 122 |
+
# )
|
| 123 |
+
|
| 124 |
+
# # Create the figure
|
| 125 |
+
# fig = go.Figure(data=[edge_trace, node_trace], layout=layout)
|
| 126 |
+
# return fig
|
| 127 |
+
|
| 128 |
+
def display_tree(root):
|
| 129 |
+
nodes = []
|
| 130 |
+
edges = []
|
| 131 |
+
positions = {}
|
| 132 |
|
| 133 |
+
def traverse(node, depth=0, index=0):
|
| 134 |
+
# This assigns a unique index to each node, which we can use for positioning
|
| 135 |
+
if node not in nodes:
|
| 136 |
+
nodes.append(node)
|
| 137 |
+
idx = nodes.index(node)
|
| 138 |
+
positions[idx] = (depth, -len(nodes) + index * 2) # stagger the y-positions for clarity
|
| 139 |
+
|
| 140 |
+
# Loop through each child to connect with edges and calculate their positions
|
| 141 |
+
for child in node.children:
|
| 142 |
+
if child not in nodes:
|
| 143 |
+
nodes.append(child)
|
| 144 |
+
child_idx = nodes.index(child)
|
| 145 |
+
edges.append((idx, child_idx))
|
| 146 |
+
traverse(child, depth + 1, nodes.index(child) - idx)
|
| 147 |
+
|
| 148 |
+
# Start the traversal from the root node
|
| 149 |
+
traverse(root)
|
| 150 |
|
|
|
|
| 151 |
edge_x = []
|
| 152 |
edge_y = []
|
| 153 |
for edge in edges:
|
|
|
|
| 162 |
hoverinfo='none',
|
| 163 |
mode='lines')
|
| 164 |
|
| 165 |
+
node_x = [pos[0] for pos in positions.values()]
|
| 166 |
+
node_y = [pos[1] for pos in positions.values()]
|
| 167 |
|
| 168 |
node_trace = go.Scatter(
|
| 169 |
x=node_x, y=node_y,
|
| 170 |
mode='markers+text',
|
| 171 |
hoverinfo='text',
|
| 172 |
marker=dict(showscale=False, size=10, color='Goldenrod'),
|
| 173 |
+
text=[node.name for node in nodes],
|
| 174 |
textposition="top center"
|
| 175 |
)
|
| 176 |
|
|
|
|
| 177 |
layout = go.Layout(
|
| 178 |
+
title="Tree Visualization",
|
| 179 |
showlegend=False,
|
| 180 |
hovermode='closest',
|
| 181 |
+
margin=dict(b=0, l=0, r=0, t=40),
|
| 182 |
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
| 183 |
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
|
| 184 |
)
|
| 185 |
|
|
|
|
| 186 |
fig = go.Figure(data=[edge_trace, node_trace], layout=layout)
|
| 187 |
return fig
|
| 188 |
|