Dynamic Jupyter Notebook markdown - lmmx/devnotes GitHub Wiki

You can mix variables into the markdown cells of Jupyter notebooks

I had 2 locations for jupyter_nbconvert_config.py :

  • ~/anaconda3/lib/python3.5/site-packages/nbconvert/tests/files/jupyter_nbconvert_config.py
  • ~/anaconda3/pkgs/nbconvert-4.0.0-py35_0/lib/python3.5/site-packages/nbconvert/tests/files/jupyter_nbconvert_config.py

I chose the former, although this is very clearly going to break when I next update Python. 😓

I added:

#python markdown extension
#see https://github.com/ipython-contrib/IPython-notebook-extensions/wiki/python-markdown
c.Exporter.preprocessors = [ 'pymdpreprocessor.PyMarkdownPreprocessor' ]

to the end of the file

...but later noticed the rename in a recent nbextensions repo update and changed the last line to

c.Exporter.preprocessors = [ 'pre_pymarkdown.PyMarkdownPreprocessor' ]

Note this warning:

Caution: There is no restriction in the expression you can embed in {{ }}. Be careful as you might crash your browser if you return too large datasets.

I then downloaded zip [defaulting to master branch, for IPython 4.x] of the ipython-contrib/IPython-notebook-extensions repo and extracted to ~/.ipython/nbextensions/IPython-notebook-extensions

...ended in

Package: nbextensions-master-py35_0
source tree in: /home/louis/anaconda3/conda-bld/work
+ python setup.py install
Traceback (most recent call last):
  File "setup.py", line 10, in <module>
    import IPython
  File "/home/louis/anaconda3/envs/_build/lib/python3.5/site-packages/IPython/__init__.py", line 47, in <module>
    from .core.application import Application
  File "/home/louis/anaconda3/envs/_build/lib/python3.5/site-packages/IPython/core/application.py", line 24, in <module>
    from IPython.core import release, crashhandler
  File "/home/louis/anaconda3/envs/_build/lib/python3.5/site-packages/IPython/core/crashhandler.py", line 28, in <module>
    from IPython.core import ultratb
  File "/home/louis/anaconda3/envs/_build/lib/python3.5/site-packages/IPython/core/ultratb.py", line 116, in <module>
    from IPython.utils import path as util_path
  File "/home/louis/anaconda3/envs/_build/lib/python3.5/site-packages/IPython/utils/path.py", line 19, in <module>
    from IPython.utils.process import system
  File "/home/louis/anaconda3/envs/_build/lib/python3.5/site-packages/IPython/utils/process.py", line 19, in <module>
    from ._process_posix import system, getoutput, arg_split, check_pid
  File "/home/louis/anaconda3/envs/_build/lib/python3.5/site-packages/IPython/utils/_process_posix.py", line 24, in <module>
    import pexpect
  File "/home/louis/anaconda3/envs/_build/lib/python3.5/site-packages/pexpect/__init__.py", line 69, in <module>
    from .exceptions import ExceptionPexpect, EOF, TIMEOUT
ImportError: No module named 'pexpect.exceptions'
Command failed: /bin/bash -x -e /home/louis/anaconda3/conda-bld/work/conda_build.sh

(pexpect is installed with pip3 in anaconda3 environment)

😞

Alternative approach to Conda

./setup.py install

./setup.py: line 2: $'\r': command not found
from: can't read /var/mail/jupyter_core.paths
from: can't read /var/mail/traitlets.config.loader

...maybe not then

...without trying to install any other extensions

Copied the IPython-notebook-extensions/extensions/pre_pymarkdown.py file into $PYTHONPATH as recommended

In order to have nbconvert show the preprocessed output, copy the pymdpreprocessor.py file to a location in your PYTHONPATH

(it must have been renamed to pre_pymarkdown.py to indicate preprocessor)

  • PYTHONPATH is ~/lib/root for me
chmod +x pre_pymarkdown.py
cp pre_pymarkdown.py ~/lib/root/

🎉 Installed 🎉

>>> import pre_pymarkdown
>>> pre_pymarkdown.
pre_pymarkdown.CSSHTMLHeaderPreprocessor(    pre_pymarkdown.__lt__(
pre_pymarkdown.ClearOutputPreprocessor(      pre_pymarkdown.__name__
pre_pymarkdown.ConvertFiguresPreprocessor(   pre_pymarkdown.__ne__(
pre_pymarkdown.ExecutePreprocessor(          pre_pymarkdown.__new__(
pre_pymarkdown.ExtractOutputPreprocessor(    pre_pymarkdown.__package__
pre_pymarkdown.HighlightMagicsPreprocessor(  pre_pymarkdown.__reduce__(
pre_pymarkdown.LatexPreprocessor(            pre_pymarkdown.__reduce_ex__(
pre_pymarkdown.Preprocessor(                 pre_pymarkdown.__repr__(
pre_pymarkdown.PyMarkdownPreprocessor(       pre_pymarkdown.__setattr__(
pre_pymarkdown.RevealHelpPreprocessor(       pre_pymarkdown.__sizeof__(
pre_pymarkdown.SVG2PDFPreprocessor(          pre_pymarkdown.__spec__
pre_pymarkdown.__cached__                    pre_pymarkdown.__str__(
pre_pymarkdown.__class__(                    pre_pymarkdown.__subclasshook__(
pre_pymarkdown.__delattr__(                  pre_pymarkdown.base
pre_pymarkdown.__dict__                      pre_pymarkdown.clearoutput
pre_pymarkdown.__dir__(                      pre_pymarkdown.coalesce_streams(
pre_pymarkdown.__doc__                       pre_pymarkdown.coalescestreams
pre_pymarkdown.__eq__(                       pre_pymarkdown.convertfigures
pre_pymarkdown.__file__                      pre_pymarkdown.csshtmlheader
pre_pymarkdown.__format__(                   pre_pymarkdown.execute
pre_pymarkdown.__ge__(                       pre_pymarkdown.extractoutput
pre_pymarkdown.__getattribute__(             pre_pymarkdown.highlightmagics
pre_pymarkdown.__gt__(                       pre_pymarkdown.latex
pre_pymarkdown.__hash__(                     pre_pymarkdown.re
pre_pymarkdown.__init__(                     pre_pymarkdown.revealhelp
pre_pymarkdown.__le__(                       pre_pymarkdown.svg2pdf
pre_pymarkdown.__loader__ 

...I stumbled upon this issue comment from 1st Nov 2015, not recommending Conda installation, just simply python installation (my python is actually in the anaconda3 environment...)

The easiest way is to install all extensions using python setup.py install, start the jupyter notebook and then go to http://localhost:8888/nbextensions where you should see this:

Now you can activate/deactivate all available extensions. Using jupyter nbextensions enable leads to the difficulties you encountered, i.e. you specify an extension name that is not the name of the actual file.

  • I'd already downloaded the master branch and unzipped to ~/.ipython/nbextensions/IPython-notebook-extensions
  • it installed without problem
  • ticking the Python Markdown option at the link above enabled the config (in ~/.jupyter/nbconfig/notebook.json) :
{
  "load_extensions": {
    "usability/python-markdown/main": true,
    "python-markdown/main": true
  }
}

plus added a jupyter_notebook_config.json directly under `~/.jupyter

{
  "Exporter": {
    "preprocessors": [
      "pre_codefolding.CodeFoldingPreprocessor",
      "pre_pymarkdown.PyMarkdownPreprocessor"
    ],
    "template_path": [
      "/home/louis/.local/share/jupyter/templates"
    ]
  },
  "NbConvertApp": {
    "postprocessor_class": "post_embedhtml.EmbedPostProcessor"
  },
  "NotebookApp": {
    "server_extensions": [
      "nbextensions"
    ]
  },
  "version": 1
}
⚠️ **GitHub.com Fallback** ⚠️