Skip to content

Commit 43f346a

Browse files
Add position to Maps add method (#1156)
* Add position to add method * Rename position to index * Add collapsed option to LayerControl * Actually add ui-test * Update Playwright Snapshots --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 1b37189 commit 43f346a

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

ipyleaflet/leaflet.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -1758,11 +1758,17 @@ class LayersControl(Control):
17581758
"""LayersControl class, with Control as parent class.
17591759
17601760
A control which allows hiding/showing different layers on the Map.
1761+
Attributes
1762+
----------------------
1763+
collapsed: bool, default True
1764+
Set whether control should be open or closed by default
17611765
"""
17621766

17631767
_view_name = Unicode('LeafletLayersControlView').tag(sync=True)
17641768
_model_name = Unicode('LeafletLayersControlModel').tag(sync=True)
17651769

1770+
collapsed = Bool(True).tag(sync=True, o=True)
1771+
17661772

17671773
class MeasureControl(Control):
17681774
"""MeasureControl class, with Control as parent class.
@@ -2676,23 +2682,31 @@ def __isub__(self, item):
26762682
def __add__(self, item):
26772683
return self.add(item)
26782684

2679-
def add(self, item):
2685+
def add(self, item, index=None):
26802686
"""Add an item on the map: either a layer or a control.
26812687
26822688
Parameters
26832689
----------
26842690
item: Layer or Control instance
26852691
The layer or control to add.
2692+
index: int
2693+
The index to insert a Layer. If not specified, the layer is added to the end (on top).
26862694
"""
26872695
if hasattr(item, 'as_leaflet_layer'):
26882696
item = item.as_leaflet_layer()
26892697

26902698
if isinstance(item, Layer):
26912699
if isinstance(item, dict):
26922700
item = basemap_to_tiles(item)
2701+
26932702
if item.model_id in self._layer_ids:
26942703
raise LayerException('layer already on map: %r' % item)
2695-
self.layers = tuple([layer for layer in self.layers] + [item])
2704+
if index is not None:
2705+
if not isinstance(index, int) or index < 0 or index > len(self.layers):
2706+
raise ValueError("Invalid index value")
2707+
self.layers = tuple(list(self.layers)[:index] + [item] + list(self.layers)[index:])
2708+
else:
2709+
self.layers = tuple([layer for layer in self.layers] + [item])
26962710

26972711
elif isinstance(item, Control):
26982712
if item.model_id in self._control_ids:

ui-tests/notebooks/LayerIndex.ipynb

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 13,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"application/vnd.jupyter.widget-view+json": {
11+
"model_id": "01b5fa59850e400c907ab3a552ac4e85",
12+
"version_major": 2,
13+
"version_minor": 0
14+
},
15+
"text/plain": [
16+
"Map(center=[52.204793, 360.121558], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title'…"
17+
]
18+
},
19+
"execution_count": 13,
20+
"metadata": {},
21+
"output_type": "execute_result"
22+
}
23+
],
24+
"source": [
25+
"from ipyleaflet import Map, Marker, LayersControl\n",
26+
"\n",
27+
"\n",
28+
"m = Map(center=(52.204793, 360.121558), zoom=15)\n",
29+
"m.add(LayersControl(position='topright', collapsed=False))\n",
30+
"marker = Marker(location=(52.204793, 360.121558), draggable=False, name=\"One\")\n",
31+
"marker2 = Marker(location=(52.204793, 360.131728), draggable=False, name=\"Two\")\n",
32+
"marker3 = Marker(location=(52.204793, 360.111728), draggable=False, name=\"Three\")\n",
33+
"\n",
34+
"\n",
35+
"m.add(marker)\n",
36+
"m.add(marker2)\n",
37+
"m.add(marker3, index=1)\n",
38+
"\n",
39+
"m"
40+
]
41+
}
42+
],
43+
"metadata": {
44+
"kernelspec": {
45+
"display_name": "Python 3 (ipykernel)",
46+
"language": "python",
47+
"name": "python3"
48+
},
49+
"language_info": {
50+
"codemirror_mode": {
51+
"name": "ipython",
52+
"version": 3
53+
},
54+
"file_extension": ".py",
55+
"mimetype": "text/x-python",
56+
"name": "python",
57+
"nbconvert_exporter": "python",
58+
"pygments_lexer": "ipython3",
59+
"version": "3.12.0"
60+
}
61+
},
62+
"nbformat": 4,
63+
"nbformat_minor": 4
64+
}
Loading
Loading

0 commit comments

Comments
 (0)