Update app.py
Browse files
app.py
CHANGED
@@ -75,17 +75,83 @@ def get_tree(imgpath):
|
|
75 |
return get_root(node)
|
76 |
|
77 |
|
78 |
-
|
79 |
-
#
|
80 |
|
81 |
-
#
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
-
#
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
-
# # Create traces for nodes and edges
|
89 |
# edge_x = []
|
90 |
# edge_y = []
|
91 |
# for edge in edges:
|
@@ -100,95 +166,31 @@ def get_tree(imgpath):
|
|
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=
|
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 |
-
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:
|
158 |
-
x0, y0 = positions[edge[0]]
|
159 |
-
x1, y1 = positions[edge[1]]
|
160 |
-
edge_x.extend([x0, x1, None])
|
161 |
-
edge_y.extend([y0, y1, None])
|
162 |
-
|
163 |
-
edge_trace = go.Scatter(
|
164 |
-
x=edge_x, y=edge_y,
|
165 |
-
line=dict(width=2, color='Black'),
|
166 |
-
hoverinfo='none',
|
167 |
-
mode='lines')
|
168 |
-
|
169 |
-
node_x = [pos[0] for pos in positions.values()]
|
170 |
-
node_y = [pos[1] for pos in positions.values()]
|
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=[node.name for node in nodes],
|
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=40),
|
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 |
|
@@ -208,9 +210,9 @@ with gr.Blocks() as demo:
|
|
208 |
print(imgpath)
|
209 |
ROOT = get_tree(imgpath)
|
210 |
print(ROOT.name)
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
|
215 |
with gr.Row():
|
216 |
with gr.Column():
|
|
|
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 |
+
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 |
# hoverinfo='none',
|
167 |
# mode='lines')
|
168 |
|
169 |
+
# node_x = [pos[0] for pos in positions.values()]
|
170 |
+
# node_y = [pos[1] for pos in positions.values()]
|
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=[node.name for node in nodes],
|
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=40),
|
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 |
|
196 |
|
|
|
210 |
print(imgpath)
|
211 |
ROOT = get_tree(imgpath)
|
212 |
print(ROOT.name)
|
213 |
+
gr.Markdown("## Interactive Tree and Image Display")
|
214 |
+
with gr.Row():
|
215 |
+
tree_output = gr.Plot(display_tree) # Connect the function directly
|
216 |
|
217 |
with gr.Row():
|
218 |
with gr.Column():
|