Skip to content

Commit 4d65d46

Browse files
committed
Make Layer.pane and Map.panes reactive, add notebook example
1 parent 6244c7c commit 4d65d46

File tree

3 files changed

+138
-7
lines changed

3 files changed

+138
-7
lines changed

examples/MapPanes.ipynb

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "cbcccab4-2d48-4ed2-9a01-3aebef7a3f2e",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import os\n",
11+
"import json\n",
12+
"import requests\n",
13+
"from ipyleaflet import Map, GeoJSON, Heatmap"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"id": "d6a05f27-4740-4561-9164-fbf7cdd627a3",
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"if not os.path.exists('europe_110.geo.json'):\n",
24+
" url = 'https://github.com/jupyter-widgets/ipyleaflet/raw/master/examples/europe_110.geo.json'\n",
25+
" r = requests.get(url)\n",
26+
" with open('europe_110.geo.json', 'w') as f:\n",
27+
" f.write(r.content.decode(\"utf-8\"))\n",
28+
"\n",
29+
"with open('europe_110.geo.json', 'r') as f:\n",
30+
" data = json.load(f)\n",
31+
"\n",
32+
"data['features'] = [data['features'][0]] # Trim to one country so printing layers is readable\n",
33+
"\n",
34+
"m = Map(center=(40.9964, 19.9851), zoom=6, panes={\"heatmap_down\": {\"zIndex\": 350}, \"heatmap_top\": {\"zIndex\": 650}})\n",
35+
"\n",
36+
"geo_json = GeoJSON(\n",
37+
" data=data,\n",
38+
" style={\n",
39+
" 'opacity': 1, 'dashArray': '9', 'fillOpacity': 0.9, 'weight': 1, 'fillColor': 'white',\n",
40+
" },\n",
41+
")\n",
42+
"\n",
43+
"heatmap = Heatmap(\n",
44+
" locations=[[41.327,19.819],[40.73,19.562]],\n",
45+
" radius=5,\n",
46+
" blur=1,\n",
47+
" min_opacity=1,\n",
48+
")\n",
49+
"\n",
50+
"m.add_layer(heatmap)\n",
51+
"\n",
52+
"m.add_layer(geo_json)"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": null,
58+
"id": "7ae14f1e-203a-4622-8d6f-7f112d28c63c",
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"m"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": null,
68+
"id": "4ce8c2df-156c-40d0-9123-b1e905c13b03",
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"heatmap.pane = \"heatmap_top\""
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"id": "bb558261-3bdf-45d9-964a-52314fdf87cb",
79+
"metadata": {},
80+
"outputs": [],
81+
"source": [
82+
"heatmap.pane = \"heatmap_down\""
83+
]
84+
}
85+
],
86+
"metadata": {
87+
"kernelspec": {
88+
"display_name": "Python 3 (ipykernel)",
89+
"language": "python",
90+
"name": "python3"
91+
},
92+
"language_info": {
93+
"codemirror_mode": {
94+
"name": "ipython",
95+
"version": 3
96+
},
97+
"file_extension": ".py",
98+
"mimetype": "text/x-python",
99+
"name": "python",
100+
"nbconvert_exporter": "python",
101+
"pygments_lexer": "ipython3",
102+
"version": "3.10.5"
103+
}
104+
},
105+
"nbformat": 4,
106+
"nbformat_minor": 5
107+
}

js/src/Map.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ export class LeafletMapView extends utils.LeafletDOMWidgetView {
168168

169169
create_panes() {
170170
const panes = this.model.get('panes');
171-
for (let name in panes) {
171+
for (const name in panes) {
172172
const pane = this.obj.createPane(name);
173173
const styles = panes[name];
174-
for (let key in styles) {
174+
for (const key in styles) {
175175
pane.style[key] = styles[key];
176176
}
177177
}
@@ -222,7 +222,7 @@ export class LeafletMapView extends utils.LeafletDOMWidgetView {
222222
this.el.classList.add('jupyter-widgets');
223223
this.el.classList.add('leaflet-widgets');
224224
this.map_container = document.createElement('div');
225-
this.el.appendChild(this.map_container);
225+
this.map_child = this.el.appendChild(this.map_container);
226226
if (this.get_options().interpolation == 'nearest') {
227227
this.map_container.classList.add('crisp-image');
228228
}
@@ -266,6 +266,11 @@ export class LeafletMapView extends utils.LeafletDOMWidgetView {
266266
});
267267
}
268268

269+
rerender() {
270+
this.el.removeChild(this.map_child);
271+
this.render();
272+
}
273+
269274
leaflet_events() {
270275
this.obj.on('moveend', e => {
271276
if (!this.dirty) {
@@ -330,6 +335,12 @@ export class LeafletMapView extends utils.LeafletDOMWidgetView {
330335
);
331336
}
332337
this.listenTo(this.model, 'msg:custom', this.handle_msg, this);
338+
this.listenTo(
339+
this.model,
340+
'change:panes',
341+
this.rerender,
342+
this
343+
);
333344
this.listenTo(
334345
this.model,
335346
'change:layers',

js/src/layers/Layer.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@ export class LeafletLayerView extends utils.LeafletWidgetView {
5959
this.listenTo(this.model, 'change:popup', function(model, value) {
6060
this.bind_popup(value);
6161
});
62-
const pane = this.model.get('pane');
63-
if (pane !== '') {
64-
L.setOptions(this.obj, {pane});
65-
}
62+
this.update_pane();
6663
});
6764
}
6865

66+
update_pane() {
67+
const pane = this.model.get('pane');
68+
if (pane !== '') {
69+
L.setOptions(this.obj, {pane});
70+
}
71+
}
72+
6973
leaflet_events() {
7074
// If the layer is interactive
7175
if (this.obj.on) {
@@ -116,6 +120,15 @@ export class LeafletLayerView extends utils.LeafletWidgetView {
116120
this.update_popup,
117121
this
118122
);
123+
this.listenTo(
124+
this.model,
125+
'change:pane',
126+
function() {
127+
this.update_pane(),
128+
this.map_view.rerender();
129+
},
130+
this
131+
);
119132
}
120133

121134
remove() {

0 commit comments

Comments
 (0)