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
nbconvert: Remove IPython magic commands from --format="python" output #3707
Comments
It's quite easy to write code that won't work if you just strip out IPython special syntax - e.g. use I think we should save the transformed code, which will always be syntactically valid Python, but will need to be run in IPython if you've used any IPython special features. And no, it's not a bug that ipython chokes on magic commands in a .py file, because it expects those to be valid Python. It can handle magic commands in |
@takluyver is right - they should not just be stripped. We should do one of two things (or both, possibly):
|
The
Isn't Oh, have to remove the |
So we need two separate formats; I propose |
that awk solution isn't actually general - you will learn that input transforms are much more complicated than that. |
I'd leave the name We could add an 'ipy' output format, but I don't think .ipy files are widely used, and we've never particularly promoted them. It's also not completely trivial, because we'd need to take blank lines out of things like cell magics, so that it could work out where they finished. In some cases, removing blank lines could change the effect of a cell magic. |
OK, it certainly sounds like a can of worms. But the main point to fix right now is that we claim, implicitly or Since, on the other hand, it suddenly becomes usable if the extension is On Sun, Jul 21, 2013 at 3:45 PM, Thomas Kluyver notifications@github.comwrote:
Dr. David P. Sanders Profesor Titular "A" / Associate Professor dpsanders@gmail.com Cubículo / office: #414, 4o. piso del Depto. de Física Tel.: +52 55 5622 4965 |
That the reason why nbconvert is in tech preview, there are many things at which nbconvert will not be good at, and that we will need to change. The export to I guess be can have a |
Also, one of the main reasons to export a .py file is so you can use it as Further, I don't know of any editor that will recognise a .ipy extension, On 22 July 2013 05:20, Matthias Bussonnier notifications@github.com wrote:
|
I am biased towards the filter/transform solution ( |
We do now perform input transforms in the Python template. |
There are still a few wrinkles that can crop up from the literal input transforms of certain magics, i.e.
This is problematic in ipython and python, because the notebook backend is only available in jupyter. It's possible to modify this pattern in postprocessing to something like:
This works in ipython, but get_ipython() is only available in ipython, so it won't work in python. Since I want the converted .py scripts to be runnable in python, the best option was to create a filter and a template to comment out these magics all together. Filter: import re
# This is a list of magics that we do not want in our output scripts
magics_regexs = [
r"get_ipython\(\).magic\(u\'pylab .*\'\)",
r"get_ipython\(\).magic\(u\'matplotlib .*\'\)"
]
def comment_magics(input, **kwargs):
lines = input.splitlines(True)
output = ""
for line in lines:
new_line = ""
for regex in magics_regexs:
if re.match(regex, line):
new_line = new_line + "#" + line
if new_line:
output = output + new_line
else:
output = output + line
return output Template:
|
@jclosure Can you provide an example of how to apply that filter to a .py file? (sorry not familiar with templates). Thanks! |
Found a better way @dpsanders and @nickweseman :
|
FWIW I wrapped @jclosure's filter to a command line executable https://github.com/NicWayand/hacks/blob/master/pyconvert.sh |
Just for future, if you want to use @jclosure filter, create a
And a template file
Then you can do:
|
Does somebody have a solution that's as nice and simple as @shett044's in #3707 for They have overhauled the way templates work, and the previous way does not work anymore. See also https://blog.jupyter.org/the-templating-system-of-nbconvert-6-47ea781eacd2 |
See for new format: jupyter/nbconvert#1428 (comment) |
Leaving a note here to adjust the solution above for cell magic (e.g. In def comment_magics(input, **kwargs):
if input.startswith("%"):
input = "# " + input
return input
# Export all the notebooks in the current directory to the sphinx_howto format
c = get_config()
c.NbConvertApp.notebooks = ["*.ipynb"]
c.Exporter.filters = {"comment_magics": comment_magics} In
In
Convert with:
|
nbconvert
with the python output format:ipython nbconvert --format="python"
currently leaves untouched the IPython magics, which get output to the
.py
.This is a bug, since the output is no longer a valid Python script.
Indeed, even trying to run the output from the command line with
ipython
chokes on the magic commands (which is presumably another, separate, bug?)Summary: the magics must be stripped out.
The text was updated successfully, but these errors were encountered: