Skip to content

Commit 7eeb7fc

Browse files
committed
add predefined OSM tile layer and tiles argument for CyLeaflet
1 parent 17d9e62 commit 7eeb7fc

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

dash_cytoscape/CyLeaflet.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,25 @@
3333

3434

3535
class CyLeaflet(html.Div):
36+
# Predefined Leaflet tile layer with max zoom of 19
37+
OSM = (
38+
dl.TileLayer(
39+
url="https://tile.openstreetmap.org/{z}/{x}/{y}.png",
40+
maxZoom=19,
41+
attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
42+
)
43+
if dl
44+
else None
45+
)
46+
3647
def __init__(
3748
self,
3849
id,
3950
cytoscape_props=None,
4051
leaflet_props=None,
4152
width="600px",
4253
height="480px",
43-
max_zoom=None,
54+
tiles=None,
4455
):
4556
# Throw error if `dash_leaflet` package is not installed
4657
if dl is None:
@@ -58,7 +69,7 @@ def __init__(
5869
leaflet_props = leaflet_props or {}
5970
elements = cytoscape_props.get("elements", [])
6071
cytoscape_props, leaflet_props = self.set_default_props_and_overrides(
61-
cytoscape_props, leaflet_props, max_zoom
72+
cytoscape_props, leaflet_props, tiles
6273
)
6374

6475
super().__init__(
@@ -101,11 +112,19 @@ def __init__(
101112
)
102113

103114
def set_default_props_and_overrides(
104-
self, user_cytoscape_props, user_leaflet_props, max_zoom
115+
self, user_cytoscape_props, user_leaflet_props, tiles
105116
):
117+
# If `tiles` is specified, append to end of Leaflet children
118+
# This will make it the visible TileLayer
119+
leaflet_children = user_leaflet_props.get("children", [])
120+
if not isinstance(leaflet_children, list):
121+
leaflet_children = [leaflet_children]
122+
if tiles is not None:
123+
leaflet_children.append(tiles)
124+
106125
# Try to figure out Leaflet maxZoom from Leaflet children,
107126
# then convert to Cytoscape max zoom
108-
leaflet_max_zoom = max_zoom or self.get_leaflet_max_zoom(user_leaflet_props)
127+
leaflet_max_zoom = self.get_leaflet_max_zoom(leaflet_children)
109128
cytoscape_max_zoom = self.get_cytoscape_max_zoom(leaflet_max_zoom)
110129

111130
# Props where we want to override values supplied by the user
@@ -121,6 +140,7 @@ def set_default_props_and_overrides(
121140
# even though these will be immediately overwritten by syncing w/ Cytoscape
122141
leaflet_overrides = {
123142
"id": self.ids["leaf"],
143+
"children": leaflet_children or [dl.TileLayer()],
124144
"center": [0, 0],
125145
"zoom": 6,
126146
"zoomSnap": 0,
@@ -138,9 +158,7 @@ def set_default_props_and_overrides(
138158
"boxSelectionEnabled": True,
139159
"maxZoom": cytoscape_max_zoom,
140160
}
141-
leaflet_defaults = {
142-
"children": dl.TileLayer(),
143-
}
161+
leaflet_defaults = {}
144162

145163
# Start with default props
146164
cytoscape_props = dict(cytoscape_defaults)
@@ -158,22 +176,17 @@ def set_default_props_and_overrides(
158176

159177
# Try to figure out Leaflet maxZoom from Leaflet children
160178
# If not possible, return the maxZoom of the default Leaflet tile layer
161-
def get_leaflet_max_zoom(self, user_leaflet_props):
162-
if "children" not in user_leaflet_props or user_leaflet_props["children"] == []:
179+
def get_leaflet_max_zoom(self, leaflet_children):
180+
if leaflet_children is None or leaflet_children == []:
163181
return LEAFLET_DEFAULT_MAX_ZOOM
164182

165-
if isinstance(user_leaflet_props["children"], list):
166-
leaflet_children = user_leaflet_props["children"]
167-
else:
168-
leaflet_children = [user_leaflet_props["children"]]
169-
170183
max_zooms = [
171184
c.maxZoom
172185
for c in leaflet_children
173186
if isinstance(c, dl.TileLayer) and hasattr(c, "maxZoom")
174187
]
175188

176-
return max_zooms[0] if max_zooms else LEAFLET_DEFAULT_MAX_ZOOM
189+
return max_zooms[-1] if len(max_zooms) > 0 else LEAFLET_DEFAULT_MAX_ZOOM
177190

178191
# Given a maxZoom value for Leaflet, map it to the corresponding maxZoom value for Cytoscape
179192
# If the value is out of range, return the closest value

0 commit comments

Comments
 (0)