template Module

template Module

Templates allow multiple Panel objects to be embedded into custom HTML documents.

class panel.template.Template(template=None, items=None, nb_template=None, **params)[source]

Bases: param.parameterized.Parameterized, panel.viewable.ServableMixin

A Template is a high-level component to render multiple Panel objects into a single HTML document defined through a Jinja2 template. The Template object is given a Jinja2 template and then allows populating this template by adding Panel objects, which are given unique names. These unique names may then be referenced in the template to insert the rendered Panel object at a specific location. For instance, given a Jinja2 template that defines roots A and B like this:

<div> {{ embed(roots.A) }} </div> <div> {{ embed(roots.B) }} </div>

We can then populate the template by adding panel ‘A’ and ‘B’ to the Template object:

template.add_panel(‘A’, pn.panel(‘A’)) template.add_panel(‘B’, pn.panel(‘B’))

Once a template has been fully populated it can be rendered using the same API as other Panel objects. Note that all roots that have been declared using the {{ embed(roots.A) }} syntax in the Jinja2 template must be defined when rendered.

Since embedding complex CSS frameworks inside a notebook can have undesirable side-effects and a notebook does not afford the same amount of screen space a Template may given separate template and nb_template objects. This allows for different layouts when served as a standalone server and when used in the notebook.

add_panel(name, panel, tags=[])[source]

Add panels to the Template, which may then be referenced by the given name using the jinja2 embed macro.

Parameters
  • name (str) – The name to refer to the panel by in the template

  • panel (panel.Viewable) – A Panel component to embed in the template.

add_variable(name, value)[source]

Add parameters to the template, which may then be referenced by the given name in the Jinja2 template.

Parameters
  • name (str) – The name to refer to the panel by in the template

  • value (object) – Any valid Jinja2 variable type.

save(filename, title=None, resources=None, embed=False, max_states=1000, max_opts=3, embed_json=False, json_prefix='', save_path='./', load_path=None)[source]

Saves Panel objects to file.

Parameters
  • filename (string or file-like object) – Filename to save the plot to

  • title (string) – Optional title for the plot

  • resources (bokeh resources) – One of the valid bokeh.resources (e.g. CDN or INLINE)

  • embed (bool) – Whether the state space should be embedded in the saved file.

  • max_states (int) – The maximum number of states to embed

  • max_opts (int) – The maximum number of states for a single widget

  • embed_json (boolean (default=True)) – Whether to export the data to json files

  • json_prefix (str (default='')) – Prefix for the auto-generated json directory

  • save_path (str (default='./')) – The path to save json files to

  • load_path (str (default=None)) – The path or URL the json files will be loaded from.

select(selector=None)[source]

Iterates over the Template and any potential children in the applying the Selector.

Parameters

selector (type or callable or None) – The selector allows selecting a subset of Viewables by declaring a type or callable function to filter by.

Returns

viewables

Return type

list(Viewable)

servable(title=None)

Serves the object if in a panel serve context and returns the Panel object to allow it to display itself in a notebook context.

Parameters

title (str) – A string title to give the Document (if served as an app)

Returns

Return type

The Panel object itself

server_doc(doc=None, title=None)[source]

Returns a servable bokeh Document with the panel attached

Parameters
  • doc (bokeh.Document (optional)) – The Bokeh Document to attach the panel to as a root, defaults to bokeh.io.curdoc()

  • title (str) – A string title to give the Document

Returns

doc – The Bokeh document the panel was attached to

Return type

bokeh.Document

show(title=None, port=0, websocket_origin=None, threaded=False, verbose=True, open=True, **kwargs)

Starts a Bokeh server and displays the Viewable in a new tab.

Parameters
  • port (int (optional, default=0)) – Allows specifying a specific port

  • websocket_origin (str or list(str) (optional)) – A list of hosts that can connect to the websocket. This is typically required when embedding a server app in an external web site. If None, “localhost” is used.

  • threaded (boolean (optional, default=False)) – Whether to launch the Server on a separate thread, allowing interactive use.

  • title (str) – A string title to give the Document (if served as an app)

  • verbose (boolean (optional, default=True)) – Whether to print the address and port

  • open (boolean (optional, default=True)) – Whether to open the server in a new browser tab

Returns

server – Returns the Bokeh server instance or the thread the server was launched on (if threaded=True)

Return type

bokeh.server.Server or threading.Thread