Skip to content

Commit 20e9bef

Browse files
authored
Merge pull request #304 from plotly/Kaleido2
Added Kaleido
2 parents d8078c8 + 8962942 commit 20e9bef

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

plotly/export_fig2/getKaleido.m

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function status = getKaleido()
2+
status=0;
3+
kDir = fullfile(fileparts(mfilename('fullpath')),'..','kaleido');
4+
arch = computer('arch');
5+
plotlyJS = 'https://cdn.plot.ly/plotly-latest.min.js';
6+
7+
if isunix()
8+
if ismac()
9+
if strcmp(arch,'maci64')
10+
url = 'https://github.com/plotly/Kaleido/releases/download/v0.2.1/kaleido_mac_x64.zip';
11+
else
12+
url = 'https://github.com/plotly/Kaleido/releases/download/v0.2.1/kaleido_mac_arm64.zip';
13+
end
14+
else
15+
if strcmp(arch,'glnxa64')
16+
url = 'https://github.com/plotly/Kaleido/releases/download/v0.2.1/kaleido_linux_x64.zip';
17+
else
18+
url = 'https://github.com/plotly/Kaleido/releases/download/v0.2.1/kaleido_linux_arm64.zip';
19+
end
20+
end
21+
elseif ispc()
22+
if strcmp(arch,'win64')
23+
url = 'https://github.com/plotly/Kaleido/releases/download/v0.2.1/kaleido_win_x64.zip';
24+
else
25+
url = 'https://github.com/plotly/Kaleido/releases/download/v0.2.1/kaleido_win_x86.zip';
26+
end
27+
else
28+
fprintf('\nCouldn''t find a suitable version of "Kaleido" required for exporting plots, for your system\n');
29+
fprintf('https://github.com/plotly/Kaleido/tags\n');
30+
fprintf('Please download a suitable Kaleido version for your system, and unzip the files in plotly/Kaleido folder.\n\n');
31+
return;
32+
end
33+
34+
fprintf('\nTrying to download Kaleido executable and plotly javascript (one-time download)... please wait...');
35+
try
36+
file = websave(fullfile(kDir,'kaleido.zip'),url);
37+
websave(fullfile(kDir,'plotly-latest.min.js'),plotlyJS);
38+
catch
39+
fprintf('\nFailed to download Kaleido.\n');
40+
fprintf('https://github.com/plotly/Kaleido/tags\n');
41+
fprintf('Please download a suitable Kaleido version for your system, and unzip the files in plotly/Kaleido folder.\n\n');
42+
return;
43+
end
44+
unzip(file,kDir);
45+
status=1;
46+
fprintf(' Done\n\n');

plotly/export_fig2/write_image.m

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
function output = write_image(pfObj, imageFormat, filename, height, width, scale)
2+
3+
% Function to write plotly figures to a supported image format, which are the following: "png", "jpg", "jpeg", "webp", "svg", "pdf", "eps", "json"
4+
5+
debug=1;
6+
if nargin < 2
7+
imageFormat='png';
8+
filename='figure.png';
9+
height=500;
10+
width=800;
11+
scale=1;
12+
end
13+
14+
wd=fileparts(fileparts(mfilename('fullpath')));
15+
output=[];
16+
17+
if ~isa(pfObj,'plotlyfig')
18+
fprintf('\nError: Input is not a plotlyfig object.\n\n');
19+
return
20+
end
21+
22+
if isunix()
23+
kExec = string(fullfile(wd,'kaleido','kaleido'));
24+
cc="cat";
25+
else
26+
kExec = string(fullfile(wd,'kaleido','kaleido.cmd'));
27+
cc="type";
28+
end
29+
plyJsLoc = string(fullfile(wd,'kaleido','plotly-latest.min.js'));
30+
31+
if ~isfile(kExec) || ~isfile(plyJsLoc)
32+
status=getKaleido();
33+
else
34+
status=1;
35+
end
36+
37+
if status == 0
38+
return
39+
end
40+
41+
mjLoc = replace(string(fullfile(wd,'kaleido','etc','mathjax','MathJax.js')),'\','/');
42+
scope="plotly";
43+
44+
45+
% Prepare input plotly object for Kaleido
46+
q=struct();
47+
q.data.data = pfObj.data;
48+
q.data.layout = pfObj.layout;
49+
q.data.layout = rmfield(q.data.layout,'height');
50+
q.data.layout = rmfield(q.data.layout,'width');
51+
q.format = imageFormat;
52+
q.height = height;
53+
q.scale = scale;
54+
q.width = width;
55+
56+
pfJson = native2unicode(jsonencode(q),'UTF-8');
57+
tFile = string(fullfile(wd,'kaleido','temp.txt'));
58+
f=fopen(tFile,'w');
59+
fprintf(f,"%s",pfJson);
60+
fclose(f);
61+
62+
cmd=[cc," ",tFile," | ",kExec," ",scope," --plotlyjs='",plyJsLoc,"' ","--mathjax='file:///",mjLoc,"' --no-sandbox --disable-gpu --allow-file-access-from-files --disable-breakpad --disable-dev-shm-usage"];
63+
64+
if debug
65+
inputCmd=char(join(cmd,''));
66+
fprintf('\nDebug info:\n%s\n\n',inputCmd);
67+
end
68+
69+
[code,out]=system(char(join(cmd,'')));
70+
if debug
71+
disp(out);
72+
end
73+
74+
if code ~= 0
75+
fprintf('\nFatal: Failed to run Kaleido.\n\n');
76+
return;
77+
else
78+
a=string(split(out,newline));
79+
if a(end)==""
80+
a(end)=[];
81+
end
82+
output = jsondecode(a(end));
83+
end
84+
85+
if output.code ~= 0
86+
fprintf('\nError: %s\n',output.message);
87+
else
88+
out=unicode2native(output.result,'UTF-8');
89+
out=base64decode(out);
90+
f=fopen(filename,'wb');
91+
fwrite(f,out);
92+
fclose(f);
93+
end

plotly/kaleido/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

0 commit comments

Comments
 (0)