|
| 1 | +from dash import ( |
| 2 | + clientside_callback, |
| 3 | + callback, |
| 4 | + ClientsideFunction, |
| 5 | + Output, |
| 6 | + Input, |
| 7 | + html, |
| 8 | + dcc, |
| 9 | + MATCH, |
| 10 | +) |
| 11 | + |
| 12 | +import dash_cytoscape as cyto |
| 13 | + |
| 14 | +try: |
| 15 | + import dash_leaflet as dl |
| 16 | +except ImportError: |
| 17 | + dl = None |
| 18 | + |
| 19 | +# Max zoom of default Leaflet tile layer |
| 20 | +LEAFLET_DEFAULT_MAX_ZOOM = 18 |
| 21 | + |
| 22 | +# Empirically-determined max zoom values for Cytoscape |
| 23 | +# which correspond to max zoom values of Leaflet |
| 24 | +LEAF_TO_CYTO_MAX_ZOOM_MAPPING = { |
| 25 | + 16: 0.418, |
| 26 | + 17: 0.837, |
| 27 | + 18: 1.674, |
| 28 | + 19: 3.349, |
| 29 | + 20: 6.698, |
| 30 | + 21: 13.396, |
| 31 | + 22: 26.793, |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +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='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', |
| 42 | + ) |
| 43 | + if dl |
| 44 | + else None |
| 45 | + ) |
| 46 | + |
| 47 | + def __init__( |
| 48 | + self, |
| 49 | + id, |
| 50 | + cytoscape_props=None, |
| 51 | + leaflet_props=None, |
| 52 | + width="600px", |
| 53 | + height="480px", |
| 54 | + tiles=None, |
| 55 | + ): |
| 56 | + # Throw error if `dash_leaflet` package is not installed |
| 57 | + if dl is None: |
| 58 | + raise ImportError( |
| 59 | + "dash_leaflet not found. Please install it, either directly (`pip install dash_leaflet`) " |
| 60 | + + "or by using `pip install dash_cytoscape[leaflet]`" |
| 61 | + ) |
| 62 | + |
| 63 | + self.ids = { |
| 64 | + s: {"id": id, "component": "cyleaflet", "sub": s} |
| 65 | + for s in ["cy", "leaf", "elements"] |
| 66 | + } |
| 67 | + self.ids["component"] = "leaflet" |
| 68 | + cytoscape_props = cytoscape_props or {} |
| 69 | + leaflet_props = leaflet_props or {} |
| 70 | + elements = cytoscape_props.get("elements", []) |
| 71 | + cytoscape_props, leaflet_props = self.set_default_props_and_overrides( |
| 72 | + cytoscape_props, leaflet_props, tiles |
| 73 | + ) |
| 74 | + |
| 75 | + super().__init__( |
| 76 | + html.Div( |
| 77 | + [ |
| 78 | + html.Div( |
| 79 | + cyto.Cytoscape(**cytoscape_props), |
| 80 | + style={ |
| 81 | + "height": "100%", |
| 82 | + "width": "100%", |
| 83 | + "position": "absolute", |
| 84 | + "top": 0, |
| 85 | + "left": 0, |
| 86 | + "zIndex": 2, |
| 87 | + }, |
| 88 | + ), |
| 89 | + html.Div( |
| 90 | + dl.Map(**leaflet_props), |
| 91 | + style={ |
| 92 | + "height": "100%", |
| 93 | + "width": "100%", |
| 94 | + "position": "absolute", |
| 95 | + "top": 0, |
| 96 | + "left": 0, |
| 97 | + "zIndex": 1, |
| 98 | + }, |
| 99 | + ), |
| 100 | + dcc.Store(id=self.ids["elements"], data=elements), |
| 101 | + ], |
| 102 | + style={ |
| 103 | + "width": width, |
| 104 | + "height": height, |
| 105 | + }, |
| 106 | + ), |
| 107 | + style={ |
| 108 | + "height": "100%", |
| 109 | + "width": "100%", |
| 110 | + "position": "relative", |
| 111 | + }, |
| 112 | + ) |
| 113 | + |
| 114 | + def set_default_props_and_overrides( |
| 115 | + self, user_cytoscape_props, user_leaflet_props, tiles |
| 116 | + ): |
| 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 | + |
| 125 | + # Try to figure out Leaflet maxZoom from Leaflet children, |
| 126 | + # then convert to Cytoscape max zoom |
| 127 | + leaflet_max_zoom = self.get_leaflet_max_zoom(leaflet_children) |
| 128 | + cytoscape_max_zoom = self.get_cytoscape_max_zoom(leaflet_max_zoom) |
| 129 | + |
| 130 | + # Props where we want to override values supplied by the user |
| 131 | + # These are props which are required for CyLeaflet to work properly |
| 132 | + cytoscape_overrides = { |
| 133 | + "id": self.ids["cy"], |
| 134 | + "elements": [], # Elements are set via clientside callback, so set to empty list initially |
| 135 | + "layout": {"name": "preset", "fit": False}, |
| 136 | + "style": {"width": "100%", "height": "100%"}, |
| 137 | + "minZoom": 3 / 100000, |
| 138 | + } |
| 139 | + # Note: Leaflet MUST be initialized with a center and zoom to avoid an error, |
| 140 | + # even though these will be immediately overwritten by syncing w/ Cytoscape |
| 141 | + leaflet_overrides = { |
| 142 | + "id": self.ids["leaf"], |
| 143 | + "children": leaflet_children or [dl.TileLayer()], |
| 144 | + "center": [0, 0], |
| 145 | + "zoom": 6, |
| 146 | + "zoomSnap": 0, |
| 147 | + "zoomControl": False, |
| 148 | + "zoomAnimation": False, |
| 149 | + "maxZoom": 100000, |
| 150 | + "maxBoundsViscosity": 1, |
| 151 | + "maxBounds": [[-85, -180.0], [85, 180.0]], |
| 152 | + "style": {"width": "100%", "height": "100%"}, |
| 153 | + } |
| 154 | + |
| 155 | + # Props where we want to fill in a default value |
| 156 | + # if a value is not supplied by the user |
| 157 | + cytoscape_defaults = { |
| 158 | + "boxSelectionEnabled": True, |
| 159 | + "maxZoom": cytoscape_max_zoom, |
| 160 | + } |
| 161 | + leaflet_defaults = {} |
| 162 | + |
| 163 | + # Start with default props |
| 164 | + cytoscape_props = dict(cytoscape_defaults) |
| 165 | + leaflet_props = dict(leaflet_defaults) |
| 166 | + |
| 167 | + # Update with user-supplied props |
| 168 | + cytoscape_props.update(user_cytoscape_props) |
| 169 | + leaflet_props.update(user_leaflet_props) |
| 170 | + |
| 171 | + # Update with overrides |
| 172 | + cytoscape_props.update(cytoscape_overrides) |
| 173 | + leaflet_props.update(leaflet_overrides) |
| 174 | + |
| 175 | + return cytoscape_props, leaflet_props |
| 176 | + |
| 177 | + # Try to figure out Leaflet maxZoom from Leaflet children |
| 178 | + # If not possible, return the maxZoom of the default Leaflet tile layer |
| 179 | + def get_leaflet_max_zoom(self, leaflet_children): |
| 180 | + if leaflet_children is None or leaflet_children == []: |
| 181 | + return LEAFLET_DEFAULT_MAX_ZOOM |
| 182 | + |
| 183 | + max_zooms = [ |
| 184 | + c.maxZoom |
| 185 | + for c in leaflet_children |
| 186 | + if isinstance(c, dl.TileLayer) and hasattr(c, "maxZoom") |
| 187 | + ] |
| 188 | + |
| 189 | + return max_zooms[-1] if len(max_zooms) > 0 else LEAFLET_DEFAULT_MAX_ZOOM |
| 190 | + |
| 191 | + # Given a maxZoom value for Leaflet, map it to the corresponding maxZoom value for Cytoscape |
| 192 | + # If the value is out of range, return the closest value |
| 193 | + def get_cytoscape_max_zoom(self, leaflet_max_zoom): |
| 194 | + leaflet_max_zoom = leaflet_max_zoom or 0 |
| 195 | + leaflet_max_zoom = min( |
| 196 | + leaflet_max_zoom, max(LEAF_TO_CYTO_MAX_ZOOM_MAPPING.keys()) |
| 197 | + ) |
| 198 | + leaflet_max_zoom = max( |
| 199 | + leaflet_max_zoom, min(LEAF_TO_CYTO_MAX_ZOOM_MAPPING.keys()) |
| 200 | + ) |
| 201 | + return LEAF_TO_CYTO_MAX_ZOOM_MAPPING[leaflet_max_zoom] |
| 202 | + |
| 203 | + |
| 204 | +if dl is not None: |
| 205 | + clientside_callback( |
| 206 | + ClientsideFunction(namespace="cyleaflet", function_name="updateLeafBounds"), |
| 207 | + Output( |
| 208 | + {"id": MATCH, "component": "cyleaflet", "sub": "leaf"}, "invalidateSize" |
| 209 | + ), |
| 210 | + Output({"id": MATCH, "component": "cyleaflet", "sub": "leaf"}, "viewport"), |
| 211 | + Input({"id": MATCH, "component": "cyleaflet", "sub": "cy"}, "extent"), |
| 212 | + ) |
| 213 | + clientside_callback( |
| 214 | + ClientsideFunction(namespace="cyleaflet", function_name="transformElements"), |
| 215 | + Output({"id": MATCH, "component": "cyleaflet", "sub": "cy"}, "elements"), |
| 216 | + Input({"id": MATCH, "component": "cyleaflet", "sub": "elements"}, "data"), |
| 217 | + ) |
0 commit comments