From 147c8d25b014c4787ac678bd99ff6beaa31331c0 Mon Sep 17 00:00:00 2001 From: Roberto Moura Date: Sat, 28 Nov 2026 14:33:45 +0000 Subject: [PATCH 01/12] plotlysetup_online: stop copying to matlabroot and editing startup.m --- plotly/plotly_setup_aux/addplotlystartup.m | 59 ----------- plotlysetup_online.m | 109 +-------------------- 2 files changed, 3 insertions(+), 165 deletions(-) delete mode 100644 plotly/plotly_setup_aux/addplotlystartup.m diff --git a/plotly/plotly_setup_aux/addplotlystartup.m b/plotly/plotly_setup_aux/addplotlystartup.m deleted file mode 100644 index dbacdebd..00000000 --- a/plotly/plotly_setup_aux/addplotlystartup.m +++ /dev/null @@ -1,59 +0,0 @@ -function [warnings] = addplotlystartup(startupPaths) - %[1]looks at startup.m files specified by the entries of startupPaths - %[2]appends the addplotly function to startup.m files (if not already - % present) - %[3]checks for other plotly addpath calls within any startup.m and - % outputs warning - - %output warnings - warnings = cell(size(startupPaths)); - - for locs = 1:size(startupPaths,1); - %addpath string for Plotly API - addString = ['addpath(genpath(fullfile(matlabroot,''toolbox'',' ... - '''plotly'')),''-end'');']; - %open current startup.m to read - currentStartupID = fopen(startupPaths{locs},'r'); - if currentStartupID == -1, error('plotly:startupRead', ... - ['\n\nShoot! It looks like something went wrong ' ... - 'reading the file: \n' startupPaths{locs} '\n\n' ... - 'Please contact your system admin or post a topic on ' ... - 'https://community.plotly.com/c/api/matlab/ for more ' ... - '\ninformation. In the mean time you can add the ' ... - 'Plotly API \nto your search path manually whenever ' ... - 'you need it! \n\n']); - end - %check for any instances of the addplotlyapi function - startupScan = textscan(currentStartupID, '%s', 'delimiter', ... - '\n', 'whitespace', ''); - startupLines = startupScan{1}; - Index = find(strcmp(startupLines,addString)); - otherPlotlyOccurrence = findstr(fileread(startupPaths{locs}), ... - 'plotly'); - %if addString is not in startup.m add it - if (~any(Index)) - %reopen current startup.m with new permission - currentStartupID = fopen(startupPaths{locs},'a+'); - if currentStartupID == -1 - error('plotly:startupWrite', ['\n\nShoot! It looks ' ... - 'like something went wrong writing to the ' ... - 'file: \n\n' startupPaths{locs} '\n\nPlease ' ... - 'contact your system admin or post a topic on ' ... - 'https://community.plotly.com/c/api/matlab/ ' ... - 'for more \ninformation. In the mean time you ' ... - 'can add the Plotly API \nto your search path ' ... - 'manually whenever you need it! \n']); - end - fprintf(currentStartupID,['\n' addString]); - end - if (length(Index) ~= length(otherPlotlyOccurrence)) - warnings{locs} = ['\n[WARNING]: \n\nWe found an addpath ' ... - 'specification for another version of Plotly at: ' ... - '\n\n' startupPaths{locs} '\n\nyou may be forcing ' ... - 'MATLAB to look for an older version of Plotly!\n\n']; - else - warnings{locs} = ''; - end - fclose(currentStartupID); - end -end diff --git a/plotlysetup_online.m b/plotlysetup_online.m index fa02012c..f1a05a27 100644 --- a/plotlysetup_online.m +++ b/plotlysetup_online.m @@ -5,10 +5,9 @@ function plotlysetup_online(username, api_key, varargin) % VALID PROPERTIES [OPTIONAL]: 'stream_ids' -> your stream ids [cell array] (found online) % 'plotly_domain' -> your desired REST API endpoint [string] % 'plotly_streaming_domain'-> your desired Stream API endpoint [string] - % [1] adds plotly api to matlabroot/toolboxes. If successful do [2] - % [2] adds plotly api to searchpath via startup.m of matlabroot and/or userpath - % [3] calls saveplotlycredentials (using username, api_key and stream_ids [optional]) - % [4] calls saveplotlyconfig with ('plotly_domain'[optional], 'plotly_streaming_domain' [optional]) + % [1] adds plotly api to search path + % [2] calls saveplotlycredentials (using username, api_key and stream_ids [optional]) + % [3] calls saveplotlyconfig with ('plotly_domain'[optional], 'plotly_streaming_domain' [optional]) %DEFAULT OUTPUT exception.message = ''; @@ -44,94 +43,6 @@ function plotlysetup_online(username, api_key, varargin) return end - if ~is_octave - try - %embed the api to the matlabroot/toolbox dir. - fprintf('\nAdding Plotly to MATLAB toolbox directory ... '); - - %plotly folder in the matlab/toolbox dir. - plotlyToolboxPath = fullfile(matlabroot,'toolbox','plotly'); - - if (exist(plotlyToolboxPath,'dir')) %check for overwrite... - fprintf(['\n\n[UPDATE]: \n\nHey! We see that a copy of Plotly has previously been added to\n' ... - 'your Matlab toolboxes. Would you like us to overwrite it with:\n' plotlyFolderPath ' ? \n'... - 'Careful! You may lose data saved to this Plotly directory.\n\n']); - - overwrite = input('Overwrite (y/n) ? : ','s'); - - if (strcmpi(overwrite,'y')); - fprintf('\n[OVERWRITE]:\n\nOverwriting Plotly! ... Done \n'); - else - fprintf('\n[NO OVERWRITE]:\n\nDid not overwrite Plotly! ... Done \n'); - end - else %toolbox Plotly not yet created - %worked (without interruption)...just a formatting thing! - fprintf('Done\n'); - - %make the plotlyToolboxPath dir. - status = mkdir(plotlyToolboxPath); - - %set status to overwrite - overwrite = 'y'; - - %check that the folder was created - if (status == 0) - error('plotly:savePlotly', permissionMessage('save the Plotly folder')); - end - end - - if strcmpi(overwrite,'y') - %move a copy of the Plotly api to matlab root directory - [status, msg, messid] = copyfile(plotlyFolderPath,plotlyToolboxPath); - %check that the plotly api was copied to the matlab root toolbox directory - if (status == 0) - if (~strcmp(messid, 'MATLAB:COPYFILE:SourceAndDestinationSame')) - error('plotly:copyPlotly',permissionMessage('copy the Plotly folder')); - end - end - end - - %add it to the searchpath (startup.m will handle this next time!) - addpath(genpath(plotlyToolboxPath),'-end'); - - %save plotly api searchpath to startup.m files (only do this if we actually were able to store the api in mtlroot/toolbox!) - fprintf('Saving Plotly to MATLAB search path via startup.m ... '); - - %check for a startup.m file in matlab rootpath (we want to add one here) - startupFile = []; - startupFileRootPath = fullfile(matlabroot,'toolbox','local','startup.m'); - if (~exist(startupFileRootPath,'file')) - startFileID = fopen(startupFileRootPath, 'w'); - %startup.m does not exist and startupFilePath is non-writable - if (startFileID == -1) - error('plotly:rootStartupCreation',permissionMessage('write the startup.m script')); - end - startupFile = {startupFileRootPath}; %needed because MATLAB only looks for startup.m when first opened. - end - - %check for all startup.m file in searchpath - startupFile = [startupFile; cell(which('startup.m','-all'))]; - %write the addpath - plotly api to the startup.m files - [warnings] = addplotlystartup(startupFile); - - %worked! - fprintf(' Done\n'); - - %print any addplotlydstatup warnings; - w = cellfun(@isempty,warnings); - if find(~w) - %output warnings - exception.warnings = warnings; - fprintf(warnings{find(~w)}); - end - - catch exception %copying to toolbox/writing to startup.m permission problem catch... - fprintf(['\n\n' exception.identifier exception.message '\n\n']); - end - else %if octave - fprintf('\n\nOctave users: Automatic Plotly API embedding coming soon!\n\n'); - end %end check for matlab... - try %save user credentials fprintf('Saving username/api_key credentials ... '); @@ -204,17 +115,3 @@ function plotlysetup_online(username, api_key, varargin) %greet the people! fprintf('\nWelcome to Plotly! If you are new to Plotly please enter: >> plotlyhelp to get started!\n\n') end - -%helper message function -function message = permissionMessage(spec) - message = ['\n\nShoot! We tried to ' spec ' to the MATLAB toolbox \n',... - 'directory, but were denied write permission. You''ll have to add\n',... - 'the Plotly folder to your MATLAB path manually by running: \n\n',... - '>> plotly_path = fullfile(pwd, ''plotly'')\n',... - '>> addpath(genpath(plotly_path))\n\n',... - 'You can save your credentials by running:\n\n', ... - '>>saveplotlycredentials(''your_username'', ''your_api_key'')\n\n',... - 'You can save your domain configuration by running:\n\n',... - '>>saveplotlyconfig(''your_base_domain'')\n\n',... - 'Questions? Ask https://community.plotly.com/c/api/matlab/\n\n']; -end From f71d255fb7c1e3fc609295a7709630c59e2cd4de Mon Sep 17 00:00:00 2001 From: Roberto Moura Date: Sat, 28 Nov 2026 14:40:49 +0000 Subject: [PATCH 02/12] Remove unused plotlycleanup --- plotly/plotly_setup_aux/plotlycleanup.m | 96 ------------------------- 1 file changed, 96 deletions(-) delete mode 100644 plotly/plotly_setup_aux/plotlycleanup.m diff --git a/plotly/plotly_setup_aux/plotlycleanup.m b/plotly/plotly_setup_aux/plotlycleanup.m deleted file mode 100644 index a8b34bf1..00000000 --- a/plotly/plotly_setup_aux/plotlycleanup.m +++ /dev/null @@ -1,96 +0,0 @@ -function removed = plotlycleanup - % cleans up any old Plotly API MATLAB library files and folders - - % initialize output - removed = {}; - - %----REMOVE WRAPPER FILES----% - REMOVEFILES = {'plotly.m'}; - - %----REMOVE WRAPPER FOLDERS----% - REMOVEFOLDERS = {'fig2plotly_aux'}; - - %----check for local Plotly instances----% - try - plotlyScriptDirs = which('plotly.m','-all'); - if isempty(plotlyScriptDirs); - error('plotly:missingScript', ['\n\nWe were unable to ' ... - 'locate plotly.m. Please Add this\nscript to your ' ... - 'MATLAB search path and try again.\n\n']); - end - catch exception %locating plotly error catch... - fprintf(['\n\n' exception.identifier exception.message '\n\n']); - return - end - - % plotly toolbox directory - plotlyToolboxDir = fullfile(matlabroot,'toolbox','plotly'); - - % find the location of all plotly/ directories - dircount = 1; - for d = 1:length(plotlyScriptDirs) - %parse filepath string at the Plotly directory - plotlyLoc = strfind(fileparts(plotlyScriptDirs{d}), ... - fullfile('MATLAB-api-master','plotly')); - plotlyToolboxLoc = strfind(fileparts(plotlyScriptDirs{d}), ... - plotlyToolboxDir); - if ~isempty(plotlyLoc) - plotlyDirs{dircount} = fullfile( ... - plotlyScriptDirs{d}(1:plotlyLoc-1), ... - 'MATLAB-api-master','plotly'); - dircount = dircount + 1; - elseif ~isempty(plotlyToolboxLoc) - plotlyDirs{dircount} = plotlyToolboxDir; - dircount = dircount + 1; - end - end - - for d = 1:length(plotlyDirs) - % add plotlydirs to searchpath - addpath(genpath(plotlyDirs{d})); - - % delete files from plotly directory - removefiles = fullfile(plotlyDirs{d}, REMOVEFILES); - - for f = 1:length(removefiles) - % remove removefiles filepath from searchpath - rmpath(fileparts(removefiles{f})); - - if exist(removefiles{f},'file') - delete(removefiles{f}); - % update removed list - removed = [removed removefiles{f}]; - end - - % add removefiles filepath back to searchpath - addpath(fileparts(removefiles{f})); - end - - % remove folders from plotly directory - removefolders = fullfile(plotlyDirs{d},REMOVEFOLDERS); - - for f = 1:length(removefolders) - if ~exist(removefolders{f}, 'dir') - continue - end - %remove folder from path - rmpath(genpath(removefolders{f})); - - %delete folder/subfolders - try - status = rmdir(removefolders{f},'s'); - if (status == 0) - error('plotly:deletePlotlyAPI', ['\n\nShoot! It ' ... - 'looks like something went wrong removing ' ... - 'the Plotly API from the MATLAB toolbox ' ... - 'directory \nPlease contact your system ' ... - 'admin or post a topic on ' ... - 'https://community.plotly.com/c/api/matlab/ ' ... - 'for more information. \n\n']); - end - % update removed list - removed = [removed removefolders{f}]; - end - end - end -end From 42e09180111a29c776294da9efa30a72e9730b2e Mon Sep 17 00:00:00 2001 From: Roberto Moura Date: Sat, 28 Nov 2026 14:43:53 +0000 Subject: [PATCH 03/12] plotlysetup: locate the plotly folder with mfilename --- plotlysetup_offline.m | 3 +-- plotlysetup_online.m | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/plotlysetup_offline.m b/plotlysetup_offline.m index 124bcae5..dd2cabcc 100644 --- a/plotlysetup_offline.m +++ b/plotlysetup_offline.m @@ -24,8 +24,7 @@ function plotlysetup_offline(plotly_bundle_url, varargin) try %check to see if plotly is in the searchpath - plotlysetupPath = which('plotlysetup'); - plotlyFolderPath = fullfile(fileparts(plotlysetupPath),'plotly'); + plotlyFolderPath = fullfile(fileparts(mfilename('fullpath')),'plotly'); %if it was not found if (strcmp(genpath(plotlyFolderPath),'')) error('plotly:notFound',... diff --git a/plotlysetup_online.m b/plotlysetup_online.m index f1a05a27..225c25c5 100644 --- a/plotlysetup_online.m +++ b/plotlysetup_online.m @@ -27,8 +27,7 @@ function plotlysetup_online(username, api_key, varargin) try %check to see if plotly is in the searchpath - plotlysetupPath = which('plotlysetup'); - plotlyFolderPath = fullfile(fileparts(plotlysetupPath),'plotly'); + plotlyFolderPath = fullfile(fileparts(mfilename('fullpath')),'plotly'); %if it was not found if (strcmp(genpath(plotlyFolderPath),'')) error('plotly:notFound',... From 380f43d55c35c01826f0189758a6a3cf192a6004 Mon Sep 17 00:00:00 2001 From: Roberto Moura Date: Sat, 28 Nov 2026 14:55:47 +0000 Subject: [PATCH 04/12] plotlysetup: persist the path without writing to matlabroot --- plotly/plotly_setup_aux/persistplotlypath.m | 21 +++++++++++++++++++++ plotlysetup_offline.m | 8 ++------ plotlysetup_online.m | 4 +++- 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 plotly/plotly_setup_aux/persistplotlypath.m diff --git a/plotly/plotly_setup_aux/persistplotlypath.m b/plotly/plotly_setup_aux/persistplotlypath.m new file mode 100644 index 00000000..4e815c2e --- /dev/null +++ b/plotly/plotly_setup_aux/persistplotlypath.m @@ -0,0 +1,21 @@ +function persistplotlypath(plotlyFolderPath) + % keeps plotlyFolderPath on the search path in future sessions without + % writing outside the user's own files: Octave's savepath writes the + % per-user ~/.octaverc, while MATLAB's normally writes MATLAB's own + % pathdef.m inside matlabroot, so MATLAB users get the line to add to + % their startup.m + if is_octave + try + savepath; + return + catch + end + startupFile = '~/.octaverc'; + else + startupFile = 'startup.m'; + end + fprintf(['\nPlotly is on the search path for this session. To keep it ', ... + 'there in future sessions,\nadd this line to your %s:\n\n', ... + ' addpath(genpath(''%s''));\n'], ... + startupFile, strrep(plotlyFolderPath, '''', '''''')); +end diff --git a/plotlysetup_offline.m b/plotlysetup_offline.m index dd2cabcc..d14e1bca 100644 --- a/plotlysetup_offline.m +++ b/plotlysetup_offline.m @@ -2,7 +2,7 @@ function plotlysetup_offline(plotly_bundle_url, varargin) % CALL: plotlysetup_offline(plotly_bundle_url); % WHERE: plotly_bundle_url is the plotly bundle url, e.g. https://cdn.plot.ly/plotly-2.35.3.min.js % If no argument is provided, the default https://cdn.plot.ly/plotly-2.35.3.min.js is used. - % [1] adds plotly api to search path and saves it via savepath + % [1] adds plotly api to search path and keeps it there via persistplotlypath %DEFAULT OUTPUT exception.message = ''; @@ -39,11 +39,7 @@ function plotlysetup_offline(plotly_bundle_url, varargin) return end - % Save search path so Plotly remains available in future sessions - try - savepath; - catch - end + persistplotlypath(plotlyFolderPath); %get offline bundle fprintf('\nNow downloading the plotly offline bundle ...'); diff --git a/plotlysetup_online.m b/plotlysetup_online.m index 225c25c5..2bcd326f 100644 --- a/plotlysetup_online.m +++ b/plotlysetup_online.m @@ -5,7 +5,7 @@ function plotlysetup_online(username, api_key, varargin) % VALID PROPERTIES [OPTIONAL]: 'stream_ids' -> your stream ids [cell array] (found online) % 'plotly_domain' -> your desired REST API endpoint [string] % 'plotly_streaming_domain'-> your desired Stream API endpoint [string] - % [1] adds plotly api to search path + % [1] adds plotly api to search path and keeps it there via persistplotlypath % [2] calls saveplotlycredentials (using username, api_key and stream_ids [optional]) % [3] calls saveplotlyconfig with ('plotly_domain'[optional], 'plotly_streaming_domain' [optional]) @@ -42,6 +42,8 @@ function plotlysetup_online(username, api_key, varargin) return end + persistplotlypath(plotlyFolderPath); + try %save user credentials fprintf('Saving username/api_key credentials ... '); From aec2407598846d26fe08b6d32450c0055ddeb917 Mon Sep 17 00:00:00 2001 From: Roberto Moura Date: Sat, 28 Nov 2026 15:26:33 +0000 Subject: [PATCH 05/12] Add Test_plotlysetup for the setup scripts --- plotly/Test_plotlysetup.m | 110 ++++++++++++++++++++++++++++++++ plotly/testing/runplotlytests.m | 3 +- 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 plotly/Test_plotlysetup.m diff --git a/plotly/Test_plotlysetup.m b/plotly/Test_plotlysetup.m new file mode 100644 index 00000000..002573f6 --- /dev/null +++ b/plotly/Test_plotlysetup.m @@ -0,0 +1,110 @@ +classdef Test_plotlysetup < PlotlyTestCase + methods + function testPlotlysetupOfflineFromAnyFolder(tc) + % the URL cannot resolve, so the real bundle is never replaced + sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok + + output = evalc(['try, plotlysetup_offline(', ... + '''https://plotly.invalid/plotly.min.js''); catch, end']); + + tc.verifyTrue(isempty(strfind(output, 'plotly:notFound')), output); + tc.verifyTrue(~isempty(strfind(output, 'Now downloading')), output); + end + + function testPlotlysetupOnlineFromAnyFolder(tc) + if ~is_octave() + return % MATLAB's getuserdir ignores HOME, so this would overwrite the real credentials. + end + sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok + + output = evalc('plotlysetup_online(''setup_test_user'', ''setup_test_key'');'); + + tc.verifyTrue(isempty(strfind(output, 'plotly:notFound')), output); + credentials = loadplotlycredentials(); + tc.verifyEqual(credentials.username, 'setup_test_user'); + end + + function testPersistPlotlyPathPrintsStartupLine(tc) + if is_octave() + return % Octave's savepath writes the per-user ~/.octaverc instead. + end + pathdefs = which('pathdef.m', '-all'); + before = Test_plotlysetup.fileDatenums(pathdefs); + + output = evalc('persistplotlypath(''/opt/it''''s here/plotly'')'); + + tc.verifyTrue(~isempty(strfind(output, 'startup.m')), output); + tc.verifyTrue(~isempty(strfind(output, ... + 'addpath(genpath(''/opt/it''''s here/plotly''));')), output); + tc.verifyEqual(Test_plotlysetup.fileDatenums(pathdefs), before); + end + + function testPersistPlotlyPathSavesOctaverc(tc) + if ~is_octave() + return % MATLAB users get a startup.m line instead. + end + plotlyFolder = fileparts(which('fig2plotly')); + sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok + + output = evalc('persistplotlypath(plotlyFolder)'); + + octaverc = fullfile(getenv('HOME'), '.octaverc'); + tc.verifyTrue(exist(octaverc, 'file') == 2, octaverc); + tc.verifyTrue(~isempty(strfind(fileread(octaverc), plotlyFolder))); + tc.verifyTrue(isempty(strfind(output, 'add this line')), output); + end + + function testPersistPlotlyPathOctavercUnwritable(tc) + if ~is_octave() + return % MATLAB users get a startup.m line instead. + end + sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok + setenv('HOME', fullfile(getenv('HOME'), 'missing')); + + output = evalc('persistplotlypath(''/opt/plotly'')'); + + tc.verifyTrue(~isempty(strfind(output, '~/.octaverc')), output); + tc.verifyTrue(~isempty(strfind(output, ... + 'addpath(genpath(''/opt/plotly''));')), output); + end + end + + methods (Static) + function sandbox = enterSetupSandbox() + %-run the setup scripts, which live in the repository root, + %-from an empty folder that is also HOME; the returned object + %-restores folder, HOME and the repository root's absence from + %-the path when it goes out of scope (restoring the whole path + %-with path() breaks the running test object in Octave)-% + oldFolder = pwd(); + oldHome = getenv('HOME'); + repoRoot = fileparts(fileparts(which('fig2plotly'))); + rootWasOnPath = ~isempty(strfind([pathsep path() pathsep], ... + [pathsep repoRoot pathsep])); + home = tempname(); + mkdir(home); + cd(home); + setenv('HOME', home); + addpath(repoRoot); + sandbox = onCleanup(@() Test_plotlysetup.leaveSetupSandbox( ... + oldFolder, oldHome, home, repoRoot, rootWasOnPath)); + end + + function leaveSetupSandbox(oldFolder, oldHome, home, repoRoot, rootWasOnPath) + if ~rootWasOnPath + rmpath(repoRoot); + end + cd(oldFolder); + setenv('HOME', oldHome); + try rmdir(home, 's'); catch; end + end + + function datenums = fileDatenums(files) + datenums = zeros(size(files)); + for i = 1:numel(files) + info = dir(files{i}); + datenums(i) = info.datenum; + end + end + end +end diff --git a/plotly/testing/runplotlytests.m b/plotly/testing/runplotlytests.m index 0311c3d2..d66369fd 100644 --- a/plotly/testing/runplotlytests.m +++ b/plotly/testing/runplotlytests.m @@ -13,7 +13,8 @@ function runplotlytests(varargin) %-every test method of the class. Without arguments the default %-suites run-% if nargin == 0 - args = {'Test_m2json', 'Test_plotlyfig', 'Test_plotlyfig_perf'}; + args = {'Test_m2json', 'Test_plotlyfig', 'Test_plotlyfig_perf', ... + 'Test_plotlysetup'}; else args = varargin; end From 455523e90f14412770cbef18faaf97670c92c8ed Mon Sep 17 00:00:00 2001 From: Roberto Moura Date: Sat, 28 Nov 2026 15:43:15 +0000 Subject: [PATCH 06/12] plotlysetup: print the addpath line in Octave too instead of calling savepath --- plotly/Test_plotlysetup.m | 45 +++++-------------- plotly/plotly_setup_aux/persistplotlypath.m | 21 --------- plotly/plotly_setup_aux/printplotlypathhint.m | 13 ++++++ plotlysetup_offline.m | 4 +- plotlysetup_online.m | 4 +- 5 files changed, 28 insertions(+), 59 deletions(-) delete mode 100644 plotly/plotly_setup_aux/persistplotlypath.m create mode 100644 plotly/plotly_setup_aux/printplotlypathhint.m diff --git a/plotly/Test_plotlysetup.m b/plotly/Test_plotlysetup.m index 002573f6..fa1ebbb5 100644 --- a/plotly/Test_plotlysetup.m +++ b/plotly/Test_plotlysetup.m @@ -24,48 +24,25 @@ function testPlotlysetupOnlineFromAnyFolder(tc) tc.verifyEqual(credentials.username, 'setup_test_user'); end - function testPersistPlotlyPathPrintsStartupLine(tc) + function testPrintPlotlyPathHint(tc) + sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok if is_octave() - return % Octave's savepath writes the per-user ~/.octaverc instead. + startupFile = '~/.octaverc'; + pathdefs = {}; + else + startupFile = 'startup.m'; + pathdefs = which('pathdef.m', '-all'); end - pathdefs = which('pathdef.m', '-all'); before = Test_plotlysetup.fileDatenums(pathdefs); - output = evalc('persistplotlypath(''/opt/it''''s here/plotly'')'); + output = evalc('printplotlypathhint(''/opt/it''''s here/plotly'')'); - tc.verifyTrue(~isempty(strfind(output, 'startup.m')), output); + tc.verifyTrue(~isempty(strfind(output, ... + ['add this line to your ' startupFile])), output); tc.verifyTrue(~isempty(strfind(output, ... 'addpath(genpath(''/opt/it''''s here/plotly''));')), output); tc.verifyEqual(Test_plotlysetup.fileDatenums(pathdefs), before); - end - - function testPersistPlotlyPathSavesOctaverc(tc) - if ~is_octave() - return % MATLAB users get a startup.m line instead. - end - plotlyFolder = fileparts(which('fig2plotly')); - sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok - - output = evalc('persistplotlypath(plotlyFolder)'); - - octaverc = fullfile(getenv('HOME'), '.octaverc'); - tc.verifyTrue(exist(octaverc, 'file') == 2, octaverc); - tc.verifyTrue(~isempty(strfind(fileread(octaverc), plotlyFolder))); - tc.verifyTrue(isempty(strfind(output, 'add this line')), output); - end - - function testPersistPlotlyPathOctavercUnwritable(tc) - if ~is_octave() - return % MATLAB users get a startup.m line instead. - end - sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok - setenv('HOME', fullfile(getenv('HOME'), 'missing')); - - output = evalc('persistplotlypath(''/opt/plotly'')'); - - tc.verifyTrue(~isempty(strfind(output, '~/.octaverc')), output); - tc.verifyTrue(~isempty(strfind(output, ... - 'addpath(genpath(''/opt/plotly''));')), output); + tc.verifyNumElements(dir(fullfile(getenv('HOME'), '.octaverc')), 0); end end diff --git a/plotly/plotly_setup_aux/persistplotlypath.m b/plotly/plotly_setup_aux/persistplotlypath.m deleted file mode 100644 index 4e815c2e..00000000 --- a/plotly/plotly_setup_aux/persistplotlypath.m +++ /dev/null @@ -1,21 +0,0 @@ -function persistplotlypath(plotlyFolderPath) - % keeps plotlyFolderPath on the search path in future sessions without - % writing outside the user's own files: Octave's savepath writes the - % per-user ~/.octaverc, while MATLAB's normally writes MATLAB's own - % pathdef.m inside matlabroot, so MATLAB users get the line to add to - % their startup.m - if is_octave - try - savepath; - return - catch - end - startupFile = '~/.octaverc'; - else - startupFile = 'startup.m'; - end - fprintf(['\nPlotly is on the search path for this session. To keep it ', ... - 'there in future sessions,\nadd this line to your %s:\n\n', ... - ' addpath(genpath(''%s''));\n'], ... - startupFile, strrep(plotlyFolderPath, '''', '''''')); -end diff --git a/plotly/plotly_setup_aux/printplotlypathhint.m b/plotly/plotly_setup_aux/printplotlypathhint.m new file mode 100644 index 00000000..0b00d70b --- /dev/null +++ b/plotly/plotly_setup_aux/printplotlypathhint.m @@ -0,0 +1,13 @@ +function printplotlypathhint(plotlyFolderPath) + % prints the line that keeps plotlyFolderPath on the search path in + % future sessions; setup leaves editing startup files to the user + if is_octave + startupFile = '~/.octaverc'; + else + startupFile = 'startup.m'; + end + fprintf(['\nPlotly is on the search path for this session. To keep it ', ... + 'there in future sessions,\nadd this line to your %s:\n\n', ... + ' addpath(genpath(''%s''));\n'], ... + startupFile, strrep(plotlyFolderPath, '''', '''''')); +end diff --git a/plotlysetup_offline.m b/plotlysetup_offline.m index d14e1bca..ff89c9b4 100644 --- a/plotlysetup_offline.m +++ b/plotlysetup_offline.m @@ -2,7 +2,7 @@ function plotlysetup_offline(plotly_bundle_url, varargin) % CALL: plotlysetup_offline(plotly_bundle_url); % WHERE: plotly_bundle_url is the plotly bundle url, e.g. https://cdn.plot.ly/plotly-2.35.3.min.js % If no argument is provided, the default https://cdn.plot.ly/plotly-2.35.3.min.js is used. - % [1] adds plotly api to search path and keeps it there via persistplotlypath + % [1] adds plotly api to search path and prints how to keep it there %DEFAULT OUTPUT exception.message = ''; @@ -39,7 +39,7 @@ function plotlysetup_offline(plotly_bundle_url, varargin) return end - persistplotlypath(plotlyFolderPath); + printplotlypathhint(plotlyFolderPath); %get offline bundle fprintf('\nNow downloading the plotly offline bundle ...'); diff --git a/plotlysetup_online.m b/plotlysetup_online.m index 2bcd326f..b5fae8a1 100644 --- a/plotlysetup_online.m +++ b/plotlysetup_online.m @@ -5,7 +5,7 @@ function plotlysetup_online(username, api_key, varargin) % VALID PROPERTIES [OPTIONAL]: 'stream_ids' -> your stream ids [cell array] (found online) % 'plotly_domain' -> your desired REST API endpoint [string] % 'plotly_streaming_domain'-> your desired Stream API endpoint [string] - % [1] adds plotly api to search path and keeps it there via persistplotlypath + % [1] adds plotly api to search path and prints how to keep it there % [2] calls saveplotlycredentials (using username, api_key and stream_ids [optional]) % [3] calls saveplotlyconfig with ('plotly_domain'[optional], 'plotly_streaming_domain' [optional]) @@ -42,7 +42,7 @@ function plotlysetup_online(username, api_key, varargin) return end - persistplotlypath(plotlyFolderPath); + printplotlypathhint(plotlyFolderPath); try %save user credentials fprintf('Saving username/api_key credentials ... '); From 307fd40861c7094c3316d8fc4cc53b4c2d8481f3 Mon Sep 17 00:00:00 2001 From: Roberto Moura Date: Sat, 28 Nov 2026 15:47:10 +0000 Subject: [PATCH 07/12] Test_plotlysetup: sandbox APPDATA and skip when getuserdir escapes it --- plotly/Test_plotlysetup.m | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/plotly/Test_plotlysetup.m b/plotly/Test_plotlysetup.m index fa1ebbb5..36c8983e 100644 --- a/plotly/Test_plotlysetup.m +++ b/plotly/Test_plotlysetup.m @@ -12,10 +12,10 @@ function testPlotlysetupOfflineFromAnyFolder(tc) end function testPlotlysetupOnlineFromAnyFolder(tc) - if ~is_octave() - return % MATLAB's getuserdir ignores HOME, so this would overwrite the real credentials. - end sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok + if ~strcmp(getuserdir(), getenv('HOME')) + return % getuserdir() is outside the sandbox, so this would overwrite the real credentials. + end output = evalc('plotlysetup_online(''setup_test_user'', ''setup_test_key'');'); @@ -49,12 +49,14 @@ function testPrintPlotlyPathHint(tc) methods (Static) function sandbox = enterSetupSandbox() %-run the setup scripts, which live in the repository root, - %-from an empty folder that is also HOME; the returned object - %-restores folder, HOME and the repository root's absence from + %-from an empty folder that is also HOME and APPDATA (where + %-getuserdir looks on Windows); the returned object restores + %-folder, HOME, APPDATA and the repository root's absence from %-the path when it goes out of scope (restoring the whole path %-with path() breaks the running test object in Octave)-% oldFolder = pwd(); oldHome = getenv('HOME'); + oldAppdata = getenv('APPDATA'); repoRoot = fileparts(fileparts(which('fig2plotly'))); rootWasOnPath = ~isempty(strfind([pathsep path() pathsep], ... [pathsep repoRoot pathsep])); @@ -62,17 +64,19 @@ function testPrintPlotlyPathHint(tc) mkdir(home); cd(home); setenv('HOME', home); + setenv('APPDATA', home); addpath(repoRoot); sandbox = onCleanup(@() Test_plotlysetup.leaveSetupSandbox( ... - oldFolder, oldHome, home, repoRoot, rootWasOnPath)); + oldFolder, oldHome, oldAppdata, home, repoRoot, rootWasOnPath)); end - function leaveSetupSandbox(oldFolder, oldHome, home, repoRoot, rootWasOnPath) + function leaveSetupSandbox(oldFolder, oldHome, oldAppdata, home, repoRoot, rootWasOnPath) if ~rootWasOnPath rmpath(repoRoot); end cd(oldFolder); setenv('HOME', oldHome); + setenv('APPDATA', oldAppdata); try rmdir(home, 's'); catch; end end From b5df1cc4d755465cdfdcc94b819f5db6f9e4335e Mon Sep 17 00:00:00 2001 From: Roberto Moura Date: Sat, 28 Nov 2026 15:52:50 +0000 Subject: [PATCH 08/12] Test_plotlysetup: remove the sandbox without Octave's rmdir prompt --- plotly/Test_plotlysetup.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plotly/Test_plotlysetup.m b/plotly/Test_plotlysetup.m index 36c8983e..adbfe90f 100644 --- a/plotly/Test_plotlysetup.m +++ b/plotly/Test_plotlysetup.m @@ -77,6 +77,9 @@ function leaveSetupSandbox(oldFolder, oldHome, oldAppdata, home, repoRoot, rootW cd(oldFolder); setenv('HOME', oldHome); setenv('APPDATA', oldAppdata); + if is_octave() + confirm_recursive_rmdir(false, 'local'); + end try rmdir(home, 's'); catch; end end From e9c107bee4e67d55227dfdcd6beccb0da8fb702a Mon Sep 17 00:00:00 2001 From: Roberto Moura Date: Sat, 28 Nov 2026 15:54:28 +0000 Subject: [PATCH 09/12] printplotlypathhint: say where MATLAB's startup.m goes --- plotly/Test_plotlysetup.m | 2 +- plotly/plotly_setup_aux/printplotlypathhint.m | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plotly/Test_plotlysetup.m b/plotly/Test_plotlysetup.m index adbfe90f..72a888a1 100644 --- a/plotly/Test_plotlysetup.m +++ b/plotly/Test_plotlysetup.m @@ -30,7 +30,7 @@ function testPrintPlotlyPathHint(tc) startupFile = '~/.octaverc'; pathdefs = {}; else - startupFile = 'startup.m'; + startupFile = 'startup.m (in the folder that userpath returns)'; pathdefs = which('pathdef.m', '-all'); end before = Test_plotlysetup.fileDatenums(pathdefs); diff --git a/plotly/plotly_setup_aux/printplotlypathhint.m b/plotly/plotly_setup_aux/printplotlypathhint.m index 0b00d70b..33ad6eec 100644 --- a/plotly/plotly_setup_aux/printplotlypathhint.m +++ b/plotly/plotly_setup_aux/printplotlypathhint.m @@ -4,7 +4,7 @@ function printplotlypathhint(plotlyFolderPath) if is_octave startupFile = '~/.octaverc'; else - startupFile = 'startup.m'; + startupFile = 'startup.m (in the folder that userpath returns)'; end fprintf(['\nPlotly is on the search path for this session. To keep it ', ... 'there in future sessions,\nadd this line to your %s:\n\n', ... From 1a83d1d035671d994ead64f4b7e309dd4fd13b5a Mon Sep 17 00:00:00 2001 From: Roberto Moura Date: Sat, 28 Nov 2026 15:56:30 +0000 Subject: [PATCH 10/12] plotlysetup: name the actual setup scripts in messages --- plotlysetup_offline.m | 2 +- plotlysetup_online.m | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/plotlysetup_offline.m b/plotlysetup_offline.m index ff89c9b4..2ef9d18f 100644 --- a/plotlysetup_offline.m +++ b/plotlysetup_offline.m @@ -30,7 +30,7 @@ function plotlysetup_offline(plotly_bundle_url, varargin) error('plotly:notFound',... ['\n\nShoot! It looks like MATLAB is having trouble finding the current version ' ... '\nof Plotly. Please make sure that the Plotly API folder is in the same ' ... - '\ndirectory as plotlysetup.m. Questions? Ask https://community.plotly.com/c/api/matlab/\n\n']); + '\ndirectory as plotlysetup_offline.m. Questions? Ask https://community.plotly.com/c/api/matlab/\n\n']); end %add Plotly API MATLAB Library to search path addpath(genpath(plotlyFolderPath)); diff --git a/plotlysetup_online.m b/plotlysetup_online.m index b5fae8a1..902eef8c 100644 --- a/plotlysetup_online.m +++ b/plotlysetup_online.m @@ -33,7 +33,7 @@ function plotlysetup_online(username, api_key, varargin) error('plotly:notFound',... ['\n\nShoot! It looks like MATLAB is having trouble finding the current version ' ... '\nof Plotly. Please make sure that the Plotly API folder is in the same ' ... - '\ndirectory as plotlysetup.m. Questions? Ask on https://community.plotly.com/c/api/matlab/\n\n']); + '\ndirectory as plotlysetup_online.m. Questions? Ask on https://community.plotly.com/c/api/matlab/\n\n']); end %add Plotly API MATLAB Library to search path addpath(genpath(plotlyFolderPath)); @@ -61,7 +61,7 @@ function plotlysetup_online(username, api_key, varargin) %check for ..,property,value,.. structure if mod(numel(varargin),2)~= 0 error('plotly:wrongInputVarargin',.... - ['\n\nWhoops! Wrong number of varargin inputs. Please run >> help plotlysetup \n',... + ['\n\nWhoops! Wrong number of varargin inputs. Please run >> help plotlysetup_online \n',... 'for more information regarding the setup of your Plotly API MATLAB Library. \n',... 'Your stream_ids, plotly_domain, and plotly_streaming domain were not set. \n',... 'Questions? Please post on https://community.plotly.com/c/api/matlab/22.']); @@ -72,7 +72,7 @@ function plotlysetup_online(username, api_key, varargin) if isempty(intersect(varargin{n},{'stream_ids','plotly_domain','plotly_streaming_domain'})) error('plotly:wrongInputPropertyName',.... ['\n\nWhoops! The properperty name: ' varargin{n} ' is invalid. \n',... - 'Please run >> help plotlysetup for more information regarding\n',... + 'Please run >> help plotlysetup_online for more information regarding\n',... 'the setup your Plotly API MATLAB Library.']); end if strcmp(varargin{n},'stream_ids') From cb4b8e02a41896c5b0278553baa58f4dbda2b667 Mon Sep 17 00:00:00 2001 From: Roberto Moura Date: Sat, 28 Nov 2026 16:13:58 +0000 Subject: [PATCH 11/12] Test_plotlysetup: drop the online setup test, which signs in a fake account --- plotly/Test_plotlysetup.m | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/plotly/Test_plotlysetup.m b/plotly/Test_plotlysetup.m index 72a888a1..ca21b3e5 100644 --- a/plotly/Test_plotlysetup.m +++ b/plotly/Test_plotlysetup.m @@ -11,19 +11,6 @@ function testPlotlysetupOfflineFromAnyFolder(tc) tc.verifyTrue(~isempty(strfind(output, 'Now downloading')), output); end - function testPlotlysetupOnlineFromAnyFolder(tc) - sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok - if ~strcmp(getuserdir(), getenv('HOME')) - return % getuserdir() is outside the sandbox, so this would overwrite the real credentials. - end - - output = evalc('plotlysetup_online(''setup_test_user'', ''setup_test_key'');'); - - tc.verifyTrue(isempty(strfind(output, 'plotly:notFound')), output); - credentials = loadplotlycredentials(); - tc.verifyEqual(credentials.username, 'setup_test_user'); - end - function testPrintPlotlyPathHint(tc) sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok if is_octave() @@ -49,14 +36,12 @@ function testPrintPlotlyPathHint(tc) methods (Static) function sandbox = enterSetupSandbox() %-run the setup scripts, which live in the repository root, - %-from an empty folder that is also HOME and APPDATA (where - %-getuserdir looks on Windows); the returned object restores - %-folder, HOME, APPDATA and the repository root's absence from + %-from an empty folder that is also HOME; the returned object + %-restores folder, HOME and the repository root's absence from %-the path when it goes out of scope (restoring the whole path %-with path() breaks the running test object in Octave)-% oldFolder = pwd(); oldHome = getenv('HOME'); - oldAppdata = getenv('APPDATA'); repoRoot = fileparts(fileparts(which('fig2plotly'))); rootWasOnPath = ~isempty(strfind([pathsep path() pathsep], ... [pathsep repoRoot pathsep])); @@ -64,19 +49,17 @@ function testPrintPlotlyPathHint(tc) mkdir(home); cd(home); setenv('HOME', home); - setenv('APPDATA', home); addpath(repoRoot); sandbox = onCleanup(@() Test_plotlysetup.leaveSetupSandbox( ... - oldFolder, oldHome, oldAppdata, home, repoRoot, rootWasOnPath)); + oldFolder, oldHome, home, repoRoot, rootWasOnPath)); end - function leaveSetupSandbox(oldFolder, oldHome, oldAppdata, home, repoRoot, rootWasOnPath) + function leaveSetupSandbox(oldFolder, oldHome, home, repoRoot, rootWasOnPath) if ~rootWasOnPath rmpath(repoRoot); end cd(oldFolder); setenv('HOME', oldHome); - setenv('APPDATA', oldAppdata); if is_octave() confirm_recursive_rmdir(false, 'local'); end From 4d1c2a1ec81569d7e94cd6f0ba5a92ff2c72c626 Mon Sep 17 00:00:00 2001 From: Roberto Moura Date: Sat, 28 Nov 2026 16:17:43 +0000 Subject: [PATCH 12/12] Test_plotlysetup: check that offline setup writes no startup file --- plotly/Test_plotlysetup.m | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/plotly/Test_plotlysetup.m b/plotly/Test_plotlysetup.m index ca21b3e5..434efa49 100644 --- a/plotly/Test_plotlysetup.m +++ b/plotly/Test_plotlysetup.m @@ -1,35 +1,39 @@ classdef Test_plotlysetup < PlotlyTestCase methods - function testPlotlysetupOfflineFromAnyFolder(tc) - % the URL cannot resolve, so the real bundle is never replaced + function testPlotlysetupOffline(tc) + % finds plotly/ from an unrelated folder and writes no startup + % file; the URL cannot resolve, so the real bundle is never replaced sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok + if is_octave() + pathdefs = {}; + else + pathdefs = which('pathdef.m', '-all'); + end + before = Test_plotlysetup.fileContents(pathdefs); output = evalc(['try, plotlysetup_offline(', ... '''https://plotly.invalid/plotly.min.js''); catch, end']); tc.verifyTrue(isempty(strfind(output, 'plotly:notFound')), output); tc.verifyTrue(~isempty(strfind(output, 'Now downloading')), output); + tc.verifyTrue(isequal(Test_plotlysetup.fileContents(pathdefs), before), ... + 'a pathdef.m on the search path changed'); + tc.verifyNumElements(dir(fullfile(getenv('HOME'), '.octaverc')), 0); end function testPrintPlotlyPathHint(tc) - sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok if is_octave() startupFile = '~/.octaverc'; - pathdefs = {}; else startupFile = 'startup.m (in the folder that userpath returns)'; - pathdefs = which('pathdef.m', '-all'); end - before = Test_plotlysetup.fileDatenums(pathdefs); output = evalc('printplotlypathhint(''/opt/it''''s here/plotly'')'); tc.verifyTrue(~isempty(strfind(output, ... - ['add this line to your ' startupFile])), output); + ['add this line to your ' startupFile ':'])), output); tc.verifyTrue(~isempty(strfind(output, ... 'addpath(genpath(''/opt/it''''s here/plotly''));')), output); - tc.verifyEqual(Test_plotlysetup.fileDatenums(pathdefs), before); - tc.verifyNumElements(dir(fullfile(getenv('HOME'), '.octaverc')), 0); end end @@ -66,11 +70,10 @@ function leaveSetupSandbox(oldFolder, oldHome, home, repoRoot, rootWasOnPath) try rmdir(home, 's'); catch; end end - function datenums = fileDatenums(files) - datenums = zeros(size(files)); + function contents = fileContents(files) + contents = cell(size(files)); for i = 1:numel(files) - info = dir(files{i}); - datenums(i) = info.datenum; + contents{i} = fileread(files{i}); end end end