-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
cannot write plotly figure to file with pickle #579
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
Comments
Is this issue resolved? |
👍 |
1 similar comment
👍 |
I figured out a workaround: instead of pickling the figure, serialize it to JSON.
|
Not sure if this is correct, but I wanted to share in case it could help someone else since it has worked for us so far. I added the following code to plotly's graph_objs.py PlotlyBase class :
(file located here: /lib/python2.7/site-packages/plotly/graph_objs/graph_objs.py) |
Thanks for bumping @cbarquest, I came up with a different workaround myself. In case people have issues with your solution, here's mine (copied from my post on SO: Since I wasn't able to get pickle working, The solution I came up with was to serialize the fig object to JSON, and then use plotly's "json chart schema" to build the plot from JSON.
|
So, there's still no fix for Pickle serialization? cbarquest solution didn't work for me
-> return |
We ran into this problem in the context of writing plotly figures into session data (a pickled file). We've since moved away from this approach, however, I do remember once seeing an error like @ColasDroin's. It went away after removing our code's and plotly's If this doesn't work for you, potentially you could try @dmarx's approach? We're now going this way as well, in our case passing data and layout separately as dictionaries and then having a javascript function assemble the final plotly figure. Hope this helps! |
Well I wanted to avoid json serialization because I wanted to keep the code as simple as possible, but in the end this is the approach I chose too, and it works. Thanks for the help! |
@dmarx thank you very much for your solution! Saved me a lot of time. One small note though - writing to a file:
didn't work when I tested it, because here we are writing string data, not binary (failed on Python 3.6, If you change it to simple 'w' mode:
everything works as a charm. Nevertheless thank you once again for your solution! |
Closed by #1191 with the introduction or proper |
import plotly.graph_objs as go
import pickle
file = open('myfile.pkl', 'wb')
a = go.Scatter(x=[1,2,3], y=[4,5,6])
figure = go.Figure(data = [a])
pickle.dump(figure, file)
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1376, in dump
Pickler(file, protocol).dump(obj)
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "xxx/bin/../lib/python2.7/copy_reg.py", line 84, in _reduce_ex
dict = getstate()
TypeError: 'NoneType' object is not callable
The text was updated successfully, but these errors were encountered: