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

Add ability to define Template variables #815

Merged
merged 2 commits into from
Nov 25, 2019
Merged
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
12 changes: 9 additions & 3 deletions examples/user_guide/Templates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"\n",
"<!-- goes in body -->\n",
"{% block contents %}\n",
"<h1>Custom Template App</h1>\n",
"{{ app_title }}\n",
"<p>This is a Panel app with a custom template allowing us to compose multiple Panel objects into a single HTML document.</p>\n",
"<br>\n",
"<div class=\"container\">\n",
Expand All @@ -127,7 +127,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"If you look closely we defined two different roots in the template using the `embed` macro. In order to be able to render the template we now have to first construct the `pn.Template` object with the template HTML and then populate the template with the two required roots, in this case `'A'` and `'B'`. If either of the roots is not defined the app is invalid and will fail to launch. The app will also fail to launch if any panels are added that are not referenced in the template."
"If you look closely we defined two different roots in the template using the `embed` macro. In order to be able to render the template we now have to first construct the `pn.Template` object with the template HTML and then populate the template with the two required roots, in this case `'A'` and `'B'` by using the `add_panel` method. If either of the roots is not defined the app is invalid and will fail to launch. The app will also fail to launch if any panels are added that are not referenced in the template.\n",
"\n",
"Additionally we have also declared a new `app_title` variable in the template, which we can populate by using the `add_variable` method:"
]
},
{
Expand All @@ -138,6 +140,8 @@
"source": [
"tmpl = pn.Template(template)\n",
"\n",
"tmpl.add_variable('app_title', '<h1>Custom Template App</h1>')\n",
"\n",
"tmpl.add_panel('A', hv.Curve([1, 2, 3]))\n",
"tmpl.add_panel('B', hv.Curve([1, 2, 3]))\n",
"\n",
Expand All @@ -161,7 +165,7 @@
"{% extends base %}\n",
"\n",
"{% block contents %}\n",
"<h1>Custom Template App</h1>\n",
"{{ app_title }}\n",
"<p>This is a Panel app with a custom template allowing us to compose multiple Panel objects into a single HTML document.</p>\n",
"<br>\n",
"<div style=\"display:table; width: 100%\">\n",
Expand All @@ -177,6 +181,8 @@
"\n",
"tmpl = pn.Template(template, nb_template=nb_template)\n",
"\n",
"tmpl.add_variable('app_title', '<h1>Custom Template App</h1>')\n",
"\n",
"tmpl.add_panel('A', hv.Curve([1, 2, 3]))\n",
"tmpl.add_panel('B', hv.Curve([1, 2, 3]))\n",
"\n",
Expand Down
21 changes: 21 additions & 0 deletions panel/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __init__(self, template=None, items=None, nb_template=None, **params):
nb_template = _Template(nb_template)
self.nb_template = nb_template or template
self._render_items = {}
self._render_variables = {}
self._server = None
self._layout = self._build_layout()
items = {} if items is None else items
Expand Down Expand Up @@ -153,6 +154,7 @@ def _init_doc(self, doc=None, comm=None, title=None, notebook=False):
doc.template = self.nb_template
else:
doc.template = self.template
doc._template_variables.update(self._render_variables)
return doc

def _repr_mimebundle_(self, include=None, exclude=None):
Expand Down Expand Up @@ -201,6 +203,25 @@ def add_panel(self, name, panel, tags=[]):
self._render_items[name] = (_panel(panel), tags)
self._layout[0].object = repr(self)

def add_variable(self, name, value):
"""
Add parameters to the template, which may then be referenced
by the given name in the Jinja2 template.

Arguments
---------
name : str
The name to refer to the panel by in the template
value : object
Any valid Jinja2 variable type.
"""
if name in self._render_variables:
raise ValueError('The name %s has already been used for '
'another variable. Ensure each variable '
'has a unique name by which it can be '
'referenced in the template.' % name)
self._render_variables[name] = value

def server_doc(self, doc=None, title=None):
"""
Returns a servable bokeh Document with the panel attached
Expand Down