๐ธ๏ธย ย Sigma Graph
Submitted by Lukas Masuch
Summary
Interactive network graph visualization using sigma.js with WebGL rendering, supporting NetworkX graphs and node-link dictionaries.
Functions
sigma_graph
Display an interactive network graph using sigma.js.
sigma.js is a high-performance WebGL-based graph visualization library that handles thousands of nodes smoothly with built-in pan, zoom, and hover interactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Graph | DiGraph | MultiGraph | MultiDiGraph | SigmaGraphData
|
Graph data to visualize. Accepts NetworkX graphs (Graph, DiGraph, MultiGraph, MultiDiGraph) or a dict in node-link format with "nodes" and "edges" keys. |
required |
layout
|
Literal['force', 'spring', 'circular', 'kamada_kawai', 'random'] | None
|
Layout algorithm for positioning nodes. - "force": ForceAtlas2 force-directed layout computed in the browser with animated settling. Best for interactive exploration. - "spring": NetworkX spring_layout (Fruchterman-Reingold). - "circular": Nodes arranged in a circle. - "kamada_kawai": Minimizes edge crossing. - "random": Random positions. - None: No layout computation. Positions must be provided via node x/y attributes. |
'force'
|
width
|
int | Literal['stretch']
|
Width of the graph container. "stretch" fills the container width; an integer sets a fixed width in pixels. |
'stretch'
|
height
|
int
|
Height of the graph container in pixels. |
500
|
node_color
|
str | None
|
Default color for nodes (CSS color string). Overridden by per-node "color" attribute. If None, uses Streamlit theme primary color. |
None
|
edge_color
|
str | None
|
Default color for edges (CSS color string). Overridden by per-edge "color" attribute. If None, uses Streamlit theme muted color. |
None
|
node_size
|
int | str
|
How to size nodes. - int (default 8): Uniform size for all nodes. - "degree": Scale size by node degree (number of connections). - str: Name of a node attribute to use for sizing. |
8
|
selection_mode
|
Literal['nodes', 'edges', 'all']
|
What can be selected: "nodes" only, "edges" only, or "all" for both. |
'nodes'
|
on_select
|
Literal['ignore', 'rerun'] | Callable[[SigmaGraphSelection], None]
|
Behavior when user clicks a selectable element. - "ignore": Disables selection (default). - "rerun": Triggers a rerun when an element is clicked. - Callable: Function called with the SigmaGraphSelection. |
'ignore'
|
key
|
str | None
|
Unique key for the widget. Required when on_select is "rerun" or a callable. |
None
|
Returns:
| Type | Description |
|---|---|
SigmaGraphSelection | None
|
When on_select="ignore": Always returns None. |
SigmaGraphSelection | None
|
When on_select="rerun": Returns SigmaGraphSelection with the clicked element, or None if nothing is selected. |
SigmaGraphSelection | None
|
When on_select is a callable: The callback receives the selection and returns None. |
Raises:
| Type | Description |
|---|---|
StreamlitAPIException
|
If layout=None and nodes lack x/y positions, or if on_select requires a key but none is provided. |
Example
Source code in src/streamlit_extras/sigma_graph/__init__.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
Import:
- You should add this to the top of your .py file
Examples
example_basic
def example_basic() -> None:
"""Basic example with a simple graph."""
st.write("### Basic NetworkX Graph")
st.write("Display a graph with the default force-directed layout.")
# Create a simple graph without NetworkX for the example
graph = {
"nodes": [
{"id": "Alice", "label": "Alice", "size": 15},
{"id": "Bob", "label": "Bob", "size": 12},
{"id": "Carol", "label": "Carol", "size": 10},
{"id": "Dave", "label": "Dave", "size": 10},
],
"edges": [
{"source": "Alice", "target": "Bob"},
{"source": "Alice", "target": "Carol"},
{"source": "Bob", "target": "Carol"},
{"source": "Carol", "target": "Dave"},
],
}
sigma_graph(graph, height=400)
example_styled
def example_styled() -> None:
"""Example with custom colors and sizes."""
st.write("### Styled Graph")
st.write("Customize node colors and sizes based on data.")
graph = {
"directed": True,
"nodes": [
{"id": "streamlit", "label": "Streamlit", "size": 25, "color": "#ff4b4b"},
{"id": "tornado", "label": "Tornado", "size": 12, "color": "#83c9ff"},
{"id": "pandas", "label": "Pandas", "size": 18, "color": "#0068c9"},
{"id": "numpy", "label": "NumPy", "size": 18, "color": "#0068c9"},
{"id": "pillow", "label": "Pillow", "size": 10, "color": "#83c9ff"},
],
"edges": [
{"source": "streamlit", "target": "tornado", "size": 2},
{"source": "streamlit", "target": "pandas", "size": 2},
{"source": "streamlit", "target": "pillow", "size": 1},
{"source": "pandas", "target": "numpy", "size": 3},
],
}
sigma_graph(graph, height=450, layout="circular")
example_interactive
def example_interactive() -> None:
"""Example with node selection."""
st.write("### Interactive Selection")
st.write("Click on a node to see its details.")
graph = {
"nodes": [
{"id": "1", "label": "Node 1", "group": "A", "size": 12},
{"id": "2", "label": "Node 2", "group": "A", "size": 15},
{"id": "3", "label": "Node 3", "group": "B", "size": 10},
{"id": "4", "label": "Node 4", "group": "B", "size": 18},
{"id": "5", "label": "Node 5", "group": "C", "size": 14},
],
"edges": [
{"source": "1", "target": "2"},
{"source": "2", "target": "3"},
{"source": "3", "target": "4"},
{"source": "4", "target": "5"},
{"source": "5", "target": "1"},
{"source": "2", "target": "4"},
],
}
selection = sigma_graph(graph, height=400, on_select="rerun", key="interactive_graph")
if selection:
st.success(f"Selected {selection['type']}: **{selection['id']}**")
with st.expander("Attributes"):
st.json(selection["attributes"])
else:
st.info("Click on a node to select it.")
example_networkx
def example_networkx() -> None:
"""Example with real NetworkX graphs."""
import networkx as nx
st.write("### NetworkX Graph Examples")
st.write("Explore different graph datasets and generators from NetworkX.")
# Graph options - mix of classic graphs and parameterized generators
graph_options: dict[str, Callable[[], nx.Graph]] = {
"Karate Club (34 nodes)": nx.karate_club_graph,
"Les Misรฉrables (77 nodes)": nx.les_miserables_graph,
"Florentine Families (15 nodes)": nx.florentine_families_graph,
"Barabรกsi-Albert (500 nodes)": lambda: nx.barabasi_albert_graph(500, 3),
"Barabรกsi-Albert (1000 nodes)": lambda: nx.barabasi_albert_graph(1000, 2),
"Watts-Strogatz (500 nodes)": lambda: nx.watts_strogatz_graph(500, 4, 0.3),
"Random Geometric (400 nodes)": lambda: nx.random_geometric_graph(400, 0.1),
}
layout_options: dict[str, str] = {
"Force (ForceAtlas2)": "force",
"Spring (Fruchterman-Reingold)": "spring",
"Circular": "circular",
"Kamada-Kawai": "kamada_kawai",
"Random": "random",
}
size_options: dict[str, int | str | None] = {
"By degree (connections)": "degree",
"Uniform (8)": 8,
"Uniform (12)": 12,
}
col1, col2, col3 = st.columns(3)
with col1:
selected_graph = st.selectbox(
"Select a graph",
options=list(graph_options.keys()),
key="networkx_graph_selector",
)
with col2:
selected_layout = st.selectbox(
"Layout algorithm",
options=list(layout_options.keys()),
key="networkx_layout_selector",
)
with col3:
selected_size = st.selectbox(
"Node sizing",
options=list(size_options.keys()),
key="networkx_size_selector",
)
# Load the selected graph
graph_fn = graph_options[selected_graph]
graph = graph_fn()
layout = layout_options[selected_layout]
node_size = size_options[selected_size]
# Add labels only for smaller graphs
if graph.number_of_nodes() <= 100:
for node in graph.nodes:
graph.nodes[node]["label"] = str(node)
st.caption(f"**{graph.number_of_nodes()}** nodes, **{graph.number_of_edges()}** edges")
sigma_graph(graph, layout=layout, node_size=node_size, height=550) # type: ignore[arg-type]