logo

mdipynb filter

← Back to Filter List

mdipynb


Generate a Jupyter (IPython) notebook from markdown with embedded code.

Aliases for this filter

  • mdipynb
  • mdjup

Converts from file formats:

  • .md

To file formats:

  • .ipynb

Available settings:

SettingDescriptionDefault
add-new-filesBoolean or list of extensions/patterns to match.False
added-in-versionDexy version when this filter was first available.
additional-doc-filtersFilters to apply to additional documents created as side effects.{}
additional-doc-settingsSettings to apply to additional documents created as side effects.{}
collapsedWhether to collapse code blocks by default.False
data-typeAlias of custom data class to use to store filter output.generic
examplesTemplates which should be used as examples for this filter.[]
exclude-add-new-filesList of patterns to skip even if they match add-new-files.[]
exclude-new-files-from-dirList of directories to skip when adding new files.[]
extFile extension to output.None
extension-mapDictionary mapping input extensions to default output extensions.None
helpHelpstring for plugin.Generate a Jupyter (IPython) notebook from markdown with embedded code.
input-extensionsList of extensions which this filter can accept as input.['.md']
keep-originalsWhether, if additional-doc-filters are specified, the original unmodified docs should also be added.False
languageDefault programming language for code blocks.python
mkdirA directory which should be created in working dir.None
mkdirsA list of directories which should be created in working dir.[]
nameName of notebook.None
nbformatSetting to use for IPython nbformat setting3
nbformat-minorSetting to use for IPython nbformat_minor setting.0
nodocWhether filter should be excluded from documentation.False
outputWhether to output results of this filter by default by reporters such as 'output' or 'website'.True
output-extensionsList of extensions which this filter can produce as output.['.ipynb']
override-workspace-exclude-filtersIf True, document will be populated to other workspaces ignoring workspace-exclude-filters.False
pprintWhether to pretty print JSON.True
preserve-prior-data-classWhether output data class should be set to match the input data class.False
require-outputShould dexy raise an exception if no output is produced by this filter?True
tagsTags which describe the filter.[]
variablesA dictionary of variable names and values to make available to this filter.{}
varsA dictionary of variable names and values to make available to this filter.{}
workspace-exclude-filtersFilters whose output should be excluded from workspace.['pyg']
workspace-includesIf set to a list of filenames or extensions, only these will be populated to working dir.None
Filter Source Code
class MarkdownJupyter(MarkdownSections):
    """
    Generate a Jupyter (IPython) notebook from markdown with embedded code.
    """
    aliases = ['mdipynb', 'mdjup']
    _settings = {
            "output-extensions" : [".ipynb"],
            "nbformat" : ("Setting to use for IPython nbformat setting", 3),
            "nbformat_minor" : ("Setting to use for IPython nbformat_minor setting.", 0),
            "name" : ("Name of notebook.", None),
            "collapsed" : ("Whether to collapse code blocks by default.", False),
            }
    
    def process_code(self, source, language, metadata=None):
        if language is None:
            raise Exception("no language specified")
        return {
                "cell_type" : "code",
                "collapsed" : self.setting('collapsed'),
                "metadata" : metadata or {},
                "language" : language,
                "input" : source,
                "outputs" : [],
                "prompt_number" : None
                }

    def process_heading(self, level, text, metadata = None):
        return {
                "cell_type" : "heading",
                "level" : level,
                "metadata" : metadata or {},
                "source" : [text]
                }

    def process_prose(self, source):
        return {
                "cell_type" : "markdown",
                "metadata" : {},
                "source" : [
                    "\n".join(source)
                    ]
                }

    def process_text(self, input_text):
        workbook_name = self.setting("name")
        cells = self.process_sections(input_text)

        notebook = {
            "nbformat" : self.setting('nbformat'),
            "nbformat_minor" : self.setting('nbformat-minor'),
            "metadata" : {
                "name" : workbook_name
                },
            "worksheets" : [{
                "cells" : cells
                }]
            }

        if self.setting('pprint'):
            return json.dumps(notebook, indent=4, sort_keys=True)
        else:
            return json.dumps(notebook)

Content © 2020 Dr. Ana Nelson | Site Design © Copyright 2011 Andre Gagnon | All Rights Reserved.