diff --git a/IPython/nbconvert/exporters/exporter.py b/IPython/nbconvert/exporters/exporter.py index 641b297..c6cde2f 100755 --- a/IPython/nbconvert/exporters/exporter.py +++ b/IPython/nbconvert/exporters/exporter.py @@ -54,6 +54,7 @@ default_filters = { 'get_lines': filters.get_lines, 'highlight2html': filters.highlight2html, 'highlight2latex': filters.highlight2latex, + 'ipython2python': filters.ipython2python, 'markdown2latex': filters.markdown2latex, 'markdown2rst': filters.markdown2rst, 'comment_lines': filters.comment_lines, diff --git a/IPython/nbconvert/filters/strings.py b/IPython/nbconvert/filters/strings.py index 99b4464..03799ab 100755 --- a/IPython/nbconvert/filters/strings.py +++ b/IPython/nbconvert/filters/strings.py @@ -19,6 +19,8 @@ templates. import re import textwrap from xml.etree import ElementTree + +from IPython.core.interactiveshell import InteractiveShell from IPython.utils import py3compat #----------------------------------------------------------------------------- @@ -32,7 +34,8 @@ __all__ = [ 'strip_dollars', 'strip_files_prefix', 'comment_lines', - 'get_lines' + 'get_lines', + 'ipython2python', ] @@ -150,3 +153,15 @@ def get_lines(text, start=None,end=None): # Return the right lines. return "\n".join(lines[start:end]) #re-join + +def ipython2python(code): + """Transform IPython syntax to pure Python syntax + + Parameters + ---------- + + code : str + IPython code, to be transformed to pure Python + """ + shell = InteractiveShell.instance() + return shell.input_transformer_manager.transform_cell(code) diff --git a/IPython/nbconvert/templates/python.tpl b/IPython/nbconvert/templates/python.tpl index fe812c9..b981752 100644 --- a/IPython/nbconvert/templates/python.tpl +++ b/IPython/nbconvert/templates/python.tpl @@ -7,7 +7,7 @@ {% block output_prompt %} # Out[{{cell.prompt_number}}]:{% endblock output_prompt %} -{% block input %}{{ cell.input }} +{% block input %}{{ cell.input | ipython2python }} {% endblock input %}