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

Avoid crash in iframe renderers when running outside iPython #1723

Merged
merged 1 commit into from
Aug 31, 2019

Conversation

mpaolini
Copy link
Contributor

when running

echo '{"hour":"2019-01-01T00:00:00Z","perf":1}' | python <(                     
echo "                                                                                                                        
import pandas as pd                                                                                                           
from plotly import express as px                                                                                              
import sys                                                                                                                    
data = pd.read_json(sys.stdin.read(), lines=True, convert_dates=['hour'])                                                     
fig = px.line(data, x='hour', y='perf')                                                                                       
fig.show(renderer='iframe')                                                                                                   
")

if ipython is not installed you get:

Traceback (most recent call last):
  File "/dev/fd/63", line 7, in <module>
  File "/home/marco/src/plotly.py/packages/python/plotly/plotly/basedatatypes.py", line 2658, in show
    return pio.show(self, *args, **kwargs)
  File "/home/marco/src/plotly.py/packages/python/plotly/plotly/io/_renderers.py", line 376, in show
    bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
  File "/home/marco/src/plotly.py/packages/python/plotly/plotly/io/_renderers.py", line 296, in _build_mime_bundle
    bundle.update(renderer.to_mimebundle(fig_dict))
  File "/home/marco/src/plotly.py/packages/python/plotly/plotly/io/_base_renderers.py", line 564, in to_mimebundle
    filename = self.build_filename()
  File "/home/marco/src/plotly.py/packages/python/plotly/plotly/io/_base_renderers.py", line 601, in build_filename
    ip = IPython.get_ipython()
AttributeError: 'NoneType' object has no attribute 'get_ipython'

if instead ipython is installed

Traceback (most recent call last):
  File "/dev/fd/63", line 7, in <module>
  File "/home/marco/src/plotly.py/packages/python/plotly/plotly/basedatatypes.py", line 2658, in show
    return pio.show(self, *args, **kwargs)
  File "/home/marco/src/plotly.py/packages/python/plotly/plotly/io/_renderers.py", line 376, in show
    bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
  File "/home/marco/src/plotly.py/packages/python/plotly/plotly/io/_renderers.py", line 296, in _build_mime_bundle
    bundle.update(renderer.to_mimebundle(fig_dict))
  File "/home/marco/src/plotly.py/packages/python/plotly/plotly/io/_base_renderers.py", line 564, in to_mimebundle
    filename = self.build_filename()
  File "/home/marco/src/plotly.py/packages/python/plotly/plotly/io/_base_renderers.py", line 602, in build_filename
    cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1
AttributeError: 'NoneType' object has no attribute 'history_manager'

@jonmmease
Copy link
Contributor

Hi @mpaolini, thanks for the PR. I'm happy to merge these changes, but I would like to understand your usecase a little better. Is the iframe renderer useful for you when running outside of IPython?

@mpaolini
Copy link
Contributor Author

Hi, yes I use it in some “headless” scripts (as the one above) to generate charts programmatically . Ipython is not required in this scenarios , and often it is not installed for this reason.

Furthermore Ipython does not work well with <() bash redirection because it does not recognise the temporary file as a python script. So the above command only works on vanilla python

hope this clarifies it

@jonmmease
Copy link
Contributor

Hi @mpaolini, sorry for the delay. Are you aware of the plotly.io.write_html function? This is the generally recommended way to export figures to HTML files. But again, no harm in avoiding crash here. Thanks for the PR!

@jonmmease jonmmease merged commit 01a78d3 into plotly:master Aug 31, 2019
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

Successfully merging this pull request may close these issues.

2 participants