-
Notifications
You must be signed in to change notification settings - Fork 10
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
Converting notebook to .py script fails when using %matplotlib inline - known issue #31
Comments
Just to clarify for those coming across this issue, the failure isn't with the conversion itself (which does not fail, and converts cell magics to python function calls), but with using python to execute the result of the conversion. |
All The problem with It should be mentioned that if students are using matplotlib 2.0+ they shouldn't need to explicitly invoke a backend when executed from a notebook (I believe it defaults to matplotlib) and so this shouldn't cause any problems. |
For the record, that didn't work either for the workshop we just held. (Using the Anaconda installer etc.)
I humbly submit that this be reconsidered. An extracted Python script should run as any Python script would - under Python. It would be very confusing to have to teach students that yes, the Python extractor extracts Python, but it's not 'real' Python but 'iPython'-Python. Perhaps this is totally fine for the developers. However, as a teacher I would then simply not teach Python extraction, and refer students who ask about it anyway (which they'll almost certainly do) to the NBconvert developers.
That's not entirely true. Obviously, when converting to either Markdown or HTML the images do get created just fine (using default choices that would work just as well for a Python script), so what to do with them if there is no backend at the time of viewing has already been solved.
I can't even tell myself which version of matplotlib I'm using, let alone controlling which one I'm using. So again, this may be fine for developers, but it's a non-starter when teaching unless it's guaranteed to work through the installer. |
@hlapp my intuition is that these shortcuts etc are meant more for developers as shortcuts to make things faster for doing interactive computing. I agree totally that it's really annoying when you have to call @mpacer I just tried opening a new notebook and running a simple viz plot, and it doesn't seem like |
@choldgraf I was basing that off of: from @takluyver
Interesting @Carreau & @NelleV, could you explain where my confusion lies? |
I've just been going through that thread - going to take discussion of this issue over there because I think it'll be more actionable there! |
I think I had overlooked a detail: with mpl 2, it detects that it's in an IPython kernel and uses the inline backend by default, but it doesn't set up our post-execute hook which displays any unclosed mpl figures. So it works if you display a figure object by normal means, but it doesn't automatically show up whenever you call plotting functions. |
ah I see...so what is the preferred user behavior in this case? To just put |
(That was informed guesswork, btw - don't take it as gospel) We kind of subverted our normal display model to make using mpl more convenient, and what you're seeing now is more like the general display system. I'm not really sure what recommended behaviour is in this case. For now, I'd put |
@hlapp There are a number of different issues here, I'll try to tackle them in different responses to keep them well contained.
"All ipython magics that are in a notebook are converted to call get_ipython(), which will be executable using ipython and not python" is true, but it is not true that all calls to See the following abbreviated tracebacks (on a notebook with only one cell with For
For
|
Because magics do not have a meaning outside of @takluyver @Carreau @minrk Why is it called a |
ok so I can confirm that calling @hlapp what I think we should do in the short term is ensure that students are using If for some reason a student can't use MPL 2.0, then we'd just need to tell them to use @takluyver or @mpacer do you see a problem with this strategy? |
Because many notebooks don't use magics, in which case it is, for practical purposes, a Python exporter. And if you're exporting to a script, you're probably doing so to run it with
Have you checked with multiple plotting commands in one cell (e.g. add a legend)? That's where I'm concerned you may see the plot multiple times. |
OK, that looks like a workable approach at present, then. :-) |
ok, I'm opening an issue in another repository that actually has |
Magic commands require the IPython environment to be able to be used as they specifically act as simpler interfaces to much more complicated commands. So it is not that nbconvert doesn't extract "classic" Python (which I prefer to "real", feel free to substitute while reading), but that in using an IPython magic, you are writing code that needs IPython to function. Put simply: If students are not writing "classic" Python, there is no way to extract "classic" Python scripts. I don't fully understand why the solution would not be to teach students that if they use commands that require the For getting around this problem, you could instead teach them to use one of the other methods for specifying a matplotlib backend (see this link). However, that still would not get around the fact that |
I'll try to address different parts about this separately.
Unless you pass The images that I think you are referring to are images that are visible in the notebook itself are visible as outputs. These are stored as encoded data in the notebook itself (which is why many people clear outputs automatically when using notebooks with version control). Because they are already available in the outputs, the HTML and Markdown converters do not need to execute the code that would have generated those outputs. They just collect the data from the notebook itself. The HTML exporter (by default) embeds these images as dataURIs that can be directly passed and do not need to create any side-effect files (see Extracting Figures using the HTML Exporter for a description). The Markdown exporter has no way (as far as I know) of embedding images as dataURIs and so it creates an accessory folder to store the output figures and includes links to those figures using standard operating system file paths. In the case where you do want to execute the code and rerun your analyses before exporting, the code would be run through the kernel that the notebook has access to, thereby avoiding any of the issues around backends that are compatible only in an interactive context because they will be run in a (headless) interactive context. At the time of exporting to a Python script, you could export all of the current outputs using the > default choices that would work just as well for a Python script However, from the conversation (and from discussions with @JamiesHQ) I think that that is not what you wanted. What you wanted is the ability to extract a python script that would write out images as files when it is run. In that context, there are no "default choices that would work just as well for a Python script" because these are not the kinds of choices that the HTML or Markdown exporters make (see above), and thus no choices exist to provide default choices. If you want a script to write out files, you need to write that explicitly and that holds whether you do this in the notebook or in a script. All nbconvert does is ask whether the notebook has access to the files. If the
The issue is not that there is no backend, but that the declared backend is not available when run from a python script. Matplotlib has default values when no backend is declared, but I believe it will always have a backend (even if just writing files to the file system). However, that is a matplotlib issue so I'm a little fuzzy on the details. |
If you use import matplotlib
matplotlib.__version__ that is the best way to trace down which version of which version of matplotlib you are using If you have used Conda, you can also run If you need to install a new version of a python package you can use So, for example, |
@JamiesHQ When converting from IPYNB to python use following direction:
|
@shett044 if you choose Export->Python from the Jupyter menu (which I think invokes |
@hlapp nope, this would require us adding the template described above to this repository, then instructing people to convert to python with the command given above. I think a workable solution here is to use |
Interesting. If you know how best to work this into the lesson, don't hesitate 😊 |
I suggested how best to do this in this comment above. Can change the notebooks to do this in a couple of weeks, but for now I am on my honeymoon so shouldn't be checking github in the first place :-) |
Ah yes. Sorry I missed that.
Exactly!!! What are you doing here 😃😏 |
This is a known Jupyter issue: jupyter/nbconvert#503, jupyter/nbconvert#507
cell magics are not supported by Python, only by IPython. Jupyter team is working on the machinery to make this work.
@hlapp
The text was updated successfully, but these errors were encountered: