Filters are for use with the TemplateExporter exporter. They provide a way for you transform notebook contents to a particular format depending on the template you are using. For example, when converting to HTML, you would want to use the ansi2html() function to convert ANSI colors (from e.g. a terminal traceback) to HTML colors.

See also

API documentation for the various exporter classes

Add an anchor-link to an html header

For use on markdown headings

nbconvert.filters.add_prompts(code, first='>>> ', cont='... ')

Add prompts to code snippets


Convert ANSI colors to HTML colors.

Parameters:text (unicode) – Text containing ANSI colors to convert to HTML

Convert ANSI colors to LaTeX colors.

Parameters:text (unicode) – Text containing ANSI colors to convert to LaTeX

ensure a string is ascii


Parse citations in Markdown cells.

This looks for HTML tags having a data attribute names data-cite and replaces it by the call to LaTeX cite command. The tranformation looks like this:

<cite data-cite=”granger”>(Granger, 2013)</cite>



Any HTML tag can be used, which allows the citations to be formatted in HTML in any manner.

nbconvert.filters.comment_lines(text, prefix='# ')

Build a Python comment line from input text.

  • text (str) – Text to comment out.
  • prefix (str) – Character to append to the start of each line.

Escape characters that may conflict with latex.

Parameters:text (str) – Text containing characters that may conflict with Latex
class nbconvert.filters.DataTypeFilter(**kw)

Returns the preferred display format

nbconvert.filters.get_lines(text, start=None, end=None)

Split the input text into separate lines and then return the lines that the caller is interested in.

  • text (str) – Text to parse lines from.
  • start (int, optional) – First line to grab from.
  • end (int, optional) – Last line to grab from.
class nbconvert.filters.Highlight2HTML(pygments_lexer=None, **kwargs)
class nbconvert.filters.Highlight2Latex(pygments_lexer=None, **kwargs)

extract inner text from html

Analog of jQuery’s $(element).text()

nbconvert.filters.indent(instr, nspaces=4, ntabs=0, flatten=False)

Indent a string a given number of spaces or tabstops.

indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.

  • instr (basestring) – The string to be indented.
  • nspaces (int (default: 4)) – The number of spaces to be indented.
  • ntabs (int (default: 0)) – The number of tabs to be indented.
  • flatten (bool (default: False)) – Whether to scrub existing indentation. If True, all lines will be aligned to the same indentation. If False, existing indentation will be strictly increased.


Return type:

string indented by ntabs and nspaces.


Transform IPython syntax to pure Python syntax

Parameters:code (str) – IPython code, to be transformed to pure Python

Convert a markdown string to HTML using mistune

nbconvert.filters.markdown2latex(source, markup='markdown', extra_args=None)

Convert a markdown string to LaTeX via pandoc.

This function will raise an error if pandoc is not installed. Any error messages generated by pandoc are printed to stderr.


out – Output as returned by pandoc.

Return type:


nbconvert.filters.markdown2rst(source, extra_args=None)

Convert a markdown string to ReST via pandoc.

This function will raise an error if pandoc is not installed. Any error messages generated by pandoc are printed to stderr.

Parameters:source (string) – Input string, assumed to be valid markdown.
Returns:out – Output as returned by pandoc.
Return type:string

Turn a file path into a URL


Turn a path into posix-style path/to/etc

Mainly for use in latex on Windows, where native Windows paths are not allowed.


Prevent presence of enumerate or itemize blocks in latex headings cells


Remove ANSI escape codes from text.

Parameters:source (str) – Source to remove the ANSI from

Remove all dollar symbols from text

Parameters:text (str) – Text to remove dollars from

Fix all fake URLs that start with files/, stripping out the files/ prefix. Applies to both urls (for html) and relative paths (for markdown paths).

Parameters:text (str) – Text in which to replace ‘src=”files/real...’ with ‘src=”real...’
nbconvert.filters.wrap_text(text, width=100)

Intelligently wrap text. Wrap text without breaking words if possible.

  • text (str) – Text to wrap.
  • width (int, optional) – Number of characters to wrap to, default 100.