adding a new node to a graph.
See original GitHub issuejust published a question in StackOverflow with the following content:
Assuming I have a graph like:
railnet= '''{
"nodes": [
{"data": { "id": 1,"name":"BER" }},
{"data": { "id": 2,"name": "MUN"}},
{"data": { "id": 3,"name": "FRA"}}
],
"edges": [
{"data": { "id": "BER - MUN", "source": "BER", "target": "MUN" }},
{"data": { "id": "MUN - FRA", "source": "MUN", "target": "FRA" }},
{"data": { "id": "FRA - BER", "source": "FRA", "target": "BER" }}
]
}'''
railnetJSON = json.loads(railnet)
ipycytoscape_obj3 = ipycytoscape.CytoscapeWidget()
ipycytoscape_obj3.graph.add_graph_from_json(railnetJSON, directed=False) # I am telling I dont want directions
ipycytoscape_obj3
later on I want to add a node to the graph.
the following does not work:
new_node_string = '''{"data": { "id": "4", "name":"DOR"}}'''
new_node_json = json.loads(new_node)
print(new_node_json['data']) # the dictionary HAS a key "data"
but when doing:
ipycytoscape_obj.graph.add_node(new_node_json)
the error is:
AttributeError: 'dict' object has no attribute 'data'
Some idea what I am doing wrong? Don’t I need the node to be of the type as JSON?
NOTE: So far I don’t want to resort to Networkx
https://stackoverflow.com/questions/64953583/adding-a-node-to-an-ipycytoscape-widget
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Graphs creation, adding and deleting nodes or edges. - LaBRI
In the following, we are adding three nodes with the member function node Graph::addNode () that will, create an instance of a 'node',...
Read more >Adding nodes and edges to the graph | Gephi Cookbook
The following steps illustrate the procedure to add a new node to the graph: ... Now, click on the area of the Graph...
Read more >Graph.add_nodes_from — NetworkX 2.8.8 documentation
Add multiple nodes. Parameters: nodes_for_addingiterable container. A container of nodes (list, dict, set, etc.). OR A container of (node, attribute dict) ...
Read more >add_node: Add a node to an existing graph object - Rdrr.io
With a graph object of class dgr_graph , add a new node to the graph. One can optionally provide node attributes for the...
Read more >How to Build a Graph Data Structure | by Sergey Piterman
Adding a new connection between nodes is as simple as adding a new entry into that array. If you're implementing an undirected graph,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I wrote myself to small helper function to create nodes and edges from dicts:
_set_attributes is used from the original source code: https://github.com/QuantStack/ipycytoscape/blob/26fed38ae6aa5c3bad375cab3ddcf937d84de72b/ipycytoscape/cytoscape.py#L220
It can be used like this:
Also the initial JSON should be
or
The edges are created between two
id
of nodes, not the name.