chrisjsewell / ipypublish

A workflow for creating and editing publication ready scientific reports and presentations, from one or more Jupyter Notebooks, without leaving the browser!
http://ipypublish.readthedocs.io
BSD 3-Clause "New" or "Revised" License
224 stars 37 forks source link

Python Markdown on PDF export #55

Open amichuda opened 5 years ago

amichuda commented 5 years ago

when doing nbconvert to PDF for a notebook, the Python Markdown Preprocessor Jupyter extension, runs and renders code in markdown cells. When doing the same with nbpublish, the code isn't run. Is there some way to get this functionality with nbpublish?

chrisjsewell commented 5 years ago

I assume you are referring this extension? If so, this is the actual file containing the preprocessor. You should be able to follow the instructions here and add the PyMarkdownPreprocessor to the converter script. For example, creating a script based on latex_ipypublish_main.py:

"""latex article in the main ipypublish format:
- only output cells with metadata tags are used
- code, figures, tables and code are formatted accordingly
"""
from ipypublish.filters.ansi_listings import ansi2listings
from ipypublish.filters.filters import remove_dollars, first_para, create_key, dict_to_kwds, is_equation
from ipypublish.latex.create_tplx import create_tplx
from ipypublish.latex.ipypublish import biblio_natbib as bib
from ipypublish.latex.ipypublish import contents_framed_code as code
from ipypublish.latex.ipypublish import contents_output as output
from ipypublish.latex.ipypublish import doc_article as doc
from ipypublish.latex.ipypublish import front_pages as title
from ipypublish.latex.standard import standard_definitions as defs
from ipypublish.latex.standard import standard_packages as package
from ipypublish.preprocessors.latex_doc_captions import LatexCaptions
from ipypublish.preprocessors.latex_doc_links import LatexDocLinks
from ipypublish.preprocessors.split_outputs import SplitOutputs
from pre_pymarkdown import PyMarkdownPreprocessor

oformat = 'Latex'
template = create_tplx([p.tplx_dict for p in
                        [package, defs, doc, title, bib, output, code]])

_filters = {'remove_dollars': remove_dollars,
            'first_para': first_para,
            'create_key': create_key,
            'dict_to_kwds': dict_to_kwds,
            'ansi2listings': ansi2listings,
            'is_equation': is_equation}

config = {'TemplateExporter.filters': _filters,
          'Exporter.filters': _filters,
          'SplitOutputs.split': True,
          'Exporter.preprocessors': [PyMarkdownPreprocessor, SplitOutputs, LatexDocLinks, LatexCaptions]}

Let me know how you get on :)

chrisjsewell commented 5 years ago

The only thing I noticed is that in PyMarkdownPreprocessor, it is searching within the markdown cells metadata for a variables key. This isn't normally present, but I assume the extension adds it to the ipynb file when you are using it. If you have any issues, perhaps drop a minimal example of a notebook you are trying to convert here.

chrisjsewell commented 5 years ago

@lordflaron please note that the plugin API has changed in v0.7.0. The plugin file should now be a JSON file (see this documentation for a conversion tool) and will look like this:

{
  "description": [
    "latex article in the main ipypublish format:",
    "- only output cells with metadata tags are used",
    "- code, figures, tables and code are formatted accordingly"
  ],
  "exporter": {
    "class": "nbconvert.exporters.LatexExporter",
    "filters": {
      "remove_dollars": "ipypublish.filters.filters.remove_dollars",
      "first_para": "ipypublish.filters.filters.first_para",
      "create_key": "ipypublish.filters.filters.create_key",
      "dict_to_kwds": "ipypublish.filters.filters.dict_to_kwds",
      "ansi2listings": "ipypublish.filters.ansi_listings.ansi2listings",
      "is_equation": "ipypublish.filters.filters.is_equation",
      "strip_ext": "ipypublish.filters.filters.strip_ext"
    },
    "preprocessors": [ 
      {
         "class": "pre_pymarkdown.PyMarkdownPreprocessor"
      },
      {
        "class": "ipypublish.preprocessors.split_outputs.SplitOutputs",
        "args": {
          "split": true
        }
      },
      {
        "class": "ipypublish.preprocessors.latex_doc_links.LatexDocLinks",
        "args": {
          "metapath": "${meta_path}",
          "filesfolder": "${files_path}"
        }
      },
      {
        "class": "ipypublish.preprocessors.latex_doc_captions.LatexCaptions",
        "args": {}
      }
    ],
    "other_args": {}
  },
  "template": {
    "outline": {
      "module": "ipypublish.templates.outline_schemas",
      "file": "latex_outline.latex.j2"
    },
    "segments": [
      {
        "module": "ipypublish.templates.segments",
        "file": "std-standard_packages.latex-tpl.json"
      },
      {
        "module": "ipypublish.templates.segments",
        "file": "std-standard_definitions.latex-tpl.json"
      },
      {
        "module": "ipypublish.templates.segments",
        "file": "ipy-doc_article.latex-tpl.json"
      },
      {
        "module": "ipypublish.templates.segments",
        "file": "ipy-front_pages.latex-tpl.json"
      },
      {
        "module": "ipypublish.templates.segments",
        "file": "ipy-biblio_natbib.latex-tpl.json"
      },
      {
        "module": "ipypublish.templates.segments",
        "file": "ipy-contents_output.latex-tpl.json"
      },
      {
        "module": "ipypublish.templates.segments",
        "file": "ipy-contents_framed_code.latex-tpl.json"
      }
    ]
  }
}
dabrze commented 5 years ago
    "preprocessors": [ 
      {
         "class": "pre_pymarkdown.PyMarkdownPreprocessor"
      },

For anyone else trying to automatically convert Python Markdown, the above template fragment procided by @chrisjsewell should be changed to (at least that's what I had to do):

    "preprocessors": [ 
      {
         "class": "jupyter_contrib_nbextensions.nbconvert_support.PyMarkdownPreprocessor",
         "args": {}
      },