Adam_G
Adam_G

Reputation: 7879

Visualize complex network in streamlit

I am creating an app in streamlit that visualizes a network. It seems fairly straightforward to visualize a basic networkx network in streamlit.

However, when I try to create a network with a node within a node, e.g.:

import streamlit.components.v1 as components
from pyvis.network import Network
import networkx as nx

# create node within node
G = nx.Graph()
G.add_node(1)
H = nx.Graph()
H.add_node(2)
G.add_node(H)
# populate network
nt = Network()
nt.from_nx(G)
nt.save_graph(f'pyvis_graph.html')
HtmlFile = open(f'pyvis_graph.html', 'r', encoding='utf-8')
components.html(HtmlFile.read(), height=435)

I receive the following error:

AssertionError
Traceback:
File "/Users/adamg/anaconda3/envs/ckcviz/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 542, in _run_script
    exec(code, module.__dict__)
File "/Users/adamg/Library/CloudStorage/Dropbox/Kairos/CKC/ckcviz/ckcviz-app.py", line 61, in <module>
    nt.from_nx(G)
File "/Users/adamg/anaconda3/envs/ckcviz/lib/python3.12/site-packages/pyvis/network.py", line 722, in from_nx
    self.add_node(node, **nodes[node])
File "/Users/adamg/anaconda3/envs/ckcviz/lib/python3.12/site-packages/pyvis/network.py", line 233, in add_node
    assert isinstance(n_id, str) or isinstance(n_id, int)

Eventually, I would like to create a network that looks like this: enter image description here

What package(s) can I use to create visualizations like this in streamlit? (Note: I will also dynamically be changing the sizes of these parent and child nodes.)

Upvotes: 0

Views: 134

Answers (0)

Related Questions