Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://gitlab.com/jezcope/template-ipython-magic
IPython magics to render cells as templates in a variety of different templating languages.
https://gitlab.com/jezcope/template-ipython-magic
ipython jupyter magic python
Last synced: 3 months ago
JSON representation
IPython magics to render cells as templates in a variety of different templating languages.
- Host: gitlab.com
- URL: https://gitlab.com/jezcope/template-ipython-magic
- Owner: jezcope
- Created: 2020-01-08T15:30:19.597Z (about 5 years ago)
- Default Branch: master
- Last Synced: 2024-10-11T10:09:09.705Z (3 months ago)
- Topics: ipython, jupyter, magic, python
- Stars: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.ipynb
Awesome Lists containing this project
README
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Template IPython magics 🎩\n",
"\n",
"This package provides simple IPython magics to render cells as templates in a variety of different templating languages. It currently supports [Mako][] and [Jinja2][].\n",
"\n",
"[Mako]: https://www.makotemplates.org/\n",
"[Jinja2]: https://jinja.palletsprojects.com/\n",
"\n",
"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]):\n",
"\n",
"```shell\n",
"pipenv install template-ipython-magic jinja2 mako\n",
"```\n",
"\n",
"[pipenv]: https://pipenv.readthedocs.io/en/latest/\n",
"\n",
"In your notebook, load the `template_magic` module:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%load_ext template_magic"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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!\n",
"\n",
"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:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Hello from **Jinja** on Python 3.8! 🐍"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import sys\n",
"\n",
"%jinja Hello from **Jinja** on Python {{sys.version_info.major}}.{{sys.version_info.minor}}! 🐍"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you prefer, `%mako` is also available:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Hello from *Mako* at 08:39 PM... ⏰"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import datetime\n",
"now = datetime.datetime.now()\n",
"\n",
"%mako Hello from *Mako* at ${now.strftime('%I:%M %p')}... ⏰"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Cell magics are also available for each language, which lets you render the entire cell as a template for convenient report generation:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"\n",
"- spam,\n",
"- spam,\n",
"- spam,\n",
"- spam,\n",
"- spam,\n",
"- spam,\n",
"- spam,\n",
"- eggs,\n",
"- and spam"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%jinja\n",
"\n",
"{%- for x in ['spam'] * 7 + ['eggs', 'spam'] %}\n",
"- {% if loop.last %}and {% endif %}{{x}}{%if not loop.last %},{% endif %}\n",
"{%- endfor %}"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}