how can I correctly plug in my own Jinja Environment? #836
Unanswered
gagan-bajwa
asked this question in
Q&A
Replies: 1 comment
-
There isn't at the moment.
I can demonstrate this with the following snippets:
from meridian.analysis import formatter
import jinja2
import os
# stand-in for a "custom" loader; this would just load the same built-in templates, though.
_template_loader = jinja2.FileSystemLoader(os.path.abspath(os.path.dirname(formatter.__file__)) + '/templates')
def _custom_create_template_env() -> jinja2.Environment:
return jinja2.Environment(
loader=_template_loader,
autoescape=False,
trim_blocks=True,
lstrip_blocks=True,
)
formatter.create_template_env = _custom_create_template_env
from meridian.analysis import summarizer
sum = summarizer.Summarizer(mmm)
# Setting up the (custom) template env
_template_env = formatter.create_template_env()
_template_env.globals["start_date"] = "Jan 1, 2021"
_template_env.globals["end_date"] = "Jan 22, 2024"
# Using the (now custom) template env, load invidivial card each as its own <card> html DOM
cards_htmls = sum._create_cards_htmls(
_template_env,
selected_times=None, # == (start_date, end_date) above
)
print(cards_htmls[0]) # no escapes
# Load the top-level page container (summary html template)
html_template = _template_env.get_template('summary.html.jinja')
report = html_template.render(title="My Title", cards=cards_htmls)
print(report) # no escapes, either |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi team,
I’m re-skinning Meridian reports to match our company’s design system.
I copied the original *.html.jinja files into src/branding/templates/ and load them via a custom Jinja environment:
In my API entry-point / training script I patch Meridian like this:
The new templates and CSS load fine (header, chips, colours, logo).
Problem: every pre-rendered fragment (card, chart, stats, table) still arrives HTML-escaped, so the browser prints
instead of rendering the grid.
What I’ve tried
Adding
|safewhenever I print card, item, etc. — helps only in the outer template; the inner markup is still encoded.Verified that my own templates are actually used (added dummy text, saw it appear).
If I monkey-patch
jinja2.escape = lambda x: x,the tags render (so it really is an escaping issue).Questions
Is there an official extension point to supply a custom Environment (ideally with
autoescape=False) instead of patching_TEMPLATE_ENV?Where in the pipeline do the fragments (CardSpec, ChartSpec, …) get rendered? Is there a recommended flag / setting to mark them “safe” so the outer template can embed them without HTML-encoding?
If disabling auto-escape globally is a no-go, what’s the proper way to inject already-rendered HTML snippets into
summary.html.jinja?Any guidance or code pointers would be greatly appreciated.happy to PR docs once I get this working.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions