I am creating an app 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:
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.)
Thanks! This looks promising! It’s a bit annoying, though, because you have to put every variable name in quotes.
Follow-up: Unfortunately this is pretty clonky, and hasn’t been updated in a while. While it’s “promising,” it’s probably not the right solution.
you csn use streamlit-agraph it might not able to increase or decrease size but really easy to work with
from operator import itemgetter
import networkx as nx
from streamlit_agraph import agraph, Node, Edge, Config
n = 3
m = 2
G = nx.generators.barabasi_albert_graph(n, m)
node_and_degree = G.degree()
most_connected_node = sorted(G.degree, key=lambda x: x[1], reverse=True)[0]
degree = G.degree(most_connected_node)
hub_ego = nx.ego_graph(G, most_connected_node[0])
nodes = [Node(id=i, label=str(i), size=100,) for i in hub_ego.nodes]
edges = [Edge(source=i, target=j, type="CURVE_SMOOTH") for (i,j) in G.edges
if i in hub_ego.nodes and j in hub_ego.nodes]
config = Config(width=1000,
height=1000,
directed=True,
nodeHighlightBehavior=False,
highlightColor="#F7A7A6",
collapsible=False,
node={'labelProperty':'label'},
)
return_value = agraph(nodes=nodes,
edges=edges,
config=config)