-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusage.py
253 lines (233 loc) · 9.41 KB
/
usage.py
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import dash_dynamic_grid_layout as dgl
from dash import *
from dash.exceptions import PreventUpdate
import plotly.express as px
import dash_leaflet as dl
import dash_mantine_components as dmc
from dash_iconify import DashIconify
from datetime import datetime, date
import json
import random
import string
import full_calendar_component as fcc
from datetime import datetime
# Set the React version
dash._dash_renderer._set_react_version("18.2.0")
app = Dash(__name__)
# Get today's date
today = datetime.now()
# Format the date
formatted_date = today.strftime("%Y-%m-%d")
# Sample data for the graph
df = px.data.iris()
# Create a Random String ID for the new component
def generate_random_string(length):
# Define the characters to choose from
characters = string.ascii_letters + string.digits
# Generate a random string
random_string = "".join(random.choice(characters) for _ in range(length))
return random_string
app.layout = dmc.MantineProvider(
[
html.Div(
[
html.Center(html.H4("json.dumps(current_layout)")),
html.Hr(),
html.Div(id="layout-output"),
html.Hr(),
dmc.Group([
html.H4("Add items or edit the layout ->"),
dmc.Menu(
[
dmc.MenuTarget(
dmc.ActionIcon(
DashIconify(icon="icon-park:add-web", width=20),
size="lg",
color="#fff",
variant="filled",
id="action-icon",
n_clicks=0,
mb=8,
style={"backgroundColor": "#fff"},
)
),
dmc.MenuDropdown(
[
dmc.MenuItem(
"Add Dynamic Component",
id="add-dynamic-component",
n_clicks=0,
),
dmc.MenuItem(
"Edit Dynamic Layout", id="edit-mode", n_clicks=0
),
]
),
],
transitionProps={
"transition": "rotate-right",
"duration": 150,
},
position="right",
),
]),
dgl.DashGridLayout(
id="grid-layout",
items=[
dgl.DraggableWrapper(
children=[
dl.Map(
dl.TileLayer(),
center=[56, 10],
zoom=6,
style={
"height": "100vh",
"width": "100vw",
},
),
],
id="draggable-map",
handleBackground="rgb(85,85,85)",
),
dgl.DraggableWrapper(
html.Img(
src="https://picsum.photos/200/300",
style={
"width": "100%",
"height": "100%",
"objectFit": "cover",
},
),
id="draggable-image",
),
dgl.DraggableWrapper(
dcc.Graph(
id="example-graph",
figure=px.scatter(
df,
x="sepal_width",
y="sepal_length",
color="species",
),
style={"height": "100%"},
),
id="draggable-graph",
),
dgl.DraggableWrapper(
dmc.ColorPicker(
id="qr-color-picker",
format="rgba",
value="rgba(0, 0, 0, 1)",
fullWidth=True,
),
id="draggable-color-picker",
),
dgl.DraggableWrapper(
fcc.FullCalendarComponent(
id="api_calendar", # Unique ID for the component
initialView='dayGridMonth', # dayGridMonth, timeGridWeek, timeGridDay, listWeek,
# dayGridWeek, dayGridYear, multiMonthYear, resourceTimeline, resourceTimeGridDay, resourceTimeLineWeek
headerToolbar={
"left": "prev,next today",
"center": "",
"right": "",
}, # Calendar header
initialDate=f"{formatted_date}", # Start date for calendar
editable=True, # Allow events to be edited
selectable=True, # Allow dates to be selected
events=[],
nowIndicator=True, # Show current time indicator
navLinks=True, # Allow navigation to other dates
),
id="draggable-calendar"
),
],
itemLayout=[
# wrapper id, x(0-12), y, w(0-12), h(0-12)
{'i': 'draggable-map', 'x': 0, 'y': 0, 'w': 6, 'h': 4},
{'i': 'draggable-image', 'x': 4, 'y': 0, 'w': 4, 'h': 2},
{'i': 'draggable-graph', 'x': 0, 'y': 4, 'w': 6, 'h': 4},
{'i': 'draggable-color-picker', 'x': 6, 'y': 2, 'w': 3, 'h': 2},
{'i': 'draggable-calendar', 'x': 6, 'y': 4, 'w': 6, 'h': 4}
],
showRemoveButton=False,
showResizeHandles=False,
rowHeight=150,
cols={"lg": 12, "md": 10, "sm": 6, "xs": 4, "xxs": 2},
style={"height": "800px"},
compactType="horizontal",
# persistence=True,
),
dcc.Store(id="layout-store"),
]
)
],
id="mantine-provider",
forceColorScheme="light",
)
@callback(Output("layout-store", "data"), Input("grid-layout", "currentLayout"))
def store_layout(current_layout):
return current_layout
@callback(
Output("grid-layout", "showRemoveButton"),
Output("grid-layout", "showResizeHandles"),
# show how to dynamically change the handle background color of a wrapped component
Output("draggable-map", "handleBackground"),
Input("edit-mode", "n_clicks"),
State("grid-layout", "showRemoveButton"),
State("grid-layout", "showResizeHandles"),
prevent_initial_call=True,
)
def enter_editable_mode(n_clicks, current_remove, current_resize):
print("Edit mode clicked:", n_clicks) # Debug print
if n_clicks is None:
raise PreventUpdate
return not current_remove, not current_resize, "red"
@callback(Output("layout-output", "children"), Input("grid-layout", "itemLayout"))
def display_layout(current_layout):
if current_layout and isinstance(current_layout, list):
return html.Div(json.dumps(current_layout))
return "No layout data available"
@callback(
Output("grid-layout", "items"),
Output("grid-layout", "itemLayout"),
Input("add-dynamic-component", "n_clicks"),
prevent_initial_call=True,
)
def add_dynamic_component(n):
if n:
items = Patch()
new_id = generate_random_string(10)
items.append(
dgl.DraggableWrapper(
dcc.Graph(
figure=px.scatter(
df, x="petal_width", y="petal_length", color="species"
),
style={"height": "100%"},
),
id=new_id
)
)
itemLayout = Patch()
itemLayout.append({"i": f"{new_id}", "w": 6})
return items, itemLayout
return no_update, no_update
@callback(
Output("grid-layout", "items", allow_duplicate=True),
Input("grid-layout", "itemToRemove"),
State("grid-layout", "itemLayout"),
prevent_initial_call=True,
)
def remove_component(key, layout):
if key:
items = Patch()
print(key)
for i in range(len(layout)):
if layout[i]['i'] == key:
del items[i]
break
return items
return no_update
if __name__ == "__main__":
app.run_server(debug=True, port=8321, host='0.0.0.0')