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) ip = get_ipython() ip.register_magics(JinjaMagics) names = ['alice','bob'] %%jinja html {{ title }} {% for name in names %} Hello {{ name }}
{% endfor %} %%jinja latex \begin{eqnarray} \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ \nabla \cdot \vec{\mathbf{B}} & = 0 \end{eqnarray} vars = {'rho':5,'alpha':6,'pi':3.14,'phi':1.618,'hbar':'6.582121 \cdot 10^{-16} eV\cdot s'} %%jinja latex \begin{eqnarray} {% for k,v in vars.iteritems() %} \{{ k }} & = {{ v }} \\ {% endfor %} \end{eqnarray}