From f9414f6b6775c49ce3c3651e9fb4ffb78fb14009 Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Mon, 8 Jul 2024 09:10:06 -0400 Subject: [PATCH 1/2] Removes jinja2 dependency Signed-off-by: Thomas J. Fan --- dev-requirements.in | 1 - dev-requirements.txt | 6 ------ flytekit/deck/deck.py | 31 ++++++++++++++++--------------- flytekit/deck/html/template.html | 10 ++-------- pyproject.toml | 1 - 5 files changed, 18 insertions(+), 31 deletions(-) diff --git a/dev-requirements.in b/dev-requirements.in index ca37177df7..2c91767a01 100644 --- a/dev-requirements.in +++ b/dev-requirements.in @@ -20,7 +20,6 @@ IPython keyrings.alt setuptools_scm pytest-icdiff -jinja2 # Tensorflow is not available for python 3.12 yet: https://github.com/tensorflow/tensorflow/issues/62003 tensorflow; python_version<'3.12' diff --git a/dev-requirements.txt b/dev-requirements.txt index d3279adcac..4eb8fa79f1 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -196,10 +196,6 @@ jaraco-functools==4.0.1 # via keyring jedi==0.19.1 # via ipython -jinja2==3.1.4 - # via - # -r dev-requirements.in - # flytekit jmespath==1.0.1 # via botocore joblib==1.4.2 @@ -221,8 +217,6 @@ markdown-it-py==3.0.0 # via # flytekit # rich -markupsafe==2.1.5 - # via jinja2 marshmallow==3.21.2 # via # dataclasses-json diff --git a/flytekit/deck/deck.py b/flytekit/deck/deck.py index fbb398ef49..c28dc960ed 100644 --- a/flytekit/deck/deck.py +++ b/flytekit/deck/deck.py @@ -1,6 +1,8 @@ import enum import os import typing +from html import escape +from string import Template from typing import Optional from flytekit.core.context_manager import ExecutionParameters, ExecutionState, FlyteContext, FlyteContextManager @@ -153,8 +155,16 @@ def _get_deck( If ignore_jupyter is set to True, then it will return a str even in a jupyter environment. """ deck_map = {deck.name: deck.html for deck in new_user_params.decks} + nav_htmls = [] + body_htmls = [] - raw_html = get_deck_template().render(metadata=deck_map) + for key, value in deck_map.items(): + nav_htmls.append(f'
  • {escape(key)}
  • ') + # Can not escape here because this is HTML. Escaping it will present the HTML as text. + # The renderer must ensure that the HTML is safe. + body_htmls.append(f'
    {value}
    ') + + raw_html = get_deck_template().substitute(NAV_HTML="".join(nav_htmls), BODY_HTML="".join(body_htmls)) if not ignore_jupyter and ipython_check(): try: from IPython.core.display import HTML @@ -184,18 +194,9 @@ def _output_deck(task_name: str, new_user_params: ExecutionParameters): logger.error(f"Failed to write flyte deck html with error {e}.") -def get_deck_template() -> "Template": - from jinja2 import Environment, FileSystemLoader, select_autoescape - +def get_deck_template() -> Template: root = os.path.dirname(os.path.abspath(__file__)) - templates_dir = os.path.join(root, "html") - env = Environment( - loader=FileSystemLoader(templates_dir), - # 🔥 include autoescaping for security purposes - # sources: - # - https://jinja.palletsprojects.com/en/3.0.x/api/#autoescaping - # - https://stackoverflow.com/a/38642558/8474894 (see in comments) - # - https://stackoverflow.com/a/68826578/8474894 - autoescape=select_autoescape(enabled_extensions=("html",)), - ) - return env.get_template("template.html") + templates_dir = os.path.join(root, "html", "template.html") + with open(templates_dir, "r") as f: + template_content = f.read() + return Template(template_content) diff --git a/flytekit/deck/html/template.html b/flytekit/deck/html/template.html index a58e8a3252..4a560b7930 100644 --- a/flytekit/deck/html/template.html +++ b/flytekit/deck/html/template.html @@ -69,20 +69,14 @@ -
    - {% for key, value in metadata.items() %} -
    {{ value | safe }}
    - {% endfor %} + $BODY_HTML
    -