From 7047c6a90535564c9d05121fc8a095aef1de3c21 Mon Sep 17 00:00:00 2001 From: kernc Date: Wed, 21 Apr 2021 17:29:22 +0200 Subject: [PATCH] Replace deprecated jinja2.Markup with jinja2.tojson (#11174) * Replace deprecated jinja2.Markup -> markupsafe.Markup * Expose |tojson as |json. Bump Jinja2 >=2.9 * Replace |json filter with jinja's |tojson throughout * Fix test_autoload_template_has_changed --- _setup_support.py | 2 +- bokeh/core/_templates/autoload_js.js | 14 +++++++------- bokeh/core/_templates/autoload_nb_js.js | 6 +++--- bokeh/core/templates.py | 4 +--- tests/unit/bokeh/core/test_templates.py | 2 +- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/bokeh/core/_templates/autoload_js.js b/bokeh/core/_templates/autoload_js.js index 792a74c7cc2..d15ea9778e6 100644 --- a/bokeh/core/_templates/autoload_js.js +++ b/bokeh/core/_templates/autoload_js.js @@ -20,7 +20,7 @@ calls it with the rendered model. return new Date(); } - var force = {{ force|default(False)|json }}; + var force = {{ force|default(False)|tojson }}; if (typeof root._bokeh_onload_callbacks === "undefined" || force === true) { root._bokeh_onload_callbacks = []; @@ -32,7 +32,7 @@ calls it with the rendered model. {% block autoload_init %} {%- if elementid -%} - var element = document.getElementById({{ elementid|json }}); + var element = document.getElementById({{ elementid|tojson }}); if (element == null) { console.warn("Bokeh: autoload.js configured with elementid '{{ elementid }}' but no matching script tag was found.") } @@ -91,7 +91,7 @@ calls it with the rendered model. document.body.appendChild(element); } - const hashes = {{ bundle.hashes|json }}; + const hashes = {{ bundle.hashes|tojson }}; for (let i = 0; i < js_urls.length; i++) { const url = js_urls[i]; @@ -110,17 +110,17 @@ calls it with the rendered model. } {% if bundle %} - var js_urls = {{ bundle.js_urls|json }}; - var css_urls = {{ bundle.css_urls|json }}; + var js_urls = {{ bundle.js_urls|tojson }}; + var css_urls = {{ bundle.css_urls|tojson }}; {% else %} - var js_urls = {{ js_urls|json }}; - var css_urls = {{ css_urls|json }}; + var js_urls = {{ js_urls|tojson }}; + var css_urls = {{ css_urls|tojson }}; {% endif %} var inline_js = [ {%- for css in (bundle.css_raw if bundle else css_raw) %} function(Bokeh) { - inject_raw_css({{ css|json }}); + inject_raw_css({{ css|tojson }}); }, {%- endfor %} {%- for js in (bundle.js_raw if bundle else js_raw) %} diff --git a/bokeh/core/_templates/autoload_nb_js.js b/bokeh/core/_templates/autoload_nb_js.js index 2c75850b2b4..ca96a59cd2d 100644 --- a/bokeh/core/_templates/autoload_nb_js.js +++ b/bokeh/core/_templates/autoload_nb_js.js @@ -132,7 +132,7 @@ {% block autoload_init %} if (typeof (root._bokeh_timeout) === "undefined" || force === true) { - root._bokeh_timeout = Date.now() + {{ timeout|default(0)|json }}; + root._bokeh_timeout = Date.now() + {{ timeout|default(0)|tojson }}; root._bokeh_failed_load = false; } @@ -153,7 +153,7 @@ ""}}; function display_loaded() { - var el = document.getElementById({{ elementid|json }}); + var el = document.getElementById({{ elementid|tojson }}); if (el != null) { el.textContent = "BokehJS is loading..."; } @@ -181,7 +181,7 @@ console.log("Bokeh: BokehJS failed to load within specified timeout."); root._bokeh_failed_load = true; } else if (force !== true) { - var cell = $(document.getElementById({{ elementid|json }})).parents('.cell').data().cell; + var cell = $(document.getElementById({{ elementid|tojson }})).parents('.cell').data().cell; cell.output_area.append_execute_result(NB_LOAD_WARNING) } {% endblock %} diff --git a/bokeh/core/templates.py b/bokeh/core/templates.py index 10344ecaf7d..c7cd25ea7f2 100644 --- a/bokeh/core/templates.py +++ b/bokeh/core/templates.py @@ -36,12 +36,11 @@ #----------------------------------------------------------------------------- # Standard library imports -import json import sys from os.path import dirname, join # External imports -from jinja2 import Environment, FileSystemLoader, Markup +from jinja2 import Environment, FileSystemLoader #----------------------------------------------------------------------------- # Globals and constants @@ -86,7 +85,6 @@ def get_env(): #----------------------------------------------------------------------------- _env = get_env() -_env.filters['json'] = lambda obj: Markup(json.dumps(obj)) #----------------------------------------------------------------------------- # General API