|
3 | 3 | import plotly.tools as tls
|
4 | 4 | import imghdr
|
5 | 5 | import threading
|
| 6 | +import six |
6 | 7 | import unittest
|
7 |
| - |
8 |
| - |
9 |
| -class TestPlotlyDisplay(unittest.TestCase): |
10 |
| - |
11 |
| - def setUp(self): |
12 |
| - plot_info = {"un": "plotlyimagetest", "fid": "2"} |
13 |
| - url = "https://plot.ly/~{un}/{fid}".format(**plot_info) |
14 |
| - self.display_obj = tls.embed(url) |
15 |
| - self.results = {} |
16 |
| - self.threads = [] |
17 |
| - self.format_to_func = { |
18 |
| - "jpeg": self.jpeg_worker, |
19 |
| - "png": self.png_worker, |
20 |
| - "svg": self.svg_worker, |
21 |
| - "pdf": self.pdf_worker} |
22 |
| - |
23 |
| - def test_plotly_display(self): |
24 |
| - for f_format, func in self.format_to_func.items(): |
25 |
| - self.threads += [threading.Thread(target=func)] |
26 |
| - self.threads[-1].setDaemon(True) |
27 |
| - self.threads[-1].start() |
28 |
| - for thread in self.threads: |
29 |
| - thread.join() |
30 |
| - for f_format in self.format_to_func: |
31 |
| - result = self.results.get(f_format, False) |
32 |
| - print("{f_format}: {result}".format(f_format=f_format, |
33 |
| - result=result)) |
34 |
| - assert self.results.get(f_format) |
35 |
| - |
36 |
| - def jpeg_worker(self): |
37 |
| - img = self.display_obj._repr_jpeg_() |
38 |
| - if imghdr.what('', img) == "jpeg": |
39 |
| - self.results["jpeg"] = True |
40 |
| - |
41 |
| - def png_worker(self): |
42 |
| - img = self.display_obj._repr_png_() |
43 |
| - if imghdr.what('', img) == "png": |
44 |
| - self.results["png"] = True |
45 |
| - |
46 |
| - def svg_worker(self): |
47 |
| - img = self.display_obj._repr_svg_() |
48 |
| - if img[:4] == '<svg': |
49 |
| - self.results["svg"] = True |
50 |
| - |
51 |
| - def pdf_worker(self): |
52 |
| - img = self.display_obj._repr_pdf_() |
53 |
| - if img[:4] == '%PDF': |
54 |
| - self.results["pdf"] = True |
| 8 | +version = six.sys.version_info[:2] # need this for conditional testing |
| 9 | + |
| 10 | +# unittest `skipIf` not supported in 2.6 and IPython not supported in 2.6/3.2 |
| 11 | +if version < (2, 7) or (2, 7) < version < (3, 3): |
| 12 | + pass |
| 13 | +else: |
| 14 | + |
| 15 | + class TestPlotlyDisplay(unittest.TestCase): |
| 16 | + |
| 17 | + def setUp(self): |
| 18 | + plot_info = {"un": "plotlyimagetest", "fid": "2"} |
| 19 | + url = "https://plot.ly/~{un}/{fid}".format(**plot_info) |
| 20 | + self.display_obj = tls.embed(url) |
| 21 | + self.results = {} |
| 22 | + self.images = {} |
| 23 | + self.threads = [] |
| 24 | + self.format_to_func = { |
| 25 | + "jpeg": self.jpeg_worker, |
| 26 | + "png": self.png_worker, |
| 27 | + "svg": self.svg_worker, |
| 28 | + "pdf": self.pdf_worker} |
| 29 | + |
| 30 | + def test_plotly_display(self): |
| 31 | + for f_format, func in self.format_to_func.items(): |
| 32 | + self.threads += [threading.Thread(target=func)] |
| 33 | + self.threads[-1].setDaemon(True) |
| 34 | + self.threads[-1].start() |
| 35 | + for thread in self.threads: |
| 36 | + thread.join() |
| 37 | + for f_format in self.format_to_func: |
| 38 | + result = self.results.get(f_format, False) |
| 39 | + print("{f_format}: {result}".format(f_format=f_format, |
| 40 | + result=result)) |
| 41 | + print("{image}\n".format(image=self.images[f_format][:72])) |
| 42 | + assert self.results.get(f_format) |
| 43 | + |
| 44 | + def jpeg_worker(self): |
| 45 | + self.images['jpeg'] = self.display_obj._repr_jpeg_() |
| 46 | + if imghdr.what('', self.images['jpeg']) == "jpeg": |
| 47 | + self.results["jpeg"] = True |
| 48 | + |
| 49 | + def png_worker(self): |
| 50 | + self.images['png'] = self.display_obj._repr_png_() |
| 51 | + if imghdr.what('', self.images['png']) == "png": |
| 52 | + self.results["png"] = True |
| 53 | + |
| 54 | + def svg_worker(self): |
| 55 | + self.images['svg'] = self.display_obj._repr_svg_() |
| 56 | + if self.images['svg'][:4] == six.b('<svg'): |
| 57 | + self.results["svg"] = True |
| 58 | + |
| 59 | + def pdf_worker(self): |
| 60 | + self.images['pdf'] = self.display_obj._repr_pdf_() |
| 61 | + if self.images['pdf'][:4] == six.b('%PDF'): |
| 62 | + self.results["pdf"] = True |
0 commit comments