st-cytoscape-extra
is a Streamlit component to embed a Cytoscape.js graph and get the selected nodes and edges in return.
THIS WILL NOT YET WORK WITHOUT ADDITIONAL SETUP AS IT HAS NOT BEEN FINALISED AND UPLOADED TO PYPI. It can be installed, but the repo will also need to be cloned and a development server run. Initial release is likely to be in late December 2023, or merged with the original st-cytoscape package as a pull request. Alongside this, a sample app using all features will be released.
This is based on the st-cytoscape package. However, several additional cytoscape.js extensions have been incorporated into this version.
- Tooltips for nodes or edges on mouseover using popper.js and tippy.js
- Toolbar for zooming
- Navigator window (optional) for birds-eye overview of network
- Additional layouts (avsdf, elk, cola, dagre, cise)
# pip install st-cytoscape
pip install git+https://github.com/Bergam0t/st-cytoscape-extra#egg=st-cytoscape-extra
import streamlit as st
from st_cytoscape_extra import cytoscape
elements = [
{"data": {"id": "X"}, "selected": True, "selectable": False},
{"data": {"id": "Y"}},
{"data": {"id": "Z"}},
{"data": {"source": "X", "target": "Y", "id": "XâžžY"}},
{"data": {"source": "Z", "target": "Y", "id": "ZâžžY"}},
{"data": {"source": "Z", "target": "X", "id": "ZâžžX"}},
]
stylesheet = [
{"selector": "node", "style": {"label": "data(id)", "width": 20, "height": 20}},
{
"selector": "edge",
"style": {
"width": 3,
"curve-style": "bezier",
"target-arrow-shape": "triangle",
},
},
]
selected = cytoscape(elements, stylesheet, key="graph")
st.markdown("**Selected nodes**: %s" % (", ".join(selected["nodes"])))
st.markdown("**Selected edges**: %s" % (", ".join(selected["edges"])))
cytoscape (elements, stylesheet, width="100%", height="300px", layout={"name": "fcose", "animationDuration": 0}, selection_type="additive", user_zooming_enabled=True, user_panning_enabled=True, min_zoom=1e-50, max_zoom=1e50, key=None )
Embeds a Cytoscape.js graph and returns a dictionary containing the list of the ids of selected nodes ("nodes" key) and the list of the ids of the selected edges ("edges" key)
elements
(list): the list of nodes and edges of the graph (cf. https://js.cytoscape.org/#notation/elements-json)stylesheet
(list): the style used for the graph (cf. https://js.cytoscape.org/#style)width
(string): the CSS width attribute of the graph's containerheight
(string): the CSS height attribute of the graph's containerlayout
(dict): the layout options for the graph (cf. https://js.cytoscape.org/#layouts)seletion_type
(string: "single" or "additive"): cf. https://js.cytoscape.org/#core/initialisationuser_zooming_enabled
(boolean): cf. https://js.cytoscape.org/#core/initialisationuser_panning_enabled
(boolean): cf. https://js.cytoscape.org/#core/initialisationmin_zoom
(float): cf. https://js.cytoscape.org/#core/initialisationmax_zoom
(float): cf. https://js.cytoscape.org/#core/initialisationkey
(str or None): an optional key that uniquely identifies this component. If this is None, and the component's arguments are changed, the component will be re-mounted in the Streamlit frontend and lose its current state
WORK IN PROGRESS
Tooltips are enabled using the tippy.js package in conjunction with popper.js
Separate tooltips are supported for edges and nodes.
Instructions to follow
Instructions to follow
st-cytoscape
supports the following built-in layout options that are bundled in cytoscape.js.
layout = {"name": "circle"}
layout = {"name": "random"}
layout = {"name": "grid"}
layout = {"name": "concentric"}
layout = {"name": "breadthfirst"}
layout = {"name": "cose"}
st-cytoscape
includes fCoSE
, a Cytoscape.js extension offering an elegant force-directed layout. You can then use {"name": "fcose", ...}
as an argument for layout
, instead of Cytoscape.js' native layout options.
A nice feature of fcose
is that it can enforce placement constraints, such as:
layout = {"name": "fcose", "animationDuration": 0}
layout["alignmentConstraint"] = {"horizontal": [["X", "Y"]]}
layout["relativePlacementConstraint"] = [{"top": "Z", "bottom": "X"}]
layout["relativePlacementConstraint"] = [{"left": "X", "right": "Y"}]
You can now similarly use the klay
layout, using the cytoscape-klay
add-on for Cytoscape.js - extension.
To use it simply name it in the layout:
layout = {"name": "klay"}
You can now also use the cise
layout, using the cytoscape-cise
add-on for Cytoscape.js - extension.
The cise
layout requires some extra parameters to be passed - it is not enough to just pass the name of the layout as a parameter.
Specifically, the other mandatory parameter is clusters, which must be passed as a list of lists, where each sublist contains the nodes that should belong to each cluster.
layout = {
"name": "cise",
"clusters": [
["node1", "node4"],
["node2", "node6", "node7"],
["node3", "node5"],
["node8"]
]
}
You can now also use the avsdf
layout, using the cytoscape-avsdf
add-on for Cytoscape.js - extension.
To use it simply name it in the layout:
layout = {"name": "avsdf"}
You can now also use the elk
layout, using the cytoscape-elk
add-on for Cytoscape.js - extension.
To use it simply name it in the layout:
layout = {"name": "elk"}
The cola
layout is also included using the cytoscape-cola
add-on for Cytoscape.js - extension.
To use it simply name it in the layout:
layout = {"name": "cola"}
You can now also use the dagre
layout, using the cytoscape-dagre
add-on for Cytoscape.js - extension. To use it simply name it in the layout:
layout = {"name": "dagre"}