Skip to content

Commit 77817ed

Browse files
authored
Merge pull request #211 from plotly/cyleaflet-improvements-and-readme
Expose CyLeaflet subcomponent IDs + Add additional documentation of CyLeaflet design
2 parents be60394 + 77d26a9 commit 77817ed

File tree

5 files changed

+68
-13
lines changed

5 files changed

+68
-13
lines changed

CYLEAFLET.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# CyLeaflet: Cytoscape graph component with map layer
2+
3+
CyLeaflet is a Dash component included with Dash Cytoscape which adds geospatial mapping layer to Cytoscape. Node latitude and longitude are specified in the node data, and then rendered by CyLeaflet in the correct position on the map. See the [CyLeaflet documentation](https://dash.plotly.com/cytoscape/cyleaflet) for full usage information.
4+
5+
This document describes the architecture of the CyLeaflet component.
6+
7+
8+
## Component structure
9+
10+
CyLeaflet uses the Dash [All-in-One Component](https://dash.plotly.com/all-in-one-components) pattern to combine Dash Cytoscape and Dash Leaflet with minimal overhead.
11+
12+
This means that CyLeaflet appears at first glance to be a first-class Dash component, but is actually multiple Dash components in a trenchcoat.
13+
14+
Under the hood, an instance of CyLeaflet consists of:
15+
16+
1. A Dash Cytoscape instance
17+
2. A Dash Leaflet instance
18+
3. A dcc.Store instance used for holding element data (explained in more detail below)
19+
4. Some surrounding html.Divs which apply the necessary styling to align the Cytoscape canvas exactly on top of the Leaflet canvas.
20+
5. Several clientside callbacks to link the individual components together
21+
22+
![fig1](img/cyleaflet-layers.png)
23+
24+
## Files
25+
26+
CyLeaflet functionality is implemented in the following files:
27+
28+
1. `dash_cytoscape/CyLeaflet.py`: CyLeaflet class definition, instantiation of underlying Cytoscape and Leaflet components, layout and styling to align the two canvases, and registration of clientside callbacks
29+
2. `src/lib/cyleaflet_clientside.js`: Clientside callback function implementations
30+
31+
32+
## Callbacks
33+
34+
![fig2](img/cyleaflet-callbacks.png)
35+
36+
37+
The following callbacks are used by CyLeaflet:
38+
39+
- **Synchronize Leaflet view with Cytoscape view**
40+
- Input: Cytoscape `extent` property
41+
- Outputs: Leaflet `viewport` property, Leaflet `invalidateSize` property
42+
43+
Purpose: Ensures that the nodes remain in the same places on the map whenever the view is zoomed or dragged. Whenever the Cytoscape view changes, this callback updates the Leaflet view to match. An additional output, the Leaflet `invalidateSize` property, ensures the map gets refreshed properly.
44+
45+
- **Update Cytoscape graph elements**
46+
- Input: dcc.Store `data` property
47+
- Output: Cytoscape `elements` property
48+
49+
Purpose: Provides a way for the Dash developer to update the graph elements displayed in Cytoscape. This callback transforms the `lat` and `lon` coordinates specified in the node data into a corresponding (x, y) canvas position that will align with the map. Updating the Cytoscape nodes directly does not work, because this skips the transformation step, and the nodes will not appear in the correct map positions.

dash_cytoscape/CyLeaflet.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ def __init__(
6464
s: {"id": id, "component": "cyleaflet", "sub": s}
6565
for s in ["cy", "leaf", "elements"]
6666
}
67-
self.ids["component"] = "leaflet"
67+
68+
self.CYTOSCAPE_ID = self.ids["cy"]
69+
self.LEAFLET_ID = self.ids["leaf"]
70+
self.ELEMENTS_ID = self.ids["elements"]
71+
6872
cytoscape_props = cytoscape_props or {}
6973
leaflet_props = leaflet_props or {}
7074
elements = cytoscape_props.get("elements", [])

demos/usage-cy-leaflet-aio.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,23 @@
5050
"Gold Coast": (-28.026901440211205, 153.4208293956674),
5151
}
5252

53+
# Create an instance of CyLeaflet
54+
cyleaflet_instance = cyto.CyLeaflet(
55+
id="my-cy-leaflet",
56+
cytoscape_props={
57+
"elements": [],
58+
"stylesheet": cy_stylesheet,
59+
},
60+
width=int(default_div_style["width"][:-2]),
61+
height=int(default_div_style["height"][:-2]),
62+
)
63+
64+
5365
# App
5466
app.layout = html.Div(
5567
[
5668
html.Div(
57-
cyto.CyLeaflet(
58-
id="my-cy-leaflet",
59-
cytoscape_props={
60-
"elements": [],
61-
"stylesheet": cy_stylesheet,
62-
},
63-
width=int(default_div_style["width"][:-2]),
64-
height=int(default_div_style["height"][:-2]),
65-
),
69+
cyleaflet_instance,
6670
id="cy-leaflet-div",
6771
style=default_div_style,
6872
),
@@ -97,9 +101,7 @@
97101
@callback(
98102
Output("cy-leaflet-div", "children"),
99103
Output("cy-leaflet-div", "style"),
100-
Output(
101-
{"id": "my-cy-leaflet", "sub": "leaf", "component": "cyleaflet"}, "children"
102-
),
104+
Output(cyleaflet_instance.LEAFLET_ID, "children"),
103105
Input("location-dropdown", "value"),
104106
Input("width-input", "value"),
105107
Input("height-input", "value"),

img/cyleaflet-callbacks.png

58 KB
Loading

img/cyleaflet-layers.png

213 KB
Loading

0 commit comments

Comments
 (0)