Skip to main content

IPython magics to render cells as templates in a variety of different templating languages.

Project description

Template IPython magics 🎩

This package provides simple IPython magics to render cells as templates in a variety of different templating languages. It currently supports Mako and Jinja2.

To use it, first install the package from PyPI, along with at least one of the supported templating languages. E.g. using pipenv (everyone should use pipenv):

pipenv install template-ipython-magic jinja2 mako

In your notebook, load the template_magic module:

%load_ext template_magic

Note that the available templating languages are detected at the point of loading the extension, and each magic only enabled if the appropriate package is found. If neither Jinja2 or Mako are installed, there will be no magics!

Now you can use %jinja as a line magic within any code block, with access to all variables in scope. The result is formatted as Markdown:

import sys

%jinja Hello from **Jinja** on Python {{sys.version_info.major}}.{{sys.version_info.minor}}! 🐍

Hello from Jinja on Python 3.8! 🐍

If you prefer, %mako is also available:

import datetime
now = datetime.datetime.now()

%mako Hello from *Mako* at ${now.strftime('%I:%M %p')}... 

Hello from Mako at 08:39 PM... ⏰

Cell magics are also available for each language, which lets you render the entire cell as a template for convenient report generation:

%%jinja

{%- for x in ['spam'] * 7 + ['eggs', 'spam'] %}
- {% if loop.last %}and {% endif %}{{x}}{%if not loop.last %},{% endif %}
{%- endfor %}
  • spam,
  • spam,
  • spam,
  • spam,
  • spam,
  • spam,
  • spam,
  • eggs,
  • and spam

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for template-ipython-magic, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size template_ipython_magic-0.1.2-py3-none-any.whl (3.1 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size template-ipython-magic-0.1.2.tar.gz (2.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page