Streamlit-Agraph
Streamlit-Agraph is a third-party Streamlit component designed to visualize interactive graph networks directly within Streamlit applications. Built on top of `react-graph-vis`, it provides a Python interface for generating nodes, edges, and customizable graph configurations, including physics, direction, hierarchy, and image support. The current version is 0.0.45, and it appears to be actively maintained with irregular updates, as indicated by recent activity on its GitHub repository. [2, 17]
Common errors
-
streamlit.errors.DuplicateWidgetID: There are multiple identical `st.streamlit_agraph.agraph` widgets with the same generated key.
cause You are rendering more than one `agraph` component on the same Streamlit page without providing a unique `key` parameter to each instance. [12, 13]fixPass a distinct `key` argument to each `agraph` call, e.g., `agraph(..., key='graph_one')` and `agraph(..., key='graph_two')`. [12] -
FileNotFoundError: [Errno 2] No such file or directory: '{path}\{node_id}'cause This error can occur when a node's `title` property is interpreted as a file path or URI that the system tries to open upon a double-click event. This is a behavior inherited from the underlying `react-graph-vis` component. [16]fixIf the `title` is not meant to be a clickable URI, ensure it's a plain string. If interaction is desired, consider handling clicks via the `return_value` of `agraph` for custom logic in Python, rather than relying on default browser behavior for `title` attributes. [16] -
PyArrow error while using Streamlit agraph component
cause This issue often arises from conflicts or incorrect installations of `pyarrow` or `streamlit` within the environment, especially when mixing `pip` and `conda` installations. [10]fixIf you initially installed `streamlit` with `pip`, try uninstalling it (`pip uninstall streamlit`) and then reinstalling it using `conda` (`conda install -c conda-forge streamlit`). Ensure `pyarrow` is also consistently installed via the same package manager. [10]
Warnings
- breaking The `TripleStore` functionality, which integrates with `rdflib` for RDF graph handling, is explicitly noted as 'currently not workin since update to agraph 2.0 - work in progress' in the GitHub README. Attempting to use it might lead to unexpected behavior or errors. [2]
- gotcha When placing multiple `agraph` components on a single Streamlit page, you will encounter `streamlit.errors.DuplicateWidgetID`. Streamlit requires all widgets to have unique keys if their structure is identical. [12, 13]
- gotcha Double-clicking on a node with a defined `title` property might cause the browser to attempt opening the `title`'s value as a URI, potentially leading to `FileNotFoundError` or opening a new, unintended page if the value is not a valid or accessible path. [16]
Install
-
pip install streamlit-agraph
Imports
- agraph, Node, Edge, Config
from streamlit_agraph import agraph, Node, Edge, Config
- Config, ConfigBuilder
from streamlit_agraph.config import Config, ConfigBuilder
- TripleStore
from streamlit_agraph import TripleStore
Quickstart
import streamlit as st
from streamlit_agraph import agraph, Node, Edge, Config
nodes = []
edges = []
nodes.append(Node(id="Spiderman", label="Peter Parker", size=25, shape="circularImage", image="http://marvel-force-chart.surge.sh/marvel_force_chart_img/top_spiderman.png"))
nodes.append(Node(id="Captain_Marvel", size=25, shape="circularImage", image="http://marvel-force-chart.surge.sh/marvel_force_chart_img/top_captainmarvel.png"))
nodes.append(Node(id="Iron_Man", label="Tony Stark", size=25, shape="circularImage", image="http://marvel-force-chart.surge.sh/marvel_force_chart_img/top_ironman.png"))
edges.append(Edge(source="Captain_Marvel", label="friend_of", target="Spiderman"))
edges.append(Edge(source="Iron_Man", label="mentor_of", target="Spiderman"))
config = Config(
width=750,
height=550,
directed=True,
physics=True,
hierarchical=False,
# Add a unique key for multiple graphs on a single page
# key="my_unique_agraph_key"
)
st.title("Streamlit-Agraph Quickstart")
st.markdown("An interactive graph visualization example.")
return_value = agraph(nodes=nodes, edges=edges, config=config)
st.write(f"Node selected: {return_value}")