|
3 | 3 | import plotly.tools as tls
|
4 | 4 | import imghdr
|
5 | 5 | import threading
|
6 |
| - |
7 |
| - |
8 |
| -def test_plotly_display(): |
9 |
| - plot_info = {"un": "plotlyimagetest", "fid": "2"} |
10 |
| - url = "https://plot.ly/~{un}/{fid}".format(**plot_info) |
11 |
| - disp_obj = tls.embed(url) |
12 |
| - format_to_func = { |
13 |
| - "jpeg": jpeg_worker, |
14 |
| - "png": png_worker, |
15 |
| - "svg": svg_worker, |
16 |
| - "pdf": pdf_worker |
17 |
| - } |
18 |
| - results = {} |
19 |
| - threads = [] |
20 |
| - for f_format, func in format_to_func.items(): |
21 |
| - threads += [threading.Thread(target=func, args=(disp_obj, results))] |
22 |
| - threads[-1].setDaemon(True) |
23 |
| - threads[-1].start() |
24 |
| - for thread in threads: |
25 |
| - thread.join() |
26 |
| - for f_format in format_to_func: |
27 |
| - result = results.get(f_format, False) |
28 |
| - print("{f_format}: {result}".format(f_format=f_format, result=result)) |
29 |
| - assert results.get(f_format) |
30 |
| - |
31 |
| - |
32 |
| -def jpeg_worker(display_obj, results): |
33 |
| - img = display_obj._repr_jpeg_() |
34 |
| - if imghdr.what('', img) == "jpeg": |
35 |
| - results["jpeg"] = True |
36 |
| - |
37 |
| - |
38 |
| -def png_worker(display_obj, results): |
39 |
| - img = display_obj._repr_png_() |
40 |
| - if imghdr.what('', img) == "png": |
41 |
| - results["png"] = True |
42 |
| - |
43 |
| - |
44 |
| -def svg_worker(display_obj, results): |
45 |
| - img = display_obj._repr_svg_() |
46 |
| - if img[:4] == '<svg': |
47 |
| - results["svg"] = True |
48 |
| - |
49 |
| - |
50 |
| -def pdf_worker(display_obj, results): |
51 |
| - img = display_obj._repr_pdf_() |
52 |
| - if img[:4] == '%PDF': |
53 |
| - results["pdf"] = True |
| 6 | +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 |
0 commit comments