3.1 KiB
title | pagination_prev | pagination_next |
---|---|---|
Mathematica | demos/cloud/index | demos/bigdata/index |
import current from '/version.js';
:::note
This demo was last tested in 2023 April 22 in Mathematica 13.2.1
:::
The "NodeJS" instructions describe installation steps for NodeJS projects. Mathematica has built-in features for external scripting with NodeJS. Helper functions can translate between CSV text and Mathematica datasets or arrays.
Mathematica can also use command-line tools
Integration Details
:::caution
Mathematica includes ExternalEvaluate
for running scripts in an external
engine. In local testing, there were incompatibilities with recent NodeJS
versions. This demo uses the shell integration to call a command-line tool.
:::
Command-Line Tools
ExternalEvaluate
can run command-line tools and capture standard output:
cmd = "/usr/local/bin/xlsx-cli ~/Downloads/pres.numbers"
csvdata = ExternalEvaluate["Shell" -> "StandardOutput", cmd];
Once evaluated, ImportString
can interpret the data as a dataset. Typically
the first row of the CSV output is the header row. The HeaderLines
option
controls how Mathematica parses the data:
data = ImportString[csvdata, "Dataset", "HeaderLines" -> 1]
Complete Demo
:::note
This demo was tested in macOS. The path names will differ in other platforms.
:::
- Create the standalone
xlsx-cli
binary:
{`\
cd /tmp
npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz exit-on-epipe commander@2
curl -LO https://docs.sheetjs.com/cli/xlsx-cli.js
npx nexe -t 14.15.3 xlsx-cli.js`}
This is discussed in "Command-line Tools"
- Move the generated
xlsx-cli
to a fixed location in/usr/local/bin
:
mkdir -p /usr/local/bin
mv xlsx-cli /usr/local/bin/
Reading a Local File
- In a new Mathematica notebook, run the following snippet:
SheetJSImportFile[x_] := ImportString[Block[{Print}, ExternalEvaluate[
"Shell" -> "StandardOutput",
"/usr/local/bin/xlsx-cli " <> x
]], "Dataset", "HeaderLines" -> 1]
-
Download https://sheetjs.com/pres.numbers and save to Downloads folder.
-
In the Mathematica notebook, run the new function. If the file was saved to the Downloads folder, the path will be
"~/Downloads/pres.numbers"
in macOS:
data = SheetJSImportFile["~/Downloads/pres.numbers"]
The result should be displayed in a concise table.
Reading from a URL
FetchURL
downloads a file from a specified URL. This function will be wrapped
in a new function called SheetJSImportURL
.
- In the same notebook, run the following:
Needs["Utilities`URLTools`"];
SheetJSImportURL[x_] := Module[{path},(
path = FetchURL["https://sheetjs.com/pres.numbers"];
SheetJSImportFile[path]
)];
- Test by downloading the test file in the notebook:
data = SheetJSImportURL["https://sheetjs.com/pres.numbers"]