File size: 3,596 Bytes
a93d680
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c86f6cb
a93d680
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45af18b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# import gradio as gr
# import igraph as ig
# import plotly.graph_objects as go

# def create_plotly_tree():
#     # Create an igraph tree
#     g = ig.Graph.Tree(7, 2)  # Example tree
#     lay = g.layout('rt')  # Reingold-Tilford layout

#     # Plotly setup
#     edge_x = []
#     edge_y = []
#     for edge in g.get_edgelist():
#         x0, y0 = lay[edge[0]]
#         x1, y1 = lay[edge[1]]
#         edge_x.extend([x0, x1, None])
#         edge_y.extend([y0, y1, None])

#     edge_trace = go.Scatter(
#         x=edge_x, y=edge_y,
#         line=dict(width=0.5, color='#888'),
#         hoverinfo='none',
#         mode='lines')

#     node_x = [lay[k][0] for k in range(len(lay))]
#     node_y = [lay[k][1] for k in range(len(lay))]

#     node_trace = go.Scatter(
#         x=node_x, y=node_y,
#         mode='markers',
#         hoverinfo='text',
#         marker=dict(showscale=False, size=10, color='#850', line_width=2))

#     fig = go.Figure(data=[edge_trace, node_trace],
#                     layout=go.Layout(
#                         showlegend=False,
#                         hovermode='closest',
#                         margin=dict(b=0, l=0, t=0, r=0),
#                         xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
#                         yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
#                     )

#     return fig

# def setup_interface():
#     iface = gr.Interface(
#         fn=create_plotly_tree,
#         inputs=[],
#         outputs=gr.Plot(),
#         title="Interactive Tree Visualization"
#     )
#     return iface

# # Launch the interface
# if __name__ == "__main__":
#     iface = setup_interface()
#     iface.launch()

# Import necessary libraries
import dash
from dash import html, dcc, Input, Output
import plotly.graph_objects as go
import igraph as ig

# Initialize the Dash app
app = dash.Dash(__name__)

# Create an igraph tree
g = ig.Graph.Tree(7, 2)  # A binary tree with 7 nodes
lay = g.layout('rt')  # Reingold-Tilford layout

# Prepare node and edge traces for Plotly
edge_trace = go.Scatter(
    x=[lay[edge[0]][0], lay[edge[1]][0], None],
    y=[lay[edge[0]][1], lay[edge[1]][1], None],
    line=dict(width=0.5, color='#888'),
    hoverinfo='none',
    mode='lines',
    line_shape='spline',
    showlegend=False
) for edge in g.get_edgelist()]

node_trace = go.Scatter(
    x=[lay[k][0] for k in range(len(lay))],
    y=[lay[k][1] for k in range(len(lay))],
    text=[str(k) for k in range(len(lay))],
    mode='markers+text',
    hoverinfo='text',
    marker=dict(
        showscale=False,
        color='blue',
        size=10,
        line_width=2))

# Create a figure containing the traces
fig = go.Figure(data=edge_trace + [node_trace])

# Define layout properties
fig.update_layout(
    showlegend=False,
    hovermode='closest',
    margin=dict(b=0, l=0, r=0, t=0),
    xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
    yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
)

# Define the app layout
app.layout = html.Div([
    dcc.Graph(id='tree-graph', figure=fig),
    html.Pre(id='click-data', style={'paddingTop': 35})
])

# Define callback to update the output on click
@app.callback(
    Output('click-data', 'children'),
    [Input('tree-graph', 'clickData')]
)
def display_click_data(clickData):
    if clickData is not None:
        node_id = clickData['points'][0]['text']
        return f"You clicked on node {node_id}"
    return "Click on a node"

# Run the app
if __name__ == '__main__':
    app.run_server(debug=True)