Skip to content

Commit

Permalink
Ensure loading of MathJax bundle is optional (#2919)
Browse files Browse the repository at this point in the history
* Ensure loading of MathJax bundle is optional

* Fix flake

* Fix autoload_js
  • Loading branch information
philippjfr committed Nov 15, 2021
1 parent 92b981a commit cdfcd78
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion panel/io/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def load_notebook(inline=True, load_timeout=5000):
user_resources = settings.resources._user_value is not _Unset
resources = Resources.from_bokeh(resources)
try:
bundle = bundle_resources(resources)
bundle = bundle_resources(None, resources)
bundle = Bundle.from_bokeh(bundle)
configs, requirements, exports, skip_imports = require_components()
ipywidget = 'ipywidgets_bokeh' in sys.modules
Expand Down
19 changes: 14 additions & 5 deletions panel/io/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Patches bokeh resources to make it easy to add external JS and CSS
resources via the panel.config object.
"""
import copy
import glob
import json
import os
Expand All @@ -15,7 +16,7 @@

from bokeh.embed.bundle import (
Bundle as BkBundle, _bundle_extensions, extension_dirs,
bundle_models
bundle_models, _use_mathjax
)

from bokeh.resources import Resources as BkResources
Expand Down Expand Up @@ -98,7 +99,8 @@ def bundled_files(model, file_type='javascript'):
return files


def bundle_resources(resources):
def bundle_resources(roots, resources):
from ..config import panel_extension as ext
global RESOURCE_MODE
js_resources = css_resources = resources
RESOURCE_MODE = mode = js_resources.mode if resources is not None else "inline"
Expand All @@ -108,13 +110,20 @@ def bundle_resources(resources):
css_files = []
css_raw = []

js_files.extend(js_resources.js_files)
js_raw.extend(js_resources.js_raw)
use_mathjax = (_use_mathjax(roots) or 'mathjax' in ext._loaded_extensions) if roots else True

if js_resources:
js_resources = copy.deepcopy(js_resources)
if not use_mathjax and "bokeh-mathjax" in js_resources.js_components:
js_resources.js_components.remove("bokeh-mathjax")

js_files.extend(js_resources.js_files)
js_raw.extend(js_resources.js_raw)

css_files.extend(css_resources.css_files)
css_raw.extend(css_resources.css_raw)

extensions = _bundle_extensions(None, js_resources)
extensions = _bundle_extensions(roots, js_resources)
if mode == "inline":
js_raw.extend([ Resources._inline(bundle.artifact_path) for bundle in extensions ])
elif mode == "server":
Expand Down
8 changes: 5 additions & 3 deletions panel/io/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import bokeh

from bokeh.document.document import Document
from bokeh.embed.bundle import bundle_for_objs_and_resources
from bokeh.embed.elements import html_page_for_render_items
from bokeh.embed.util import OutputDocumentFor, standalone_docs_json_and_render_items
from bokeh.io.export import get_screenshot_as_png
Expand All @@ -19,7 +18,10 @@
from ..config import config
from .embed import embed_state
from .model import add_to_doc
from .resources import BASE_TEMPLATE, DEFAULT_TITLE, Bundle, Resources, set_resource_mode
from .resources import (
BASE_TEMPLATE, DEFAULT_TITLE, Bundle, Resources, bundle_resources,
set_resource_mode
)
from .state import state

#---------------------------------------------------------------------
Expand Down Expand Up @@ -134,7 +136,7 @@ def file_html(models, resources, title=None, template=BASE_TEMPLATE,
models_seq, suppress_callback_warning=True
)
title = _title_from_models(models_seq, title)
bundle = bundle_for_objs_and_resources(None, resources)
bundle = bundle_resources(models_seq, resources)
bundle = Bundle.from_bokeh(bundle)
return html_page_for_render_items(
bundle, docs_json, render_items, title=title, template=template,
Expand Down
11 changes: 7 additions & 4 deletions panel/io/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ def server_html_page_for_session(session, resources, title, template=BASE_TEMPLA
if template_variables is None:
template_variables = {}

bundle = bundle_resources(resources)
bundle = bundle_resources(session.document.roots, resources)
return html_page_for_render_items(bundle, {}, [render_item], title,
template=template, template_variables=template_variables)

def autoload_js_script(resources, token, element_id, app_path, absolute_url):
def autoload_js_script(doc, resources, token, element_id, app_path, absolute_url):
resources = Resources.from_bokeh(resources)
bundle = bundle_resources(resources)
bundle = bundle_resources(doc.roots, resources)

render_items = [RenderItem(token=token, elementid=element_id, use_for_title=False)]
bundle.add(Script(script_for_render_items({}, render_items, app_path=app_path, absolute_url=absolute_url)))
Expand Down Expand Up @@ -260,7 +260,10 @@ async def get(self, *args, **kwargs):
state.curdoc = session.document
try:
resources = Resources.from_bokeh(self.application.resources(server_url))
js = autoload_js_script(resources, session.token, element_id, app_path, absolute_url)
js = autoload_js_script(
session.document, resources, session.token, element_id,
app_path, absolute_url
)
finally:
state.curdoc = None

Expand Down

0 comments on commit cdfcd78

Please sign in to comment.