diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c2bc58a5..a57858c6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,6 +3,7 @@ name: CI on: push: branches: [master, develop] + tags: ['[0-9]+.[0-9]+.[0-9]+'] pull_request: jobs: @@ -10,7 +11,7 @@ jobs: runs-on: ubuntu-26.04 steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v7 - name: Install Octave, Xvfb and packages run: | @@ -32,10 +33,10 @@ jobs: strategy: fail-fast: false matrix: - release: [R2021a, R2022a, R2023a, R2024a, R2025a, R2026a] + release: [R2021a, R2022a, R2023a, R2024a, R2025a, R2026a, R2026b] steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v7 - name: Set up MATLAB uses: matlab-actions/setup-matlab@v3 @@ -60,7 +61,116 @@ jobs: - name: Upload gallery if: ${{ !cancelled() }} - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v7 with: name: gallery-${{ matrix.release }} path: gallery/ + + package-toolbox: + runs-on: ubuntu-24.04 + steps: + - name: Checkout + uses: actions/checkout@v7 + + # the toolbox version comes from plotly_version.m, and File Exchange + # expects it to equal the release version + - name: Check the tag matches plotly_version.m + if: startsWith(github.ref, 'refs/tags/') + run: | + version=$(grep -oE '"[0-9]+\.[0-9]+\.[0-9]+"' plotly/plotly_aux/plotly_version.m | tr -d '"') + if [ "$version" != "$GITHUB_REF_NAME" ]; then + echo "::error::tag $GITHUB_REF_NAME does not match plotly_version.m ($version)" + exit 1 + fi + + # R2023a is the oldest release with matlab.addons.toolbox.ToolboxOptions + - name: Set up MATLAB + uses: matlab-actions/setup-matlab@v3 + with: + release: R2023a + cache: true + + - name: Package toolbox + uses: matlab-actions/run-command@v3 + with: + command: buildtool package + + - name: Upload toolbox + uses: actions/upload-artifact@v7 + with: + name: toolbox + path: release/*.mltbx + + install-toolbox: + name: install-toolbox (${{ matrix.os }}, ${{ matrix.release }}) + needs: package-toolbox + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-24.04] + release: [R2021a, R2026a, R2026b] + include: + - os: windows-latest + release: R2026b + - os: macos-latest + release: R2026b + steps: + # no checkout: fig2plotly must come from the installed toolbox + - name: Download toolbox + uses: actions/download-artifact@v8 + with: + name: toolbox + path: release + + - name: Set up MATLAB + uses: matlab-actions/setup-matlab@v3 + with: + release: ${{ matrix.release }} + cache: true + + - name: Install, use and uninstall the toolbox + uses: matlab-actions/run-command@v3 + with: + command: | + mltbx = dir(fullfile("release", "*.mltbx")); + assert(isscalar(mltbx), "expected one .mltbx in release/"); + installed = matlab.addons.toolbox.installToolbox(fullfile(mltbx.folder, mltbx.name)); + disp(installed); + location = which("fig2plotly"); + assert(~isempty(location), "fig2plotly is not on the path after installing"); + disp(location); + assert(isfile(fullfile(fileparts(location), "LICENSE")), "the toolbox has no LICENSE"); + fig = figure("Visible", "off"); + plot(1:3); + p = plotlyfig(fig, "visible", "off"); + assert(numel(p.data) == 1, "plotlyfig converted %d traces, expected 1", numel(p.data)); + addtheme(struct("layout", struct()), "ggplot2"); + clear p; % plotlyfig's destructor must run while the toolbox is still installed + close(fig); + matlab.addons.toolbox.uninstallToolbox(installed); + assert(isempty(which("fig2plotly")), "fig2plotly is still on the path after uninstalling"); + + # Creates a draft with the .mltbx attached; a maintainer publishes it, so + # services that react to published releases see it complete. + release: + if: startsWith(github.ref, 'refs/tags/') + needs: [octave-tests, matlab-tests, install-toolbox] + runs-on: ubuntu-24.04 + permissions: + contents: write + steps: + # the exact .mltbx the install jobs tested + - name: Download toolbox + uses: actions/download-artifact@v8 + with: + name: toolbox + path: release + + - name: Create draft release + env: + GH_TOKEN: ${{ github.token }} + run: >- + gh release create "$GITHUB_REF_NAME" release/*.mltbx + --repo "$GITHUB_REPOSITORY" --verify-tag --draft + --title "$GITHUB_REF_NAME" --generate-notes diff --git a/.gitignore b/.gitignore index a3e48acb..0f98db54 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ dev/ *.asv untitled.html +release/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c2cc8b48..73e3d9ba 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -62,3 +62,29 @@ makegallery('Functions', {'myplot'}, 'Open', false) ``` Keep commits to one logical change with a short imperative message. + +## Releasing + +Releases are made from `master` by pushing a version tag. CI then tests the +toolbox (`.mltbx`) and attaches it to a draft GitHub release. + +1. Set the new version in `plotly/plotly_aux/plotly_version.m` (for example + `"3.2.0"`) and merge that change into `master`. This file is the only place + the version is kept; the toolbox takes its version from it. +2. Tag the merged commit with the plain version (no `v` prefix) and push the + tag: + + ```bash + git tag 3.2.0 + git push origin 3.2.0 + ``` + + CI checks that the tag matches `plotly_version.m`, runs all the tests, + installs the packaged toolbox on Linux, Windows and macOS, and then creates + a draft release with `plotly_matlab-.mltbx` attached and release + notes generated from the merged pull requests. +3. Review the draft on the repository's Releases page and click **Publish**. + +If CI fails, nothing has been published yet. Delete the draft if there is one, +delete the tag (`git push --delete origin 3.2.0` and `git tag -d 3.2.0`), fix +the problem, and tag again. diff --git a/README.md b/README.md index e6782e61..1821feef 100644 --- a/README.md +++ b/README.md @@ -11,30 +11,49 @@ -Version: 3.1.0 - *MATLAB is a registered trademark of The MathWorks, Inc.* ## Features - Convert native MATLAB and GNU Octave figures to interactive [Plotly](https://plotly.com) charts with a single line of code -- Fully offline: charts render locally in your browser — plotly.js is downloaded once during setup and cached on your machine +- Fully offline: charts render locally in your browser — plotly.js is downloaded once after installing and cached on your machine - Publication-quality static export (PNG, JPEG, PDF, SVG) via the Kaleido runtime (downloaded once during setup) - Comparison gallery generator (`makegallery`) showing native and converted figures side by side ## Install -Download the latest version of the wrapper: +### MATLAB (R2021a or later) + +1. Download `plotly_matlab-.mltbx` from the [latest release](https://github.com/plotly/plotly_matlab/releases/latest). +2. In MATLAB's file browser, double-click the file and follow the installer, or run: + + ```matlab + matlab.addons.install('plotly_matlab-.mltbx') + ``` + + The toolbox installs into your Add-Ons folder, and MATLAB adds it to the path. +3. Download plotly.js (needs an internet connection the first time): -- [github.com/plotly/plotly_matlab/archive/master.zip](https://github.com/plotly/plotly_matlab/archive/master.zip) + ```matlab + getplotlyoffline() + ``` -Then, from the extracted folder, run: +### GNU Octave, or MATLAB from source + +Download the [source](https://github.com/plotly/plotly_matlab/archive/master.zip) (or clone the repository), then run from the extracted folder: ```matlab -plotlysetup_offline() % one-time setup: downloads and caches plotly.js for offline usage (requires internet once) +plotlysetup_offline() % adds plotly to the path and downloads plotly.js (requires internet once) ``` -This works in both MATLAB and GNU Octave. +This adds plotly to the path for the current session and prints the `addpath` line to add to your `~/.octaverc` (Octave) or `startup.m` (MATLAB) to keep it there. + +### Uninstall + +- Toolbox: `matlab.addons.uninstall('Plotly Graphing Library for MATLAB')` +- Source: delete the folder and the `addpath` line from your `~/.octaverc` or `startup.m` + +The plotly.js bundle and any saved Plotly credentials are kept in a `.plotly` folder in your home folder (`%APPDATA%\.plotly` on Windows); delete it to remove them too. ## Usage @@ -80,7 +99,7 @@ saveplotlyfig(p, 'testimage.svg') % PNG, JPEG, PDF, and SVG supported Offline usage is the default and requires nothing. For online features — storing charts on the Plotly cloud, retrieving figures with `getplotlyfig`, and streaming — configure your Plotly credentials: ```matlab -plotlysetup_online('your_username', 'your_api_key') +saveplotlycredentials('your_username', 'your_api_key') ``` ## Retrieving figures diff --git a/buildfile.m b/buildfile.m new file mode 100644 index 00000000..114a0a6e --- /dev/null +++ b/buildfile.m @@ -0,0 +1,60 @@ +function plan = buildfile + plan = buildplan(localfunctions); +end + +function packageTask(~) + % Package the plotly folder as a toolbox in release/ + root = fileparts(mfilename('fullpath')); + + % only tracked files: a working copy also holds downloads such as the + % Kaleido binaries under plotly/kaleido; core.quotePath=false keeps + % names with non-ASCII characters unquoted + [status, listing] = system(sprintf('git -C "%s" -c core.quotePath=false ls-files plotly', root)); + if status ~= 0 + error('buildfile:git', 'git ls-files failed:\n%s', listing); + end + files = string(splitlines(strtrim(listing))); + missing = files(~isfile(root + "/" + files)); + if ~isempty(missing) + error('buildfile:missingFiles', 'git ls-files listed files that do not exist:\n%s', ... + strjoin(missing, newline)); + end + files = files(~startsWith(files, "plotly/testing/") & ~contains(files, "/Test_")); + folders = unique(regexprep(files(endsWith(files, ".m")), '/[^/]*$', '')); + + version = regexp(fileread(fullfile(root, 'plotly', 'plotly_aux', 'plotly_version.m')), ... + '"(\d+\.\d+\.\d+)"', 'tokens', 'once'); + if isempty(version) + error('buildfile:version', ... + 'could not read a version like "1.2.3" from plotly/plotly_aux/plotly_version.m'); + end + version = string(version{1}); + + % ship the license at the toolbox root; the copy only lives for the build + licenseCopy = fullfile(root, 'plotly', 'LICENSE'); + copyfile(fullfile(root, 'LICENSE'), licenseCopy); + removeLicense = onCleanup(@() delete(licenseCopy)); %#ok + + opts = matlab.addons.toolbox.ToolboxOptions(fullfile(root, 'plotly'), ... + "e59f316c-4a80-11e4-9553-005056977bd0"); + opts.ToolboxName = "Plotly Graphing Library for MATLAB"; + opts.ToolboxVersion = version; + opts.Summary = "Convert MATLAB figures into interactive plotly.js charts"; + if isprop(opts, 'Description') % removed in R2026b + opts.Description = "fig2plotly converts MATLAB figures into interactive " + ... + "plotly.js web charts that open in your browser, fully offline. " + ... + "Run getplotlyoffline() once after installing to download plotly.js."; + end + opts.AuthorCompany = "Plotly"; + opts.ToolboxFiles = [root + "/" + files; string(licenseCopy)]; + opts.ToolboxMatlabPath = root + "/" + folders; + opts.MinimumMatlabRelease = "R2021a"; + opts.MaximumMatlabRelease = ""; + opts.OutputFile = fullfile(root, "release", "plotly_matlab-" + version + ".mltbx"); + + if ~isfolder(fullfile(root, "release")) + mkdir(fullfile(root, "release")); + end + fprintf('Packaging %d files into %s\n', numel(opts.ToolboxFiles), opts.OutputFile); + matlab.addons.toolbox.packageToolbox(opts); +end diff --git a/plotly/Test_plotlysetup.m b/plotly/Test_plotlysetup.m index 434efa49..2c4929dc 100644 --- a/plotly/Test_plotlysetup.m +++ b/plotly/Test_plotlysetup.m @@ -35,6 +35,23 @@ function testPrintPlotlyPathHint(tc) tc.verifyTrue(~isempty(strfind(output, ... 'addpath(genpath(''/opt/it''''s here/plotly''));')), output); end + + function testGetPlotlyOfflineDownloadsBundle(tc) + if isempty(getenv('CI')) + return % downloads plotly.js from the CDN + end + sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok + if ~strcmp(getuserdir(), getenv('HOME')) + return % getuserdir() is outside the sandbox, so this would replace the real bundle. + end + + evalc('getplotlyoffline()'); + + bundle = fileread(fullfile(getenv('HOME'), '.plotly', 'plotlyjs', ... + 'plotly-matlab-offline-bundle.js')); + tc.verifyTrue(~isempty(strfind(bundle(1:min(end, 100)), 'plotly.js v2.35.3')), ... + 'the saved bundle does not start with the plotly.js 2.35.3 header'); + end end methods (Static) diff --git a/plotly/addtheme.m b/plotly/addtheme.m index e7aed3b3..2e9f4f47 100644 --- a/plotly/addtheme.m +++ b/plotly/addtheme.m @@ -1,35 +1,20 @@ function f = addtheme(f, theme) %-validate theme name-% - themePath = 'plotly/themes'; - S = dir(themePath); - - if isempty(S) - paths = split(path, ':'); - for p = 1:length(paths) - if ~isempty(strfind(paths{p}, themePath)) - themePath = paths{p}; - break; - end - end - S = dir(themePath); - end + themePath = fullfile(fileparts(mfilename('fullpath')), 'themes'); + S = dir(fullfile(themePath, '*.json')); N = {S.name}; - if ~any(strcmp(N,strcat(theme, '.json'))) == 1 - ME = MException('MyComponent:noSuchVariable', ... - [strcat('\n', theme, ... - ' is not a supported themes.'), ... - ' Please choose one of these theme names:\n\n', ... - strrep(strrep([S.name], '...', ''), '.json', ' | ')]); - throw(ME) + if ~any(strcmp(N, strcat(theme, '.json'))) + error('addtheme:unknownTheme', ... + '%s is not a supported theme. Choose one of: %s', ... + theme, strjoin(strrep(N, '.json', ''), ' | ')); end %-add theme to figure-% - fname = sprintf('%s/%s.json', themePath, theme); - % fname = strcat('plotly/themes/', theme, '.json'); + fname = fullfile(themePath, sprintf('%s.json', theme)); fid = fopen(fname); raw = fread(fid,inf); str = char(raw'); @@ -37,14 +22,4 @@ theme_template = jsondecode(str); f.layout.template = theme_template; - - if isfield(f.layout.template.layout, 'paper_bgcolor') - disp(strcat('layout.bg_paper:::',... - f.layout.template.layout.paper_bgcolor)) - end - - if isfield(f.layout.template.layout, 'plot_bgcolor') - disp(strcat('layout.plot_bgcolor:::',... - f.layout.template.layout.plot_bgcolor)) - end end diff --git a/plotly/plotly_aux/plotly_version.m b/plotly/plotly_aux/plotly_version.m index f56f5497..fa4ee25f 100644 --- a/plotly/plotly_aux/plotly_version.m +++ b/plotly/plotly_aux/plotly_version.m @@ -1,3 +1,3 @@ function version = plotly_version() - version = "3.0.0"; + version = "3.1.0"; end diff --git a/plotly/plotly_offline_aux/getplotlyoffline.m b/plotly/plotly_offline_aux/getplotlyoffline.m index e4ac2816..800d2e67 100644 --- a/plotly/plotly_offline_aux/getplotlyoffline.m +++ b/plotly/plotly_offline_aux/getplotlyoffline.m @@ -1,7 +1,17 @@ function getplotlyoffline(plotly_bundle_url) + % GETPLOTLYOFFLINE(url) downloads the plotly.js bundle used for offline + % plots; without url it downloads plotly.js 2.35.3 + if nargin < 1 + plotly_bundle_url = 'https://cdn.plot.ly/plotly-2.35.3.min.js'; + end + try - % download bundle - plotly_bundle = webread(plotly_bundle_url); + % download bundle; Octave 11.1's webread errors even on plain web pages + if is_octave() + plotly_bundle = urlread(plotly_bundle_url); + else + plotly_bundle = webread(plotly_bundle_url); + end catch exception disp("Whoops! There was an error attempting to download the MATLAB offline Plotly bundle"); rethrow(exception); @@ -29,7 +39,7 @@ function getplotlyoffline(plotly_bundle_url) % success! fprintf(['\nSuccess! You can now generate offline ', ... 'graphs.\nTo generate online graphs, run ', ... - 'plotlysetup_online(username, api_key) ', ... + 'saveplotlycredentials(username, api_key) ', ... '\nand use the ''offline'' flag of fig2plotly as ', ... 'follows:\n\n>> plot(1:10); fig2plotly(gcf, ', ... '''offline'', false);\n\n']) diff --git a/plotlysetup_offline.m b/plotlysetup_offline.m index 2ef9d18f..085311a9 100644 --- a/plotlysetup_offline.m +++ b/plotlysetup_offline.m @@ -1,7 +1,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. + % If no argument is provided, getplotlyoffline's default bundle is used. % [1] adds plotly api to search path and prints how to keep it there %DEFAULT OUTPUT @@ -9,9 +9,7 @@ function plotlysetup_offline(plotly_bundle_url, varargin) exception.identifier = ''; try %check number of inputs - if nargin == 0 - plotly_bundle_url = 'https://cdn.plot.ly/plotly-2.35.3.min.js'; - elseif nargin>1 + if nargin>1 error('plotly:wrongInput',.... ['\n\nWhoops! Wrong number of inputs. Please run >> help plotlysetup_offline \n',... 'for more information regarding the setup your Plotly API MATLAB \n',... @@ -43,7 +41,11 @@ function plotlysetup_offline(plotly_bundle_url, varargin) %get offline bundle fprintf('\nNow downloading the plotly offline bundle ...'); - getplotlyoffline(plotly_bundle_url); + if nargin == 0 + getplotlyoffline(); + else + getplotlyoffline(plotly_bundle_url); + end %greet the people! fprintf('\nWelcome to Plotly! If you are new to Plotly please enter: >> plotlyhelp to get started!\n\n')