##// END OF EJS Templates
fix remove math space
fix remove math space

File last commit:

r8948:c38103d2 merge
r9882:ff7d5c0c
Show More
python.py
130 lines | 4.4 KiB | text/x-python | PythonLexer
David Warde-Farley
Introduce standard structure from coding guidelines in converters/.
r8789 """Notebook export to .py source code files.
Since Python export is provided in the notebook itself (provided by classes
in `IPython.nbformat`), this class serves mainly as a base class for other
converters that may wish to implement cell-type-specific behaviors.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2012, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# IPython imports
Matthias BUSSONNIER
all seem to convert again
r8620 from IPython.utils.text import indent
David Warde-Farley
Introduce standard structure from coding guidelines in converters/.
r8789
# Our own imports
Anthony Scopatz
convters sub-package use relative imports
r8933 from .base import Converter
from .utils import remove_ansi
Matthias BUSSONNIER
latex working
r8618
David Warde-Farley
PEP8-ify rest of the repository.
r8749
David Warde-Farley
Introduce standard structure from coding guidelines in converters/.
r8789 #-----------------------------------------------------------------------------
David Warde-Farley
More descriptive comment headers.
r8808 # Class declarations
David Warde-Farley
Introduce standard structure from coding guidelines in converters/.
r8789 #-----------------------------------------------------------------------------
Matthias BUSSONNIER
latex working
r8618 class ConverterPy(Converter):
"""
A converter that takes a notebook and converts it to a .py file.
What distinguishes this from PyWriter and PyReader in IPython.nbformat is
that subclasses can specify what to do with each type of cell.
Additionally, unlike PyWriter, this does not preserve the '# <markdown>'
opening and closing comments style comments in favor of a cleaner looking
python program.
Note:
Even though this produces a .py file, it is not guaranteed to be valid
python file, since the notebook may be using magics and even cell
magics.
"""
extension = 'py'
David Warde-Farley
Fix test, add highlight_source to ConverterPy....
r8809 def __init__(self, infile, highlight_source=False, show_prompts=True,
Bussonnier Matthias
fix text for exclude
r8945 show_output=True, **kw):
David Warde-Farley
Fix test, add highlight_source to ConverterPy....
r8809 # Note that highlight_source is meaningless here, so even if it
# is passed as True we ignore it and propagate False to the
# superclass.
super(ConverterPy, self).__init__(infile,
Bussonnier Matthias
fix text for exclude
r8945 highlight_source=False, **kw)
Matthias BUSSONNIER
latex working
r8618 self.show_prompts = show_prompts
self.show_output = show_output
@staticmethod
def comment(input):
"returns every line in input as commented out"
David Warde-Farley
PEP8-ify rest of the repository.
r8749 return "# " + input.replace("\n", "\n# ")
Matthias BUSSONNIER
latex working
r8618
def render_heading(self, cell):
David Warde-Farley
PEP8-ify rest of the repository.
r8749 return ['#{0} {1}'.format('#' * cell.level, cell.source), '']
Matthias BUSSONNIER
latex working
r8618
def render_code(self, cell):
Maximilian Albert
Replace ad-hoc/broken code with safe method to extract the prompt_number of a cell....
r8746 n = self._get_prompt_number(cell)
Matthias BUSSONNIER
latex working
r8618 if not cell.input:
return []
lines = []
if self.show_prompts:
Maximilian Albert
Replace ad-hoc/broken code with safe method to extract the prompt_number of a cell....
r8746 lines.extend(['# In[%s]:' % n])
Matthias BUSSONNIER
latex working
r8618 src = cell.input
lines.extend([src, ''])
if self.show_output:
David Warde-Farley
PEP8-ify rest of the repository.
r8749 if cell.outputs:
Maximilian Albert
Replace ad-hoc/broken code with safe method to extract the prompt_number of a cell....
r8746 lines.extend(['# Out[%s]:' % n])
Matthias BUSSONNIER
latex working
r8618 for output in cell.outputs:
conv_fn = self.dispatch(output.output_type)
lines.extend(conv_fn(output))
return lines
def render_markdown(self, cell):
return [self.comment(cell.source), '']
def render_raw(self, cell):
if self.raw_as_verbatim:
return [self.comment(indent(cell.source)), '']
else:
return [self.comment(cell.source), '']
def render_pyout(self, output):
lines = []
## if 'text' in output:
Maximilian Albert
Replace ad-hoc/broken code with safe method to extract the prompt_number of a cell....
r8746 ## lines.extend(['*Out[%s]:*' % self._get_prompt_number(cell), ''])
Matthias BUSSONNIER
latex working
r8618
# output is a dictionary like object with type as a key
if 'latex' in output:
pass
if 'text' in output:
lines.extend([self.comment(indent(output.text)), ''])
lines.append('')
return lines
def render_pyerr(self, output):
# Note: a traceback is a *list* of frames.
return [indent(remove_ansi('\n'.join(output.traceback))), '']
def _img_lines(self, img_file):
David Warde-Farley
PEP8-ify rest of the repository.
r8749 return [self.comment('image file: %s' % img_file), '']
Matthias BUSSONNIER
latex working
r8618
def render_display_format_text(self, output):
return [self.comment(indent(output.text))]
def _unknown_lines(self, data):
David Warde-Farley
PEP8-ify rest of the repository.
r8749 return [self.comment('Warning: Unknown cell' + str(data))]
Matthias BUSSONNIER
latex working
r8618
def render_display_format_html(self, output):
return [self.comment(output.html)]
def render_display_format_latex(self, output):
return []
def render_display_format_json(self, output):
return []
def render_display_format_javascript(self, output):
return []