-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.py
More file actions
272 lines (254 loc) · 9.42 KB
/
Copy pathusage.py
File metadata and controls
272 lines (254 loc) · 9.42 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""
usage.py — the compiled dash_leaflet2 component package in action.
Demos the full step-3 surface: the core trio + vectors + popup/tooltip + GeoJSON + emoji /
iconify markers + LayersControl (base + overlays) + EditControl (draw / delete).
No hooks, no CDN — Leaflet 2 is bundled inside the component's own JS, marker icons and
liquid-glass UI come along too.
Run:
npm run build # build the components first (writes dash_leaflet2/)
python usage.py # http://127.0.0.1:8060
"""
import json
import dash_leaflet2 as dl2
from dl2_locations import WASHINGTON_DC
from dash import Dash, Input, Output, callback, html
CENTER = WASHINGTON_DC.center
GEOJSON = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"name": "Sector A"},
# GeoJSON is [lon, lat] — the opposite order to Leaflet.
"geometry": {"type": "Point", "coordinates": WASHINGTON_DC.at_lonlat(2.2, 2.9)},
},
{
"type": "Feature",
"properties": {"name": "Survey line"},
"geometry": {
"type": "LineString",
"coordinates": [
WASHINGTON_DC.at_lonlat(3.3, -4.9),
WASHINGTON_DC.at_lonlat(1.1, 1.0),
WASHINGTON_DC.at_lonlat(4.5, 4.9),
],
},
},
],
}
app = Dash(__name__)
app.layout = html.Div(
style={
"fontFamily": "system-ui, sans-serif",
"padding": "16px",
"maxWidth": "1100px",
"margin": "0 auto",
},
children=[
html.H2("dash-leaflet2 — compiled components"),
html.P(
"Layers control toggles base tiles + overlays. Edit control adds shapes to its own "
"FeatureGroup and round-trips the GeoJSON to Python. Glass tooltips/popups follow "
"any DMC-themed app's light/dark scheme (these vars fall back to a light look here)."
),
dl2.Map(
id="map",
center=CENTER,
zoom=12,
style={"height": "62vh", "borderRadius": "8px", "overflow": "hidden"},
children=[
# Base + overlays managed by LayersControl (radio + checkboxes).
dl2.LayersControl(
id="layers",
position="topright",
children=[
dl2.BaseLayer(
dl2.TileLayer(),
name="OSM",
checked=True,
),
dl2.BaseLayer(
dl2.TileLayer(
url="https://basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png",
attribution="© OpenStreetMap © CARTO",
),
name="CARTO Dark",
),
dl2.Overlay(
dl2.Polygon(
positions=[
*WASHINGTON_DC.ring([
(3.3, -4.9),
(4.5, 2.9),
(-1.1, 4.9),
(-2.2, -2.9),
])
],
color="#2f9e44",
fillOpacity=0.25,
children=dl2.Tooltip(children="harbor zone"),
),
name="Harbor zone",
checked=True,
),
dl2.Overlay(
dl2.Circle(
center=WASHINGTON_DC.at(-2.2, 1.0),
radius=1500,
color="#e8590c",
fillOpacity=0.2,
children=dl2.Tooltip(children="buoy radius"),
),
name="Buoy radius",
checked=True,
),
],
),
# Always-on markers + GeoJSON
dl2.Marker(
id="marker",
position=CENTER,
draggable=True,
children=[
dl2.Tooltip(children="drag me — position round-trips"),
dl2.Popup(
children=html.Div(
[
html.B("dl2.Marker + dl2.Popup"),
html.Br(),
"Rich Dash content inside a Leaflet 2 popup.",
]
)
),
],
),
dl2.Marker(
position=WASHINGTON_DC.at(4.5, 2.9),
emoji="🛥️",
iconSize=34,
children=dl2.Tooltip(children="emoji marker"),
),
dl2.Marker(
position=[27.99, -97.10],
iconify="mdi:lighthouse-on",
iconColor="#e8590c",
iconSize=36,
children=dl2.Tooltip(children="iconify lighthouse"),
),
dl2.GeoJSON(
id="geojson", data=GEOJSON, style={"color": "#d6336c", "weight": 3}
),
# Editable on-map caption — drag to move, double-click to edit, and (when
# selected) use the on-canvas resize / rotate handles + the glass toolbar.
# Position / text / rotation / fontSize / color all round-trip to Python.
dl2.TextMarker(
id="caption",
text="Harbor District",
position=WASHINGTON_DC.at(3.3, 6.9),
color="#10243a",
backgroundColor="rgba(255,255,255,0.55)",
fontSize=28,
fontWeight=700,
selected=True,
),
# Drawing toolbar (top-left). Drawn shapes -> EditControl.geojson.
dl2.EditControl(id="edit", position="topleft"),
],
),
html.Div(
style={
"display": "flex",
"gap": "16px",
"marginTop": "12px",
"fontFamily": "monospace",
},
children=[
html.Pre(
id="layers-out",
style={
"flex": 1,
"background": "#f1f3f5",
"padding": "10px",
"borderRadius": "6px",
},
),
html.Pre(
id="edit-out",
style={
"flex": 2,
"background": "#f1f3f5",
"padding": "10px",
"borderRadius": "6px",
"maxHeight": "180px",
"overflow": "auto",
},
),
html.Pre(
id="marker-out",
style={
"flex": 1,
"background": "#f1f3f5",
"padding": "10px",
"borderRadius": "6px",
},
),
html.Pre(
id="caption-out",
style={
"flex": 1,
"background": "#f1f3f5",
"padding": "10px",
"borderRadius": "6px",
},
),
],
),
],
)
@callback(
Output("caption-out", "children"),
Input("caption", "text"),
Input("caption", "position"),
Input("caption", "rotation"),
Input("caption", "fontSize"),
Input("caption", "color"),
Input("caption", "n_edits"),
Input("caption", "n_drags"),
)
def show_caption(text, pos, rot, size, color, n_edits, n_drags):
return (
f"text: {text!r}\nposition: {pos}\nrotation: {rot}\n"
f"fontSize: {size}\ncolor: {color}\n"
f"n_edits: {n_edits or 0}\nn_drags: {n_drags or 0}"
)
@callback(
Output("layers-out", "children"),
Input("layers", "activeBase"),
Input("layers", "activeOverlays"),
)
def show_layers(base, overlays):
return f"base:\n {base}\noverlays:\n {overlays}"
@callback(
Output("edit-out", "children"),
Input("edit", "geojson"),
Input("edit", "n_drawn"),
Input("edit", "lastAction"),
)
def show_edit(geo, n, last):
if not geo:
return "Pick a tool and click the map to start drawing."
return (
f"n_drawn: {n or 0}\nlastAction: {last}\n"
f"geojson features: {len((geo or {}).get('features', []))}\n\n"
f"{json.dumps(geo, indent=1)[:600]}"
)
@callback(
Output("marker-out", "children"),
Input("marker", "n_clicks"),
Input("marker", "n_drags"),
Input("marker", "position"),
)
def show_marker(n, drags, pos):
return f"clicks: {n or 0}\ndrags: {drags or 0}\nposition: {pos}"
if __name__ == "__main__":
app.run(debug=True, port=8060)