-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
179 lines (164 loc) · 5.53 KB
/
demo.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
from dash_snap_grid import ResponsiveGrid, DraggableDiv
from dash import Dash, html, Input, Output, dcc, State, ctx, ALL
import dash_mantine_components as dmc
import random
from dash_iconify import DashIconify
app = Dash(__name__)
def card(id, title, text, bg=1):
return dmc.Card(
id=id,
children=[
dmc.CardSection(
dmc.Image(
src=f"https://raw.githubusercontent.com/mantinedev/mantine/master/.demo/images/bg-{bg}.png",
alt="Norway",
style={"maxHeight": 100, "objectFit": "cover"},
)
),
dmc.Space(h=10),
dmc.Text(title, fw=500),
dmc.Text(
text,
size="sm",
c="dimmed",
),
dmc.ActionIcon(
DashIconify(icon="material-symbols:close"),
radius="xl",
id={"type": "close", "index": id},
className="dont-drag",
style={"position": "absolute", "top": 0, "right": 0},
variant="transparent",
color="gray",
),
],
withBorder=True,
shadow="sm",
radius="md",
h="100%",
)
initial_layout = [
{"i": "a", "x": 0, "y": 0, "w": 2, "h": 2},
{"i": "b", "x": 2, "y": 0, "w": 2, "h": 2, "isResizable": False},
{"i": "c", "x": 4, "y": 0, "w": 2, "h": 2, "static": True},
{"i": "d", "x": 6, "y": 0, "w": 3, "h": 2, "minW": 3},
]
app.layout = dmc.MantineProvider(
[
dcc.Store(id="layout-store", storage_type="local"),
html.Div(
[
dmc.Group(
[
dmc.Button("Save Layout", id="save-layout"),
dmc.Button("Reset Layout", id="reset-layout"),
dmc.Button("Add New", id="add-new"),
DraggableDiv(
id="draggable-div", children=[dmc.Button("Drag me")]
),
]
),
ResponsiveGrid(
id="input",
layouts={"lg": initial_layout},
cols={"lg": 12, "md": 6, "sm": 2, "xs": 1},
rowHeight=100,
preventCollision=True,
compactType="vertical",
draggableCancel=".dont-drag",
resizeHandles=["sw", "se", "nw", "ne"],
isDroppable=True,
children=[
card("a", "Regular Card", "This is a regular grid item"),
card(
"b",
"Non-Resizable Card",
"This card cannot be resized, but can still be moved",
bg=2,
),
card(
"c",
"Fixed card",
"This card cannot be resized or moved",
bg=7,
),
card(
"d",
"Card with min-width",
"This card has a minimum width of 3 grid columns",
bg=8,
),
],
),
dmc.Text(id="output"),
],
style={"padding": "20px"},
),
]
)
@app.callback(
Output("layout-store", "data"),
Input("save-layout", "n_clicks"),
Input("reset-layout", "n_clicks"),
State("input", "layouts"),
prevent_initial_call=True,
)
def update_layout(save_click, reset_click, layouts):
if ctx.triggered_id == "save-layout":
return layouts
return {}
@app.callback(
Output("input", "layouts", allow_duplicate=True),
Input("layout-store", "data"),
prevent_initial_call=True,
)
def retrieve_layouts(layouts):
if not layouts:
return {"lg": initial_layout}
return layouts
@app.callback(
Output("input", "children"),
Output("input", "layouts"),
Input("add-new", "n_clicks"),
Input({"type": "close", "index": ALL}, "n_clicks"),
Input("input", "droppedItem"),
State("input", "children"),
State("input", "layouts"),
State("layout-store", "data"),
prevent_initial_call=True,
)
def add_new(n_clicks, close_clicks, dropped_item, children, layouts, stored_layouts):
if ctx.triggered_id == "add-new":
bg = random.randint(1, 9)
children.append(
card(
f"new-{len(children)}",
"New Card",
"This card was added dynamically",
bg=bg,
)
)
if stored_layouts:
layouts = stored_layouts
elif isinstance(ctx.triggered_id, dict) and ctx.triggered_id["type"] == "close":
children = [
child
for child in children
if child["props"]["id"] != ctx.triggered_id["index"]
]
elif ctx.triggered_id == "input" and dropped_item:
bg = random.randint(1, 9)
dropped_item["i"] = f"new-{len(children)}"
children.append(
card(
dropped_item["i"],
"New Card",
"This card was added by dropping an item",
bg=bg,
)
)
for key in layouts.keys():
layouts[key].append(dropped_item)
return children, layouts
if __name__ == "__main__":
app.run_server(debug=True)