cytoscape / ipycytoscape

A Cytoscape Jupyter widget
https://ipycytoscape.readthedocs.io/en/master/
BSD 3-Clause "New" or "Revised" License
264 stars 62 forks source link

Pickling error for Graph, Node, Edge #274

Open shadiakiki1986 opened 2 years ago

shadiakiki1986 commented 2 years ago

Bug report

Bug summary

Cannot pickle ipycytoscape graph

Code for reproduction

ex_json = {
    "nodes": [
        {"data": {"id": 0, "name": "A"}},
        {"data": {"id": 1, "name": "B"}},
        {"data": {"id": 2, "name": "C"}}
    ],
    "edges": [
        {"data": {"id": 3, "source": 0, "target": 1}},
        {"data": {"id": 4, "source": 0, "target": 2}},
    ]
}

import ipycytoscape
cyto = ipycytoscape.CytoscapeWidget()
cyto.graph.add_graph_from_json(ex_json)

# pickling code in "actual outcome" section below

Actual outcome

# import pickle
# pickle.dumps(cyto) # PicklingError: Can't pickle <function <lambda> at 0x7fb6da6009d0>: attribute lookup <lambda> on jupyter_client.session failed
# pickle.dumps(cyto.graph) # AttributeError: Can't pickle local object 'Mutable.instance_init.<locals>.callback'
# pickle.dumps(cyto.graph.nodes[0]) # PicklingError: Can't pickle <function <lambda> at 0x7fb6da6009d0>: attribute lookup <lambda> on jupyter_client.session failed
#pickle.dumps(cyto.graph.edges[0]) # PicklingError: Can't pickle <function <lambda> at 0x7fb6da6009d0>: attribute lookup <lambda> on jupyter_client.session failed

#import dill
#dill.dumps(cyto.graph.nodes[0]) # TypeError: cannot pickle '_hashlib.HASH' object

#import cloudpickle
#cloudpickle.dumps(cyto.graph.nodes[0]) # TypeError: cannot pickle '_hashlib.HASH' object

Expected outcome

Result of pickle

Version Info

Selected Jupyter core packages... IPython : 7.27.0 ipykernel : 6.4.1 ipywidgets : 7.6.5 jupyter_client : 6.1.12 jupyter_core : 4.8.1 jupyter_server : 1.11.0 jupyterlab : not installed nbclient : 0.5.4 nbconvert : 6.1.0 nbformat : 5.1.3 notebook : 6.4.4 qtconsole : not installed traitlets : 5.1.0

MridulS commented 2 years ago

It's not possible to pickle a widget object, this probably needs to be solved upstream.

shadiakiki1986 commented 2 years ago

Ok, linking here for reference: https://github.com/jupyter-widgets/ipywidgets/issues/2879

marimeireles commented 2 years ago

Thanks for pointing it to the right direction peeps. I personally didn't know that.