Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,127 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import igraph as ig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import plotly.graph_objects as go
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
def create_plotly_tree():
|
6 |
-
# Create an igraph tree
|
7 |
-
g = ig.Graph.Tree(7, 2) # Example tree
|
8 |
-
lay = g.layout('rt') # Reingold-Tilford layout
|
9 |
-
|
10 |
-
# Plotly setup
|
11 |
-
edge_x = []
|
12 |
-
edge_y = []
|
13 |
-
for edge in g.get_edgelist():
|
14 |
-
x0, y0 = lay[edge[0]]
|
15 |
-
x1, y1 = lay[edge[1]]
|
16 |
-
edge_x.extend([x0, x1, None])
|
17 |
-
edge_y.extend([y0, y1, None])
|
18 |
-
|
19 |
-
edge_trace = go.Scatter(
|
20 |
-
x=edge_x, y=edge_y,
|
21 |
-
line=dict(width=0.5, color='#888'),
|
22 |
-
hoverinfo='none',
|
23 |
-
mode='lines')
|
24 |
-
|
25 |
-
node_x = [lay[k][0] for k in range(len(lay))]
|
26 |
-
node_y = [lay[k][1] for k in range(len(lay))]
|
27 |
-
|
28 |
-
node_trace = go.Scatter(
|
29 |
-
x=node_x, y=node_y,
|
30 |
-
mode='markers',
|
31 |
-
hoverinfo='text',
|
32 |
-
marker=dict(showscale=False, size=10, color='#850', line_width=2))
|
33 |
-
|
34 |
-
fig = go.Figure(data=[edge_trace, node_trace],
|
35 |
-
layout=go.Layout(
|
36 |
-
showlegend=False,
|
37 |
-
hovermode='closest',
|
38 |
-
margin=dict(b=0, l=0, t=0, r=0),
|
39 |
-
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
40 |
-
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
|
41 |
-
)
|
42 |
-
|
43 |
-
return fig
|
44 |
-
|
45 |
-
def setup_interface():
|
46 |
-
iface = gr.Interface(
|
47 |
-
fn=create_plotly_tree,
|
48 |
-
inputs=[],
|
49 |
-
outputs=gr.Plot(),
|
50 |
-
title="Interactive Tree Visualization"
|
51 |
-
)
|
52 |
-
return iface
|
53 |
-
|
54 |
-
# Launch the interface
|
55 |
-
if __name__ == "__main__":
|
56 |
-
iface = setup_interface()
|
57 |
-
iface.launch()
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
# import igraph as ig
|
3 |
+
# import plotly.graph_objects as go
|
4 |
+
|
5 |
+
# def create_plotly_tree():
|
6 |
+
# # Create an igraph tree
|
7 |
+
# g = ig.Graph.Tree(7, 2) # Example tree
|
8 |
+
# lay = g.layout('rt') # Reingold-Tilford layout
|
9 |
+
|
10 |
+
# # Plotly setup
|
11 |
+
# edge_x = []
|
12 |
+
# edge_y = []
|
13 |
+
# for edge in g.get_edgelist():
|
14 |
+
# x0, y0 = lay[edge[0]]
|
15 |
+
# x1, y1 = lay[edge[1]]
|
16 |
+
# edge_x.extend([x0, x1, None])
|
17 |
+
# edge_y.extend([y0, y1, None])
|
18 |
+
|
19 |
+
# edge_trace = go.Scatter(
|
20 |
+
# x=edge_x, y=edge_y,
|
21 |
+
# line=dict(width=0.5, color='#888'),
|
22 |
+
# hoverinfo='none',
|
23 |
+
# mode='lines')
|
24 |
+
|
25 |
+
# node_x = [lay[k][0] for k in range(len(lay))]
|
26 |
+
# node_y = [lay[k][1] for k in range(len(lay))]
|
27 |
+
|
28 |
+
# node_trace = go.Scatter(
|
29 |
+
# x=node_x, y=node_y,
|
30 |
+
# mode='markers',
|
31 |
+
# hoverinfo='text',
|
32 |
+
# marker=dict(showscale=False, size=10, color='#850', line_width=2))
|
33 |
+
|
34 |
+
# fig = go.Figure(data=[edge_trace, node_trace],
|
35 |
+
# layout=go.Layout(
|
36 |
+
# showlegend=False,
|
37 |
+
# hovermode='closest',
|
38 |
+
# margin=dict(b=0, l=0, t=0, r=0),
|
39 |
+
# xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
40 |
+
# yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
|
41 |
+
# )
|
42 |
+
|
43 |
+
# return fig
|
44 |
+
|
45 |
+
# def setup_interface():
|
46 |
+
# iface = gr.Interface(
|
47 |
+
# fn=create_plotly_tree,
|
48 |
+
# inputs=[],
|
49 |
+
# outputs=gr.Plot(),
|
50 |
+
# title="Interactive Tree Visualization"
|
51 |
+
# )
|
52 |
+
# return iface
|
53 |
+
|
54 |
+
# # Launch the interface
|
55 |
+
# if __name__ == "__main__":
|
56 |
+
# iface = setup_interface()
|
57 |
+
# iface.launch()
|
58 |
+
|
59 |
+
# Import necessary libraries
|
60 |
+
import dash
|
61 |
+
from dash import html, dcc, Input, Output
|
62 |
import plotly.graph_objects as go
|
63 |
+
import igraph as ig
|
64 |
+
|
65 |
+
# Initialize the Dash app
|
66 |
+
app = dash.Dash(__name__)
|
67 |
+
|
68 |
+
# Create an igraph tree
|
69 |
+
g = ig.Graph.Tree(7, 2) # A binary tree with 7 nodes
|
70 |
+
lay = g.layout('rt') # Reingold-Tilford layout
|
71 |
+
|
72 |
+
# Prepare node and edge traces for Plotly
|
73 |
+
edge_trace = go.Scatter(
|
74 |
+
x=[lay[edge[0]][0], lay[edge[1]][0], None],
|
75 |
+
y=[lay[edge[0]][1], lay[edge[1]][1], None],
|
76 |
+
line=dict(width=0.5, color='#888'),
|
77 |
+
hoverinfo='none',
|
78 |
+
mode='lines',
|
79 |
+
line_shape='spline',
|
80 |
+
showlegend=False
|
81 |
+
) for edge in g.get_edgelist()]
|
82 |
+
|
83 |
+
node_trace = go.Scatter(
|
84 |
+
x=[lay[k][0] for k in range(len(lay))],
|
85 |
+
y=[lay[k][1] for k in range(len(lay))],
|
86 |
+
text=[str(k) for k in range(len(lay))],
|
87 |
+
mode='markers+text',
|
88 |
+
hoverinfo='text',
|
89 |
+
marker=dict(
|
90 |
+
showscale=False,
|
91 |
+
color='blue',
|
92 |
+
size=10,
|
93 |
+
line_width=2))
|
94 |
+
|
95 |
+
# Create a figure containing the traces
|
96 |
+
fig = go.Figure(data=edge_trace + [node_trace])
|
97 |
+
|
98 |
+
# Define layout properties
|
99 |
+
fig.update_layout(
|
100 |
+
showlegend=False,
|
101 |
+
hovermode='closest',
|
102 |
+
margin=dict(b=0, l=0, r=0, t=0),
|
103 |
+
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
104 |
+
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
|
105 |
+
)
|
106 |
+
|
107 |
+
# Define the app layout
|
108 |
+
app.layout = html.Div([
|
109 |
+
dcc.Graph(id='tree-graph', figure=fig),
|
110 |
+
html.Pre(id='click-data', style={'paddingTop': 35})
|
111 |
+
])
|
112 |
+
|
113 |
+
# Define callback to update the output on click
|
114 |
+
@app.callback(
|
115 |
+
Output('click-data', 'children'),
|
116 |
+
[Input('tree-graph', 'clickData')]
|
117 |
+
)
|
118 |
+
def display_click_data(clickData):
|
119 |
+
if clickData is not None:
|
120 |
+
node_id = clickData['points'][0]['text']
|
121 |
+
return f"You clicked on node {node_id}"
|
122 |
+
return "Click on a node"
|
123 |
+
|
124 |
+
# Run the app
|
125 |
+
if __name__ == '__main__':
|
126 |
+
app.run_server(debug=True)
|
127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|