nbconvert: `%matplotlib inline` fails when converted to script

This might be known, but didn’t see a bug for it. Consider a notebook test.pynb containing the magic %matplotlib inlineand converted to a script test.py:

jupyter nbconvert --to script test.ipynb

test.py now looks like

get_ipython().magic('matplotlib inline')

However, get_ipython() is undefined and the script will be unusable. nbconvert version 5.0.0

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Reactions: 15
  • Comments: 41 (15 by maintainers)

Most upvoted comments

I write 'exec(%matplotlib inline)' instead of get_ipython().magic('matplotlib inline')

I don’t get any error, but there is no output I mean there is no output images.

Anyone can fix it please ???

slightly off topic - but hypothetically - using iterm - someone could rig up the terminal to actually show image at the command line. Here’s the heavy lifting already done - just need to call out from python to command line https://github.com/johndpope/terminal-image-cli Examples $ image unicorn.jpg $ cat unicorn.jpg | image

I’m not sure that would work… but if you try running it using ipython as your interpreter (ipython your_file.py instead of python your_file.py) does that work?

On Wed, Dec 28, 2016 at 10:56 Jacob Peacock notifications@github.com wrote:

Yes, converting magic commands to regular python syntax makes sense to me. I think the bug here is that the python that results from that conversion is not usable. A notebook containing %matplotlib inline will not convert to a usable script; the script will raise an error that get_ipython() is not defined.

As you said, a possible fix would be to strip out the magic commands, but this has obvious shortcomings. Instead, perhaps nbconvert could add an import for get_ipython().

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jupyter/nbconvert/issues/503#issuecomment-269522800, or mute the thread https://github.com/notifications/unsubscribe-auth/ACXg6GPhZ5MlkaQ3DF6ekSDYYJscAGaIks5rMrDmgaJpZM4LWqCw .