Inline variable insertion in markdown

Heya, I’ve created a (very) draft PR in nbclient: PoP for Inline variable insertion in markdown by chrisjsewell · Pull Request #160 · jupyter/nbclient · GitHub

Here you simply provide it with a callable, to extract substitution expressions from the Markdown content, then execute the extracted expressions as user_expressions, and finally save them as attachments.

So very much like the @agoose77 prototype, except you are essentially extracting the variable in a pre-process stage (rather than having to go all the way through to rendering the Markdown), then it would be easy to “later” inject these attachments back into the Markdown parse+render in myst-nb or nbconvert etc (this should also be able to still work with jupyter-cache)

It also means nbclient doesn’t directly need to know anything about the Markdown syntax

A few additional thoughts:

  • I would definitely give these cells an execution number; the “imarkdown” maybe should have a slightly different JSON schema to “markdown”, that is a hybrid between “markdown” and “code”
  • For integration with nbconvert, it would be cool if it adopted GitHub - executablebooks/markdown-it-py: Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed. Now in Python! as the default parser :wink: then it would be easy to synergise between the Javascript and Python implementations
  • I guess the Jinja {{ }} syntax is a slightly double-edged sword; it is fairly intuitive for anyone who has used jinja/mustache, but it may clash with other uses of these engines, and people may expect it to work exactly the same. (It would certainly clash with https://myst-parser.readthedocs.io/en/latest/syntax/optional.html#substitutions-with-jinja2)
  • For injecting things like floats, it would be cool if you could do something like f-strings: {{ variable:.3f }}, although with python kernels you can already do {{ f'{a:.3f}' }}