Skip to main content
`Ctrl`+`K`

# Filters

Filters are for use with the `nbconvert.exporters.templateexporter.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

Exporters

  API documentation for the various exporter classes

nbconvert.filters.add_anchor(html, anchor_link_text='¶')

  Add an id and an anchor-link to an html header

  For use on markdown headings

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

  Add prompts to code snippets

nbconvert.filters.ansi2html(text)

  Convert ANSI colors to HTML colors.

  Parameters:

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

nbconvert.filters.ansi2latex(text)

  Convert ANSI colors to LaTeX colors.

  Parameters:

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

nbconvert.filters.ascii_only(s)

  ensure a string is ascii

nbconvert.filters.citation2latex(s)

  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 transformation looks like this:

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

  Becomes

      \cite{granger}

  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.

  Parameters:

    - text (str) – Text to comment out.

    - prefix (str) – Character to append to the start of each line.

nbconvert.filters.escape_latex(text)

  Escape characters that may conflict with latex.

  Parameters:

    text (str) – Text containing characters that may conflict with Latex

class nbconvert.filters.DataTypeFilter(**kwargs: Any)

  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.

  Parameters:

    - text (str) – Text to parse lines from.

    - start (int, optional) – First line to grab from.

    - end (int, optional) – Last line to grab from.

nbconvert.filters.convert_pandoc(source, from_format, to_format, extra_args=None)

  Convert between any two formats using 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 in from_format.

    - from_format (string) – Pandoc format of source.

    - to_format (string) – Pandoc format for output.

  Returns:

    out – Output as returned by pandoc.

  Return type:

    string

class nbconvert.filters.Highlight2HTML(**kwargs: Any)

  Convert highlighted code to html.

class nbconvert.filters.Highlight2Latex(**kwargs: Any)

  Convert highlighted code to latex.

nbconvert.filters.html2text(element)

  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.

  Parameters:

    - 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.

  Returns:

    str|unicode

  Return type:

    string indented by ntabs and nspaces.

nbconvert.filters.ipython2python(code)

  Transform IPython syntax to pure Python syntax

  Parameters:

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

nbconvert.filters.markdown2html(source: str) → str

  Convert a markdown string to HTML using mistune

nbconvert.filters.markdown2latex(source, markup='markdown+lists_without_preceding_blankline', 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.

  Parameters:

    - source (string) – Input string, assumed to be valid markdown.

    - markup (string) – Markup used by pandoc’s reader default : pandoc extended markdown (see https://pandoc.org/README.html#pandocs-markdown)

  Returns:

    out – Output as returned by pandoc.

  Return type:

    string

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

nbconvert.filters.path2url(path)

  Turn a file path into a URL

nbconvert.filters.posix_path(path)

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

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

nbconvert.filters.prevent_list_blocks(s)

  Prevent presence of enumerate or itemize blocks in latex headings cells

nbconvert.filters.strip_ansi(source)

  Remove ANSI escape codes from text.

  Parameters:

    source (str) – Source to remove the ANSI from

nbconvert.filters.strip_dollars(text)

  Remove all dollar symbols from text

  Parameters:

    text (str) – Text to remove dollars from

nbconvert.filters.strip_files_prefix(text)

  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.

  Parameters:

    - text (str) – Text to wrap.

    - width (int, optional) – Number of characters to wrap to, default 100.