-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathusage.py
More file actions
192 lines (181 loc) · 5.59 KB
/
Copy pathusage.py
File metadata and controls
192 lines (181 loc) · 5.59 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import dash
from dash import html, Input, Output, State, clientside_callback, _dash_renderer
import dash_flow
import dash_mantine_components as dmc
import dash_daq as daq
import json
_dash_renderer._set_react_version("18.2.0")
app = dash.Dash(__name__, assets_folder='assets')
initial_nodes = [
# First node stays the same
{
'id': '1',
'type': 'resizable',
'data': {
'label': html.Div([
html.Img(src="https://avatars.githubusercontent.com/u/120129682?v=4",
style={'width': '100%', 'height': '100%'}),
], style={
'display': 'flex',
'flexDirection': 'column',
'alignItems': 'center',
'gap': '10px',
'padding': '10px'
})
},
'position': {'x': 250, 'y': 25},
'style': {
'width': 300,
'height': 300,
}
},
# Second node (static)
{
'id': '2',
'type': 'resizable',
'data': {'label': html.Div([
html.Img(src="https://avatars.discourse-cdn.com/v4/letter/h/50afbb/288.png",
style={'width': '100%', 'height': '100%'}),
], style={
'display': 'flex',
'flexDirection': 'column',
'alignItems': 'center',
'gap': '10px',
'padding': '10px'
})},
'position': {'x': 250, 'y': 150},
'style': {
'width': 300,
'height': 300,
}
},
# Add an animated node
{
'id': 'animated1',
'type': 'circle',
'data': {'label': '🔄'},
'position': {'x': 250, 'y': 150},
'style': {
'width': 60,
'height': 60,
}
},
# Third node stays the same
{
'id': '3',
'type': 'resizable',
'data': {'label': html.Div([
html.Img(src="https://avatars.discourse-cdn.com/v4/letter/h/50afbb/288.png",
style={'width': '100%', 'height': '100%'}),
], style={
'display': 'flex',
'flexDirection': 'column',
'alignItems': 'center',
'gap': '10px',
'padding': '10px'
})},
'position': {'x': 250, 'y': 250},
'style': {
'width': 300,
'height': 300,
}
}
]
initial_edges = [
{
'id': 'e1-2',
'source': '1',
'target': '2',
'type': 'animated',
'data': {
'animatedNode': 'animated1' # Reference the dedicated animated node
},
'style': {
'strokeWidth': 2,
'stroke': '#555'
}
},
{
'id': 'e2-3',
'source': '2',
'target': '3',
'type': 'default', # Changed to default type
'style': {
'strokeWidth': 2,
'stroke': '#555'
}
}
]
# Add layout buttons above the DashFlow component
layout_buttons = dmc.Group([
dmc.Button("Vertical Layout", id="btn-vertical", variant="outline"),
dmc.Button("Horizontal Layout", id="btn-horizontal", variant="outline"),
dmc.Button("Radial Layout", id="btn-radial", variant="outline"),
dmc.Button("Force Layout", id="btn-force", variant="outline"),
], mt="md", mb="md")
app.layout = dmc.MantineProvider([
layout_buttons,
dash_flow.DashFlow(
id='react-flow-example',
nodes=initial_nodes,
edges=initial_edges,
showDevTools=True,
style={'height': '600px'},
layoutOptions=None # Add this prop
),
# Hidden div for storing layout options
html.Div(id='layout-options', style={'display': 'none'})
])
# Create a clientside callback to handle layout changes
app.clientside_callback(
"""
function(n_vertical, n_horizontal, n_radial, n_force) {
const triggered = dash_clientside.callback_context.triggered[0];
if (!triggered) return window.dash_clientside.no_update;
const btnId = triggered.prop_id.split('.')[0];
let options = {};
switch(btnId) {
case 'btn-vertical':
options = {
'elk.algorithm': 'layered',
'elk.direction': 'DOWN',
'elk.spacing.nodeNode': 80,
'elk.layered.spacing.nodeNodeBetweenLayers': 100
};
break;
case 'btn-horizontal':
options = {
'elk.algorithm': 'layered',
'elk.direction': 'RIGHT',
'elk.spacing.nodeNode': 80,
'elk.layered.spacing.nodeNodeBetweenLayers': 100
};
break;
case 'btn-radial':
options = {
'elk.algorithm': 'org.eclipse.elk.radial',
'elk.radial.radius': 200
};
break;
case 'btn-force':
options = {
'elk.algorithm': 'org.eclipse.elk.force',
'elk.force.iterations': 300,
'elk.spacing.nodeNode': 80
};
break;
default:
return window.dash_clientside.no_update;
}
return JSON.stringify(options);
}
""",
Output('react-flow-example', 'layoutOptions'), # Change output target to DashFlow's layoutOptions
Input('btn-vertical', 'n_clicks'),
Input('btn-horizontal', 'n_clicks'),
Input('btn-radial', 'n_clicks'),
Input('btn-force', 'n_clicks'),
prevent_initial_call=True
)
if __name__ == '__main__':
app.run_server(debug=True, port=7777)