Newer
Older
"""
Methods for rendering of the
various Jinja templates from the given data.
"""
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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'))
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, nren=False):
if nren:
file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'nren_access',
'nren-dashboard.json.j2'))
else:
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