Skip to content

Commit 4d9df0a

Browse files
committed
first getplotlyoffline + plotlyoffline
1 parent ec7506c commit 4d9df0a

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function getplotlyoffline(plotly_bundle_url)
2+
3+
% download bundle
4+
[plotly_bundle, extras] = urlread2(plotly_bundle_url, 'get');
5+
6+
% handle response
7+
if ~extras.isGood
8+
error(['Whoops! There was an error attempting to ', ...
9+
'download the MATLAB offline Plotly ', ...
10+
'bundle. Status: %s %s.'], ...
11+
num2str(extras.status.value), extras.status.msg);
12+
end
13+
14+
% create Plotly config folder
15+
userhome = getuserdir();
16+
plotly_config_folder = fullfile(userhome, '.plotly');
17+
[status, mess, messid] = mkdir(plotly_config_folder);
18+
validatedir(status, mess, messid, 'plotly');
19+
20+
% create plotlyjs folder
21+
plotly_js_folder = fullfile(plotly_config_folder, 'plotlyjs');
22+
[status, mess, messid] = mkdir(plotly_js_folder);
23+
validatedir(status, mess, messid, 'plotlyjs');
24+
25+
% save bundle
26+
bundle = escapechars(plotly_bundle);
27+
bundle_name = 'plotly-matlab-offline-bundle.js';
28+
bundle_file = fullfile(plotly_js_folder, bundle_name);
29+
file_id = fopen(bundle_file, 'w');
30+
fprintf(file_id, '%s', bundle);
31+
fclose(file_id);
32+
end
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
function response = plotlyoffline(plotlyfig)
2+
% Generate offline Plotly figure saved as an html file within
3+
% the current working directory. The file will be saved as:
4+
% 'plotlyfig.PlotOptions.FileName'.html.
5+
6+
% grab the bundled dependencies
7+
userhome = getuserdir();
8+
plotly_config_folder = fullfile(userhome,'.plotly');
9+
plotly_js_folder = fullfile(plotly_config_folder, 'plotlyjs');
10+
bundle_name = 'plotly-matlab-offline-bundle.js';
11+
bundle_file = fullfile(plotly_js_folder, bundle_name);
12+
13+
% check that the bundle exists
14+
try
15+
bundle = fileread(bundle_file);
16+
catch
17+
error(['Error reading: %s.\nPlease download the required ', ...
18+
'dependencies using: >>getplotloffline \n', ...
19+
'or contact [email protected] for assistance.'], ...
20+
bundle_file);
21+
end
22+
23+
% handle plot div specs
24+
id = char(java.util.UUID.randomUUID);
25+
width = [num2str(plotlyfig.layout.width) 'px'];
26+
height = [num2str(plotlyfig.layout.height) 'px'];
27+
28+
if plotlyfig.PlotOptions.ShowLinkText
29+
link_text = plotlyfig.PlotOptions.LinkText;
30+
else
31+
link_text = '';
32+
end
33+
34+
% format the data and layout
35+
jdata = m2json(plotlyfig.data);
36+
jlayout = m2json(plotlyfig.layout);
37+
clean_jdata = escapechars(jdata);
38+
clean_jlayout = escapechars(jlayout);
39+
40+
% template dependencies
41+
dep_script = sprintf('<script type="text/javascript">%s</script>', ...
42+
bundle);
43+
44+
% template environment vars
45+
plotly_domain = plotlyfig.UserData.PlotlyDomain;
46+
env_script = sprintf(['\n<script type="text/javascript">', ...
47+
'window.PLOTLYENV=window.PLOTLYENV || {};', ...
48+
'window.PLOTLYENV.BASE_URL="%s";', ...
49+
'Plotly.LINKTEXT="%s";', ...
50+
'</script>'], plotly_domain, link_text);
51+
52+
% template Plotly.plot
53+
script = sprintf(['\n Plotly.plot("%s", %s, %s).then(function(){'...
54+
'\n $(".%s.loading").remove();' ...
55+
'\n $(".link--embedview").text("%s");'...
56+
'\n });'], id, clean_jdata, clean_jlayout, ...
57+
id, link_text);
58+
59+
plotly_script = sprintf(['\n<div class="%s loading" style=', ...
60+
'color: rgb(50,50,50);">Drawing...</div>' ...
61+
'\n<div id="%s" style="height: %s;',...
62+
'width: %s;" class="plotly-graph-div">' ...
63+
'</div> \n<script type="text/javascript">' ...
64+
'%s \n</script>'], id, id, height, width, ...
65+
script);
66+
67+
% template entire script
68+
offline_script = [dep_script env_script plotly_script];
69+
filename = plotlyfig.PlotOptions.FileName;
70+
71+
% remove the whitespace from the filename
72+
clean_filename = filename(filename~=' ');
73+
html_filename = [clean_filename '.html'];
74+
75+
% save the html file in the working directory
76+
plotly_offline_file = fullfile(pwd, html_filename);
77+
file_id = fopen(plotly_offline_file, 'w');
78+
fprintf(file_id, offline_script);
79+
fclose(file_id);
80+
81+
% return the local file url to be rendered in the browser
82+
response = ['file://' plotly_offline_file];
83+
84+
end

0 commit comments

Comments
 (0)