Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure external resources are configured correctly on save #2452

Merged
merged 2 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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