Created
November 13, 2015 13:53
-
-
Save schuster-rainer/cccdab4877a90fba35f5 to your computer and use it in GitHub Desktop.
IPython jinja cell magic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython import display | |
from IPython.core.magic import register_cell_magic, Magics, magics_class, cell_magic | |
import jinja2 | |
@magics_class | |
class JinjaMagics(Magics): | |
'''Magics class containing the jinja2 magic and state''' | |
def __init__(self, shell): | |
super(JinjaMagics, self).__init__(shell) | |
# create a jinja2 environment to use for rendering | |
# this can be modified for desired effects (ie: using different variable syntax) | |
self.env = jinja2.Environment(loader=jinja2.FileSystemLoader('.')) | |
# possible output types | |
self.display_functions = dict(html=display.HTML, | |
latex=display.Latex, | |
json=display.JSON, | |
pretty=display.Pretty, | |
display=display.display) | |
@cell_magic | |
def jinja(self, line, cell): | |
''' | |
jinja2 cell magic function. Contents of cell are rendered by jinja2, and | |
the line can be used to specify output type. | |
ie: "%%jinja html" will return the rendered cell wrapped in an HTML object. | |
''' | |
f = self.display_functions.get(line.lower().strip(), display.display) | |
tmp = self.env.from_string(cell) | |
rend = tmp.render(dict((k,v) for (k,v) in self.shell.user_ns.items() | |
if not k.startswith('_') and k not in self.shell.user_ns_hidden)) | |
return f(rend) | |
def load_ipython_extension(ip): | |
ip.register_magics(JinjaMagics) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment