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

Fixed saving to html and json in Python 3 #1016

Merged
merged 4 commits into from
Dec 18, 2016
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
13 changes: 7 additions & 6 deletions holoviews/plotting/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
'webm': 'video/webm',
'mp4': 'video/mp4',
'pdf': 'application/pdf',
'html': None,
'json': None
'html': 'text/html',
'json': 'text/json'
}

static_template = """
Expand Down Expand Up @@ -361,12 +361,13 @@ def export_widgets(self_or_cls, obj, filename, fmt=None, template=None,
widget = obj

html = self_or_cls.static_html(widget, fmt, template)
encoded = self_or_cls.encode((html, {'mime_type': 'text/html'}))
if isinstance(filename, BytesIO):
filename.write(html)
filename.write(encoded)
filename.seek(0)
else:
with open(filename, 'w') as f:
f.write(html)
f.write(encoded)


@classmethod
Expand Down Expand Up @@ -477,11 +478,11 @@ def save(self_or_cls, obj, basename, fmt='auto', key={}, info={}, options=None,
rendered = self_or_cls(plot, fmt)
if rendered is None: return
(data, info) = rendered
encoded = self_or_cls.encode(rendered)
if isinstance(basename, BytesIO):
basename.write(data)
basename.write(encoded)
basename.seek(0)
else:
encoded = self_or_cls.encode(rendered)
filename ='%s.%s' % (basename, info['file-ext'])
with open(filename, 'wb') as f:
f.write(encoded)
Expand Down
53 changes: 48 additions & 5 deletions tests/testrenderclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
"""
Test cases for rendering exporters
"""
from __future__ import unicode_literals

from io import BytesIO
from hashlib import sha256
from unittest import SkipTest
import numpy as np

from holoviews.plotting.mpl.renderer import MPLRenderer
from holoviews import HoloMap, Image, ItemTable
from holoviews import HoloMap, Image, ItemTable, Store
from holoviews.core.util import unicode
from holoviews.element.comparison import ComparisonTestCase
from holoviews.plotting import Renderer

from nose.plugins.attrib import attr

Expand All @@ -18,16 +21,36 @@
try:
# Standardize backend due to random inconsistencies
from matplotlib import pyplot
from holoviews.plotting.mpl import MPLRenderer
pyplot.switch_backend('agg')
except:
pyplot = None
pass

try:
from holoviews.plotting.bokeh import BokehRenderer
except:
pass

def digest_data(data):
hashfn = sha256()
hashfn.update(data)
return hashfn.hexdigest()


class TestRenderer(ComparisonTestCase):
"""
Test the basic serializer and deserializer (i.e. using pickle),
including metadata access.
"""

def test_renderer_encode_unicode_types(self):
mime_types = ['image/svg+xml', 'text/html', 'text/json']
for mime in mime_types:
info = {'mime_type': mime}
encoded = Renderer.encode(('Testing «ταБЬℓσ»: 1<2 & 4+1>3', info))
self.assertTrue(isinstance(encoded, bytes))


@attr(optional=1)
class MPLRendererTest(ComparisonTestCase):
"""
Expand All @@ -36,7 +59,7 @@ class MPLRendererTest(ComparisonTestCase):
"""

def setUp(self):
if pyplot is None:
if 'matplotlib' not in Store.renderers:
raise SkipTest("Matplotlib required to test widgets")

self.basename = 'no-file'
Expand Down Expand Up @@ -102,11 +125,31 @@ def test_static_html_widgets(self):

def test_static_html_gif(self):
data = self.renderer.static_html(self.map1, fmt='gif')
self.assertEqual(digest_data(data),
self.assertEqual(digest_data(normalize(data)),
'9d43822e0f368f3c673b19aaf66d22252849947b7dc4a157306c610c42d319b5')

def test_export_widgets(self):
bytesio = BytesIO()
self.renderer.export_widgets(self.map1, bytesio, fmt='widgets')
data = normalize(bytesio.read())
self.assertEqual(digest_data(data),
'91bbc7b4efebd07b1ee595b902d9899b27f2c7e353dfc87c57c2dfd5d0404301')


class BokehRendererTest(ComparisonTestCase):

def setUp(self):
if 'bokeh' not in Store.renderers:
raise SkipTest("Bokeh required to test widgets")
self.image1 = Image(np.array([[0,1],[2,3]]), label='Image1')
self.image2 = Image(np.array([[1,0],[4,-2]]), label='Image2')
self.map1 = HoloMap({1:self.image1, 2:self.image2}, label='TestMap')
self.renderer = BokehRenderer.instance()

def test_save_html(self):
bytesio = BytesIO()
self.renderer.save(self.image1, bytesio)

def test_export_widgets(self):
bytesio = BytesIO()
self.renderer.export_widgets(self.map1, bytesio, fmt='widgets')
8 changes: 7 additions & 1 deletion tests/testwidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
raise SkipTest("Matplotlib required to test widgets")

from holoviews import Image, HoloMap
from holoviews.core.util import unicode
from holoviews.plotting.mpl import RasterPlot

def digest_data(data):
Expand All @@ -32,10 +33,15 @@ def digest_data(data):
filters += [re.compile('new [A-Za-z]*SelectionWidget\([a-z0-9_, "]+')]

def normalize(data):
if isinstance(data, bytes):
data = data.decode('utf8')
for f in filters:
data = re.sub(f, '[CLEARED]', data)
# Hack around inconsistencies in jinja between Python 2 and 3
return data.replace('0.0', '0').replace('1.0', '1')
data = data.replace('0.0', '0').replace('1.0', '1')
if isinstance(data, unicode):
data = data.encode('utf8')
return data

@attr(optional=1)
class TestWidgets(IPTestCase):
Expand Down