Update app.py
Browse files
app.py
CHANGED
@@ -75,83 +75,17 @@ def get_tree(imgpath):
|
|
75 |
return get_root(node)
|
76 |
|
77 |
|
78 |
-
def display_tree():
|
79 |
-
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
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 |
-
|
125 |
-
|
126 |
-
return fig
|
127 |
-
|
128 |
-
ROOT = None
|
129 |
-
|
130 |
-
# def display_tree():
|
131 |
-
# nodes = []
|
132 |
-
# edges = []
|
133 |
-
# positions = {}
|
134 |
-
|
135 |
-
# root = ROOT
|
136 |
-
|
137 |
-
# def traverse(node, depth=0, index=0):
|
138 |
-
# # This assigns a unique index to each node, which we can use for positioning
|
139 |
-
# if node not in nodes:
|
140 |
-
# nodes.append(node)
|
141 |
-
# idx = nodes.index(node)
|
142 |
-
# positions[idx] = (depth, -len(nodes) + index * 2) # stagger the y-positions for clarity
|
143 |
-
|
144 |
-
# # Loop through each child to connect with edges and calculate their positions
|
145 |
-
# for child in node.children:
|
146 |
-
# if child not in nodes:
|
147 |
-
# nodes.append(child)
|
148 |
-
# child_idx = nodes.index(child)
|
149 |
-
# edges.append((idx, child_idx))
|
150 |
-
# traverse(child, depth + 1, nodes.index(child) - idx)
|
151 |
-
|
152 |
-
# # Start the traversal from the root node
|
153 |
-
# traverse(root)
|
154 |
|
|
|
155 |
# edge_x = []
|
156 |
# edge_y = []
|
157 |
# for edge in edges:
|
@@ -166,30 +100,93 @@ ROOT = None
|
|
166 |
# hoverinfo='none',
|
167 |
# mode='lines')
|
168 |
|
169 |
-
# node_x = [pos[0] for pos in positions
|
170 |
-
# node_y = [pos[1] for pos in positions
|
171 |
|
172 |
# node_trace = go.Scatter(
|
173 |
# x=node_x, y=node_y,
|
174 |
# mode='markers+text',
|
175 |
# hoverinfo='text',
|
176 |
# marker=dict(showscale=False, size=10, color='Goldenrod'),
|
177 |
-
# text=
|
178 |
# textposition="top center"
|
179 |
# )
|
180 |
|
|
|
181 |
# layout = go.Layout(
|
182 |
-
# title="Tree Visualization",
|
183 |
# showlegend=False,
|
184 |
# hovermode='closest',
|
185 |
-
# margin=dict(b=0, l=0, r=0, t=
|
186 |
# xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
187 |
# yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
|
188 |
# )
|
189 |
|
|
|
190 |
# fig = go.Figure(data=[edge_trace, node_trace], layout=layout)
|
191 |
# return fig
|
192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
|
194 |
|
195 |
|
|
|
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:
|
|
|
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 |
+
ROOT = None
|
129 |
+
|
130 |
+
|
131 |
+
def display_tree():
|
132 |
+
nodes = []
|
133 |
+
edges = []
|
134 |
+
positions = {}
|
135 |
+
|
136 |
+
root = ROOT
|
137 |
+
|
138 |
+
def traverse(node, depth=0, index=0):
|
139 |
+
if node not in nodes:
|
140 |
+
nodes.append(node)
|
141 |
+
idx = nodes.index(node)
|
142 |
+
positions[idx] = (depth * 1, index * 1 - len(nodes) / 2) # Adjusted the multipliers for depth and index
|
143 |
+
|
144 |
+
for child in node.children:
|
145 |
+
if child not in nodes:
|
146 |
+
nodes.append(child)
|
147 |
+
child_idx = nodes.index(child)
|
148 |
+
edges.append((idx, child_idx))
|
149 |
+
traverse(child, depth + 1, index + len(node.children) / 2) # Recursively traverse to set positions
|
150 |
+
|
151 |
+
traverse(root)
|
152 |
+
|
153 |
+
edge_x = []
|
154 |
+
edge_y = []
|
155 |
+
for edge in edges:
|
156 |
+
x0, y0 = positions[edge[0]]
|
157 |
+
x1, y1 = positions[edge[1]]
|
158 |
+
edge_x.extend([x0, x1, None])
|
159 |
+
edge_y.extend([y0, y1, None])
|
160 |
+
|
161 |
+
edge_trace = go.Scatter(
|
162 |
+
x=edge_x, y=edge_y,
|
163 |
+
line=dict(width=2, color='Black'),
|
164 |
+
hoverinfo='none',
|
165 |
+
mode='lines')
|
166 |
+
|
167 |
+
node_x = [pos[0] for pos in positions.values()]
|
168 |
+
node_y = [pos[1] for pos in positions.values()]
|
169 |
+
|
170 |
+
node_trace = go.Scatter(
|
171 |
+
x=node_x, y=node_y,
|
172 |
+
mode='markers+text',
|
173 |
+
hoverinfo='text',
|
174 |
+
marker=dict(showscale=False, size=10, color='Goldenrod'),
|
175 |
+
text=[node.name for node in nodes],
|
176 |
+
textposition="top center"
|
177 |
+
)
|
178 |
+
|
179 |
+
layout = go.Layout(
|
180 |
+
title="Tree Visualization",
|
181 |
+
showlegend=False,
|
182 |
+
hovermode='closest',
|
183 |
+
margin=dict(b=0, l=0, r=0, t=40),
|
184 |
+
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
185 |
+
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
|
186 |
+
)
|
187 |
+
|
188 |
+
fig = go.Figure(data=[edge_trace, node_trace], layout=layout)
|
189 |
+
return fig
|
190 |
|
191 |
|
192 |
|