Skip to content

Commit

Permalink
Ensure external resources are configured correctly on save (#2452)
Browse files Browse the repository at this point in the history
* Ensure external resources are configured correctly on save

* Add test
  • Loading branch information
philippjfr committed Jun 29, 2021
1 parent e1713d0 commit db57c8e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
12 changes: 11 additions & 1 deletion panel/io/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pyviz_comms import Comm

from ..config import config
from . import resources as resource_module
from .embed import embed_state
from .model import add_to_doc
from .resources import BASE_TEMPLATE, DEFAULT_TITLE, Bundle, Resources
Expand Down Expand Up @@ -217,11 +218,14 @@ def save(panel, filename, title=None, resources=None, template=None,

if resources is None:
resources = CDN
mode = 'cdn'
elif isinstance(resources, str):
if resources.lower() == 'cdn':
resources = CDN
mode = 'cdn'
elif resources.lower() == 'inline':
resources = INLINE
mode = 'inline'
else:
raise ValueError("Resources %r not recognized, specify one "
"of 'CDN' or 'INLINE'." % resources)
Expand All @@ -245,7 +249,13 @@ def save(panel, filename, title=None, resources=None, template=None,

resources = Resources.from_bokeh(resources)

html = file_html(doc, resources, title, **kwargs)
# Set resource mode
old_mode = resource_module.RESOURCE_MODE
resource_module.RESOURCE_MODE = mode
try:
html = file_html(doc, resources, title, **kwargs)
finally:
resource_module.RESOURCE_MODE = old_mode
if hasattr(filename, 'write'):
if isinstance(filename, io.BytesIO):
html = html.encode('utf-8')
Expand Down
32 changes: 32 additions & 0 deletions panel/tests/io/test_save.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from io import StringIO

from panel.pane import Vega
from panel.models.vega import VegaPlot


vega_example = {
'config': {
'mark': {'tooltip': None},
'view': {'height': 300, 'width': 400}
},
'data': {'values': [{'x': 'A', 'y': 5},
{'x': 'B', 'y': 3},
{'x': 'C', 'y': 6},
{'x': 'D', 'y': 7},
{'x': 'E', 'y': 2}]},
'mark': 'bar',
'encoding': {'x': {'type': 'ordinal', 'field': 'x'},
'y': {'type': 'quantitative', 'field': 'y'}},
'$schema': 'https://vega.github.io/schema/vega-lite/v3.2.1.json'
}


def test_save_external():
sio = StringIO()
pane = Vega(vega_example)

pane.save(sio)
sio.seek(0)
html = sio.read()
for js in VegaPlot.__javascript_raw__:
assert js in html

0 comments on commit db57c8e

Please sign in to comment.