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

Pickle an ipywidget #2879

Open
mhangaard opened this issue May 19, 2020 · 7 comments
Open

Pickle an ipywidget #2879

mhangaard opened this issue May 19, 2020 · 7 comments

Comments

@mhangaard
Copy link

mhangaard commented May 19, 2020

It would be amazing if it was possible to pickle an ipywidget. I can pickle and unpickle a plotly.graph_objects.FigureWidget with no problem, so why not ipywidgets?

If I attempt to pickle an ipywidget.Button for example, I get this:
Log

---------------------------------------------------------------------------
PicklingError                             Traceback (most recent call last)
<ipython-input-47-210db5043c03> in <module>
      1 mybutton = ipywidgets.Button(description="Test")
      2 with open('test_ipywidget.pkl', 'wb') as f:
----> 3     pickle.dump(mybutton, f)

PicklingError: Can't pickle <built-in function input>: it's not the same object as builtins.input

To reproduce

import pickle
import ipywidgets

mybutton = ipywidgets.Button(description="Test")
with open('test_ipywidget.pkl', 'wb') as f:
    pickle.dump(mybutton, f)

My environment

Environment
Python 3.8.2
[GCC 7.3.0] on linux
...
nodejs 13.9.0
ipython 7.14.0
ipywidgets 7.5.1
JupyterLab v2.0.1
@jupyter-widgets/base v3.0.0 enabled OK
@jupyter-widgets/jupyterlab-manager v2.0.0 enabled OK
...
@alexstaravoitau
Copy link

I'm also interested in this — would be great to be able to pickle/restore ipywidgets, if that's at all possible?

@cabreraalex
Copy link

Not sure if this solves it, but you can pickle the state: pickle.dump(mybutton.getState(), f) and restore it that way

@itsergiu
Copy link

itsergiu commented Jul 15, 2021

How to solve it? ipywidgets can not be saved withpickle.dump.
I tried to use get_state and send_state, but it does not work.

w_is =  widgets.IntSlider(
        value=7,
        min=0,
        max=10,
        step=1,
        description='Test:',
        disabled=False,
        continuous_update=False,
        orientation='horizontal',
        readout=True,
        readout_format='d'
        )

# pickle.dump(w_is,open('w_is.pkl','wb'))
# PicklingError: Can't pickle <function <lambda> at 0x0000023391B3FEE0>: attribute lookup <lambda> on jupyter_client.session failed

w_is_state = w_is.get_state()
pickle.dump(w_is_state,open('w_is_state.pkl','wb'))
w_is_state = pickle.load(open('w_is_state.pkl','rb'))
type(w_is_state) # dict

w_is_new = widgets.IntSlider() # dummy
w_is_new.send_state(w_is_state)
w_is_new # does not recieve arguments from w_is_state

# OR

w_is_new.set_state(w_is_state)
w_is_new # does not recieve arguments from w_is_state

@cabreraalex
Copy link

I'm not sure if send_state is the right function. Is there a set_state()?

@itsergiu
Copy link

I tried w_is_new.send_state(w_is_state) and it does not work either.

@kirk0306
Copy link

Is there any other way to download the entire widget (including the children widgets) as a file, so that it can be loaded again next time?

@AyhamSaffar
Copy link

The exception raised when attempting to pickle an ipywidget is very ambiguous. In my case the widget was one attribute of a larger class instance, so it took me days to find this page. I think it would be very helpful if a custom, more descriptive exception was raised

@kirk0306 the workaround to pickle a set of widgets is to just pickle a class instance that has a .create_and_display_widgets() method. You just need to make sure to overide the attribute that stores the displayed widgets created by that call before calling pickle.dump(). This workaround solved the problem for my use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants