pythonjinja2panel-pyviz

Python Panel Template change the Title from Bokeh Application to MyApp


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?


Solution

  • @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')