Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP adding renderers #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added omniduct/renderers/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions omniduct/renderers/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from abc import abstractmethod
from omniduct.utils.magics import MagicsProvider, process_line_arguments, process_line_cell_arguments


class BaseRenderer(MagicsProvider):

@abstractmethod
def render_template(self, statement):
pass

def _register_magics(self, base_name):
"""
The following magic functions will be registered (assuming that
the base name is chosen to be 'hive'):
- Cell Magics:
- `%%html`: For querying the database.
- Line Magics:
- `%hive`: For querying the database using a named template.

Documentation for these magics is provided online.
"""
from IPython.core.magic import register_cell_magic

def template_render_magic(template, context=None, **kwargs):

ip = get_ipython()

if context is None:
context = ip.user_ns

return self.render_template(template, context)


@register_cell_magic(base_name)
@process_line_cell_arguments
def render_magic(*args, **kwargs):
return template_render_magic(*args, **kwargs)
10 changes: 10 additions & 0 deletions omniduct/renderers/jinja.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .base import BaseRenderer
from jinja2 import Template


class JinjaRenderer(BaseRenderer):

name = 'jinja'

def render_template(self, template, context=None, **kwargs):
return Template(template).render(**context)
20 changes: 20 additions & 0 deletions omniduct/renderers/md.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from .base import BaseRenderer
from jinja2 import Template
import markdown


class MdRepr(object):

def __init__(self, rendered):
self.html = markdown.markdown(rendered)

def _repr_html_(self):
return self.html


class MdRenderer(BaseRenderer):

name = 'md'

def render_template(self, template, context=None, **kwargs):
return MdRepr(Template(template).render(**context))