Skip to content

vivien000/st-cytoscape

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

st-cytoscape

st-cytoscape is a Streamlit component to embed a Cytoscape.js graph and get the selected nodes and edges in return.

Screenshot

A more advanced example can be seen live here (code).

Installation

pip install st-cytoscape

Quickstart

import streamlit as st
from st_cytoscape 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"])))

Usage

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)

Parameters

Advanced layout

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"}