Plotly.py: problem with image.save_as in Jupyter notebook

Created on 18 Nov 2015  路  6Comments  路  Source: plotly/plotly.py

Hi,

first of all, thanks for releasing your incredible work. I just downloaded and start using plot.ly this morning. I encountered a problem when trying to save a figure as a stand-alone file. The following code is run in Jupyter notebook.

import plotly
plotly.__version__

'1.9.0'
import plotly.plotly as py
fig = {'data': [{'x': [1, 2, 3], 'y': [3, 1, 5], 'type': 'bar'}]}
py.image.save_as(fig, 'test.png')

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.5/site-packages/plotly/plotly/plotly.py in get(figure_or_data, format, width, height, scale)
    675                         'json' in headers['content-type']):
--> 676                     return_data = json.loads(res.content)
    677                 else:

/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    311         raise TypeError('the JSON object must be str, not {!r}'.format(
--> 312                             s.__class__.__name__))
    313     if s.startswith(u'\ufeff'):

TypeError: the JSON object must be str, not 'bytes'

During handling of the above exception, another exception occurred:

PlotlyError                               Traceback (most recent call last)
<ipython-input-4-313e94b790ad> in <module>()
      1 import plotly.plotly as py
      2 fig = {'data': [{'x': [1, 2, 3], 'y': [3, 1, 5], 'type': 'bar'}]}
----> 3 py.image.save_as(fig, 'test.png')

/usr/local/lib/python3.5/site-packages/plotly/plotly/plotly.py in save_as(cls, figure_or_data, filename, format, width, height, scale)
    758             filename += '.' + format
    759 
--> 760         img = cls.get(figure_or_data, format, width, height, scale)
    761 
    762         f = open(filename, 'wb')

/usr/local/lib/python3.5/site-packages/plotly/plotly/plotly.py in get(figure_or_data, format, width, height, scale)
    678                     return_data = {'error': res.content}
    679             except:
--> 680                 raise exceptions.PlotlyError("The response "
    681                                              "from plotly could "
    682                                              "not be translated.")

PlotlyError: The response from plotly could not be translated.

Best regards,
Flavien.

Most helpful comment

Would be nice if there is a fix to save at last a standard png in the non-commercial "offline" edition.
Just noticed this ugly bug in my jupyter notbook.

py.image.save_as(plotly_fig, 'your_image_filename.png') 
...
TypeError                                 Traceback (most recent call last)
C:\Tools\Anaconda\envs\ETL\lib\site-packages\plotly\plotly\plotly.py in get(figure_or_data, format, width, height, scale)
    702                         'json' in headers['content-type']):
--> 703                     return_data = json.loads(res.content)
    704                 else:

C:\Tools\Anaconda\envs\ETL\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    311         raise TypeError('the JSON object must be str, not {!r}'.format(
--> 312                             s.__class__.__name__))
    313     if s.startswith(u'\ufeff'):

TypeError: the JSON object must be str, not 'bytes'

All 6 comments

You should "log-in" first with your credentials before calling py.image.save_as.

import plotly
plotly.tools.set_credentials_file(username='DemoAccount', api_key='lr1c37zw81')

then

import plotly.plotly as py
fig = {'data': [{'x': [1, 2, 3], 'y': [3, 1, 5], 'type': 'bar'}]}
py.image.save_as(fig, 'test.png')

OK, my apologies. It means that there is no way to save an image (except from the PNG button on top) in the open-source code, like SVG and so on?

I appears so. See #352

Fair enough. Thanks a lot!

Would be nice if there is a fix to save at last a standard png in the non-commercial "offline" edition.
Just noticed this ugly bug in my jupyter notbook.

py.image.save_as(plotly_fig, 'your_image_filename.png') 
...
TypeError                                 Traceback (most recent call last)
C:\Tools\Anaconda\envs\ETL\lib\site-packages\plotly\plotly\plotly.py in get(figure_or_data, format, width, height, scale)
    702                         'json' in headers['content-type']):
--> 703                     return_data = json.loads(res.content)
    704                 else:

C:\Tools\Anaconda\envs\ETL\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    311         raise TypeError('the JSON object must be str, not {!r}'.format(
--> 312                             s.__class__.__name__))
    313     if s.startswith(u'\ufeff'):

TypeError: the JSON object must be str, not 'bytes'

Update: Fully free/offline/open source static image export is now possible using the orca integration introduced in version 3.2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

entron picture entron  路  4Comments

ghtmtt picture ghtmtt  路  5Comments

astrojuanlu picture astrojuanlu  路  4Comments

fcollonval picture fcollonval  路  3Comments

jisaacso picture jisaacso  路  4Comments