Python Panel Template change the Title from Bokeh Application to MyApp

1.1k Views Asked by At

I am running the example https://panel.pyviz.org/user_guide/Templates.html and would like to change the title from "Bokeh Application" to "My App".

import panel as pn
import holoviews as hv
from jinja2 import Environment, FileSystemLoader

pn.extension()
env = Environment(loader=FileSystemLoader('.'))
jinja_template = env.get_template('z_base.html')

tmpl = pn.Template(jinja_template)

tmpl.add_panel('A', hv.Curve([1, 2, 3]))
tmpl.add_panel('B', hv.Curve([1, 2, 3]))

tmpl.show();

with html z_base.html extending base:

{% extends base %}

<title>{% block title %}My App{% endblock %}</title> <!-- THIS DOES NOT WORK !!!! -->

{% block postamble %}
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">    
{% endblock postamble %}


{% block contents %}

<h1>Custom Template App 3</h1>
<p>This is a Panel app with a custom template allowing us to compose multiple Panel objects into a single HTML document.</p>
<br>
<div class="container">
  <div class="row">
    <div class="col-sm">
     {{ embed(roots.A) }}
    </div>
    <div class="col-sm">
      {{ embed(roots.B) }}
    </div>
  </div>
</div>

{% endblock %}

base template refer to the title <title>{% block title %}{{ title | e if title else "Panel App" }}{% endblock %}</title>

<!DOCTYPE html>
<html lang="en">
{% block head %}
<head>
    {% block inner_head %}
    <meta charset="utf-8">
    <title>{% block title %}{{ title | e if title else "Panel App" }}{% endblock %}</title>
    {% block preamble %}{% endblock %}
    {% block resources %}
        {% block css_resources %}
        {{ bokeh_css | indent(8) if bokeh_css }}
        {% endblock %}
        {% block js_resources %}
        {{ bokeh_js | indent(8) if bokeh_js }}
        {% endblock %}
    {% endblock %}
    {% block postamble %}{% endblock %}
    {% endblock %}
</head>

How can I pass the title variable to the base template?

2

There are 2 best solutions below

0
On BEST ANSWER

@EMayorga answer didn't work, but I think what is intended is to extend and implement that block. I got it to work from doing this in my template:

{% extends base %}

{% block title %}
{{ html_title }}
{% endblock %}

<!-- goes in body -->
{% block postamble %}
<link rel="stylesheet" href="https://stackpath.bootstrapcd ... 
...

And then in python: tmpl.add_variable('html_title', 'Demo 23')

1
On

I had the same need and couldn't find an answer anywhere on the documentation. Your question here helped guide me, so I wanted to report the simple solution I've found.

I ended up going through the panel.template module code and API doc to look for hints. It turns out you can pass your custom title to the servable or show methods. In your code, it would look like this:

tmpl.show(title="My App");

I've tested it with both servable and show methods.