1
+ from __future__ import absolute_import
2
+
3
+ import plotly .tools as tls
4
+ import imghdr
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
0 commit comments