Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializing data with node_link_json returns "data": null #1298

Open
ethanporcaro opened this issue Oct 18, 2024 · 1 comment
Open

Serializing data with node_link_json returns "data": null #1298

ethanporcaro opened this issue Oct 18, 2024 · 1 comment
Labels
documentation question Further information is requested

Comments

@ethanporcaro
Copy link

Information

  • rustworkx version:
    rustworkx-0.15.1-cp38-abi3-win_amd64
  • Python version:
    Python 3.11.9
  • Rust version:
    N/A
  • Operating system:
    Windows 10 64 bit

What is the current behavior?

It returns a JSON string with null for the data field:

{"directed":false,"multigraph":true,"attrs":null,"nodes":[{"id":0,"data":null},{"id":1,"data":null},{"id":2,"data":null}],"links":[{"source":0,"target":1,"id":0,"data":null},{"source":0,"target":2,"id":1,"data":null},{"source":1,"target":2,"id":2,"data":null}]}

What is the expected behavior?

I should see somthing like
[{"id":0,"data":"A"}

Steps to reproduce the problem

This uses the quickstart example:

import rustworkx

G = rustworkx.PyGraph()
a = G.add_node("A")
b = G.add_node("B")
c = G.add_node("C")
G.add_edges_from([(a, b, 1.5), (a, c, 5.0), (b, c, 2.5)])
print(rustworkx.node_link_json(G))

I wasn't sure if I was supposed to pass anything to node_attrs or edge_attrs. Thanks

@ethanporcaro ethanporcaro added the bug Something isn't working label Oct 18, 2024
@IvanIsCoding IvanIsCoding added question Further information is requested documentation and removed bug Something isn't working labels Oct 19, 2024
@IvanIsCoding
Copy link
Collaborator

Indeed, node_attrs and edge_attrs are necessary to get a more useful output. For example:

import rustworkx

G = rustworkx.PyGraph()
a = G.add_node("A")
b = G.add_node("B")
c = G.add_node("C")
G.add_edges_from([(a, b, 1.5), (a, c, 5.0), (b, c, 2.5)])

def node_attrs_fn(weight):
  return {"name": weight}

def edge_attrs_fn(weight):
  return {"cost": str(weight)}

graph_json = rustworkx.node_link_json(G, node_attrs=node_attrs_fn, edge_attrs=edge_attrs_fn)
print(graph_json)

Outputs:

{"directed":false,"multigraph":true,"attrs":null,"nodes":[{"id":0,"data":{"name":"A"}},{"id":1,"data":{"name":"B"}},{"id":2,"data":{"name":"C"}}],"links":[{"source":0,"target":1,"id":0,"data":{"cost":"1.5"}},{"source":0,"target":2,"id":1,"data":{"cost":"5.0"}},{"source":1,"target":2,"id":2,"data":{"cost":"2.5"}}]}

Which is probably closer to what you want. I think the way forward with this issue is to have a better example on the documentation itself.

Also, this decision may seem not the best when the node weights are strings. But it makes sense for general Python objects, I don't think anyone would like a JSON with {data: "<object object at 0x7fdbbeb78740>"} by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants