A streamlit custom component for visualizing and interacting with graphs
Project description
st-link-analysis
A custom Streamlit component for link analysis, built with Cytoscape.js and Streamlit.
Overview
This project provides a Streamlit custom component for visualizing and interacting with graph data using Cytoscape.js. It supports customizable edge and node styles, labels, colors, captions, and icons.
Demo
A demo deployed with Render can be accessed here.
Features
- Customizable Node and Edge Styles: Easily define the appearance of nodes and edges using a variety of style options.
- Material Icons Support: Supports a subset of Material icons for styling nodes which can be passed by name (e.g.,
icon='person'). Custom icons can still be used by passing a URL (e.g.,icon='url(...)'). - Customizable Layouts: Choose from different layout algorithms to arrange the graph elements.
- Interactive Features:
- Toolbar with fullscreen, JSON export, and layout refresh buttons.
- View control bar for zooming, fitting, and centering the view, making it easier to navigate your graphs.
- View all properties of the selected elements in a side panel.
- Highlights neighboring nodes or edges when an element is selected.
- Node Actions (Expand / Remove): Enable node removal and expansion using the
node_actionsparameter. Removal can be triggered by a delete keydown or a remove button click, while expansion occurs on a double-click or expand button click. When these events are triggered, the event details and selected node IDs are sent back to the Streamlit app as the component's return value.
Installation
To install the package, use pip:
pip install st-link-analysis
Usage
Here is a basic example of how to use the component in your Streamlit app:
import streamlit as st
from st_link_analysis import st_link_analysis, NodeStyle, EdgeStyle
st.set_page_config(layout="wide")
# Sample Data
elements = {
"nodes": [
{"data": {"id": 1, "label": "PERSON", "name": "Streamlit"}},
{"data": {"id": 2, "label": "PERSON", "name": "Hello"}},
{"data": {"id": 3, "label": "PERSON", "name": "World"}},
{"data": {"id": 4, "label": "POST", "content": "x"}},
{"data": {"id": 5, "label": "POST", "content": "y"}},
],
"edges": [
{"data": {"id": 6, "label": "FOLLOWS", "source": 1, "target": 2}},
{"data": {"id": 7, "label": "FOLLOWS", "source": 2, "target": 3}},
{"data": {"id": 8, "label": "POSTED", "source": 3, "target": 4}},
{"data": {"id": 9, "label": "POSTED", "source": 1, "target": 5}},
{"data": {"id": 10, "label": "QUOTES", "source": 5, "target": 4}},
],
}
# Style node & edge groups
node_styles = [
NodeStyle("PERSON", "#FF7F3E", "name", "person"),
NodeStyle("POST", "#2A629A", "content", "description"),
]
edge_styles = [
EdgeStyle("FOLLOWS", caption='label', directed=True),
EdgeStyle("POSTED", caption='label', directed=True),
EdgeStyle("QUOTES", caption='label', directed=True),
]
# Render the component
st.markdown("### st-link-analysis: Example")
st_link_analysis(elements, "cose", node_styles, edge_styles)
API Reference
| Element | Description |
|---|---|
st_link_analysis |
Main component for creating and displaying the graph, including layout and height settings. Refer to docstring. |
NodeStyle |
Defines styles for nodes, including labels, colors, captions, and icons. Refer to docstring. |
EdgeStyle |
Defines styles for edges, including curve styles, labels, colors, and directionality. Refer to docstring. |
Event |
Define an event to pass to component function and listen to. Use sparingly Refer to docstring. |
Development
- Ensure you have Python 3.9+, Node.js, and npm installed.
- Clone this repository
- Navigate to root directory
Python Setup
Create and activate a virtual environment, then install the package in editable mode:
python3 -m venv .venv
source .venv/bin/activate # On Windows use `.venv\Scripts\activate`
pip install -e ".[dev]"
Node Setup
Navigate to the frontend directory and install the necessary npm packages:
cd st_link_analysis/frontend
npm install
Running the App
Change RELEASE flag in st_link_analysis/component/component.py to False.
In one terminal start the frontend dev server
cd st_link_analysis/frontend
npm run start
In another terminal run the streamlit server
cd examples
streamlit run app.py
Testing
Run linting, type checking, and tests:
ruff check
mypy st_link_analysis/
pytest
Type Checking
This project uses type hints and mypy for static type checking to improve code maintainability and developer experience. The mypy configuration is defined in pyproject.toml with strict settings to catch potential type-related issues early.
To run type checking locally:
mypy st_link_analysis/
Contributing
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgments
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file st_link_analysis-0.4.0.tar.gz.
File metadata
- Download URL: st_link_analysis-0.4.0.tar.gz
- Upload date:
- Size: 459.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e831aba88e0e4ce7fa90e51e199ff7d563690c260ab18c8c0d488fc942274556
|
|
| MD5 |
cef0fa7ceb3fca59693613049b7359d2
|
|
| BLAKE2b-256 |
12b1dc95749ce76bfa4fd83492af682a33b4e83139e8e4f07ce1c144a13e4b8b
|
File details
Details for the file st_link_analysis-0.4.0-py3-none-any.whl.
File metadata
- Download URL: st_link_analysis-0.4.0-py3-none-any.whl
- Upload date:
- Size: 480.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c97ee255a98a9f59fd5814879c9f0e698718dc4ba8dd879b3537b8bcc61b94e
|
|
| MD5 |
68524c88d25eddf28d3479d24afa78bf
|
|
| BLAKE2b-256 |
b84fd6e3afe01775e554f156bde6c5b96796e232d1e32de3fef284eb2bc76dc7
|