render.py 1.94 KiB
"""
Methods for rendering of the
various Jinja templates from the given data.
"""
import os
import json
import jinja2
def create_dropdown_panel(title, **kwargs):
TEMPLATE_FILENAME = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'dropdown.json.j2'))
with open(TEMPLATE_FILENAME) as f:
template = jinja2.Template(f.read())
return template.render({**kwargs, 'title': title})
# wrapper around bits/s and err/s panel labels
def create_yaxes(type):
file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'yaxes.json.j2'))
with open(file) as f:
template = jinja2.Template(f.read())
return template.render({'type': type})
def create_panel_target(data):
file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'panel_target.json.j2'))
with open(file) as f:
template = jinja2.Template(f.read())
return template.render(data)
def create_panel(data):
file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'panel.json.j2'))
with open(file) as f:
template = jinja2.Template(f.read())
yaxes = create_yaxes(data.get('y_axis_type', 'bits'))
targets = data.get('targets', [])
for target in data.get('panel_targets', []):
targets.append(create_panel_target(target))
return template.render({**data, 'yaxes': yaxes, 'targets': targets})
def render_dashboard(dashboard):
file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'dashboard.json.j2'))
with open(file) as f:
template = jinja2.Template(f.read())
rendered = template.render(dashboard)
rendered = json.loads(rendered)
rendered['uid'] = None
rendered['id'] = None
return rendered