question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

adding a new node to a graph.

See original GitHub issue

just 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:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sven5scommented, Nov 25, 2020

I wrote myself to small helper function to create nodes and edges from dicts:

import ipycytoscape

def _set_attributes(instance, data):
    cyto_attrs = ['group', 'removed', 'selected', 'selectable',
                    'locked', 'grabbed', 'grabbable', 'classes', 'position', 'data']
    for k, v in data.items():
        if k in cyto_attrs:
            setattr(instance, k, v)
        else:
            instance.data[k] = v

def add_node(graph, node_dict):
    node = ipycytoscape.Node()
    _set_attributes(node, node_dict)
    graph.add_node(node)

def add_edge(graph, edge_dict, **kwargs):
    edge = ipycytoscape.Edge()
    _set_attributes(edge, edge_dict)
    graph.add_edge(edge, **kwargs)

_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:

add_edge(cytoscapeobj.graph, {'data': { 'source': 'a', 'target': 'c' }}, directed=True)
add_node(cytoscapeobj.graph, { 'data': { 'id': 'aa', 'name': 'Test', 'list': [1, 2, 3, 4]} })
1reaction
MridulScommented, Nov 22, 2020

Also the initial JSON should be

railnet= '''{
    "nodes": [
        {"data": { "id": "BER"}},
        {"data": { "id": "MUN"}},
        {"data": { "id": "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" }}
    ]
  }'''

or

railnet= '''{
    "nodes": [
        {"data": { "id": 1,"name":"BER" }},
        {"data": { "id": 2,"name": "MUN"}},
        {"data": { "id": 3,"name": "FRA"}}
        ],
    "edges": [
        {"data": { "id": "BER - MUN", "source": 1, "target": 2 }},
        {"data": { "id": "MUN - FRA", "source": 2, "target": 3 }},
        {"data": { "id": "FRA - BER", "source": 3, "target": 1 }}
    ]
  }'''

The edges are created between two id of nodes, not the name.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found