3
3
from io import BytesIO
4
4
from urllib .parse import quote as urlquote
5
5
import sys
6
- from kaleido .scopes .plotly import PlotlyScope
7
6
import logging
8
7
8
+ # bring in kaleido and ensure Chrome is installed
9
+ import kaleido
10
+ # this will download a small headless Chrome build the first time you run it
11
+ kaleido .get_chrome_sync ()
12
+
13
+ from kaleido import write_fig_sync
14
+
9
15
10
16
class Plot (object ):
11
17
"""A Plot object is defined by a path to the output file and the title of the plot."""
@@ -27,20 +33,18 @@ def encode(self):
27
33
return self .encode1 ()
28
34
29
35
def encode1 (self ):
30
- """Return the base64 encoding of the figure file and insert in html image tag."""
31
36
data_uri = b64encode (open (self .path , "rb" ).read ()).decode ("utf-8" ).replace ("\n " , "" )
32
37
return '<img src="data:image/png;base64,{0}">' .format (data_uri )
33
38
34
39
def encode2 (self ):
35
- """Return the base64 encoding of the fig attribute and insert in html image tag."""
36
40
buf = BytesIO ()
37
41
self .fig .savefig (buf , format = "png" , bbox_inches = "tight" , dpi = 100 )
38
42
buf .seek (0 )
39
43
string = b64encode (buf .read ())
40
44
return '<img src="data:image/png;base64,{0}">' .format (urlquote (string ))
41
45
42
46
def save (self , settings ):
43
- if not ( self .only_report ) :
47
+ if not self .only_report :
44
48
if self .html :
45
49
with open (self .path , "w" ) as html_out :
46
50
html_out .write (self .html )
@@ -52,12 +56,10 @@ def save(self, settings):
52
56
p = os .path .splitext (self .path )[0 ] + ".png"
53
57
if os .path .exists (p ):
54
58
os .remove (p )
55
-
56
59
logging .warning ("No static plots are saved due to some kaleido problem:" )
57
60
logging .warning (e )
58
61
59
62
elif self .fig :
60
- # if settings["format"] is a list, save the figure in all formats
61
63
if isinstance (settings ["format" ], list ):
62
64
for fmt in settings ["format" ]:
63
65
self .fig .savefig (
@@ -66,7 +68,11 @@ def save(self, settings):
66
68
bbox_inches = "tight" ,
67
69
)
68
70
else :
69
- self .fig .savefig (fname = self .path , format = settings ["format" ], bbox_inches = "tight" )
71
+ self .fig .savefig (
72
+ fname = self .path ,
73
+ format = settings ["format" ],
74
+ bbox_inches = "tight" ,
75
+ )
70
76
else :
71
77
sys .exit ("No method to save plot object: no html or fig defined." )
72
78
@@ -77,9 +83,14 @@ def show(self):
77
83
sys .stderr .write (".show not implemented for Plot instance without fig attribute!" )
78
84
79
85
def save_static (self , figformat ):
80
- scope = PlotlyScope ()
81
- with open (self .path .replace ("html" , figformat ), "wb" ) as f :
82
- f .write (scope .transform (self .fig , format = figformat ))
83
- logging .info (
84
- f"Saved { self .path .replace ('.html' , '' )} as { figformat } (or png for --legacy)"
85
- )
86
+ """
87
+ Export a Plotly figure via Kaleido v1’s write_fig_sync.
88
+ """
89
+ output_path = self .path .replace (".html" , f".{ figformat } " )
90
+ try :
91
+ write_fig_sync (self .fig , path = output_path )
92
+ logging .info (f"Saved { output_path } as { figformat } " )
93
+ except Exception as e :
94
+ logging .warning ("No static plots are saved due to some kaleido problem:" )
95
+ logging .warning (e )
96
+
0 commit comments