Difference between revisions of "User talk:Virajp"

From ZCubes Wiki
Jump to navigation Jump to search
m
Line 93: Line 93:
 
[[Category:Wolfram Language]]
 
[[Category:Wolfram Language]]
 
[[Category:System integration]]
 
[[Category:System integration]]
 +
 +
=ZAP and Zblack: Local npm Library Integration=
 +
 +
This article demonstrates how ZAP and Zblack enable seamless use of Node.js, npm libraries, and advanced file system/database operations. The workflow allows you to save code, install npm packages, and run scripts interactively, leveraging the full power of server-side JavaScript in your ZAP/Zblack environment.
 +
 +
==ZAP: Using Node.js and npm Libraries Overview==
 +
 +
On ZAP, Node.js is embedded, allowing you to:
 +
 +
Save code files to a folder.
 +
 +
Use npm install to add external npm libraries to that folder.
 +
 +
require and use those libraries in your Node.js scripts.
 +
 +
Access the full file system, databases, and more (unlike browser JS).
 +
 +
==Step 1: Create a .z3 Document and Install npm Libraries==
 +
 +
Open ZAP (ZAP Download), and create a new document. Save this document with the .z3 extension in a directory of your choice (for example, test.z3).
 +
 +
Next, open a terminal in that same directory. For each npm package you want to use, run:
 +
 +
<pre> npm install &lt;library-name&gt; </pre>
 +
This command will create a node_modules folder in your directory and install the specified libraries. For example, to install the underscore library, use:
 +
 +
<pre> npm install underscore </pre>
 +
Your directory will now contain your .z3 file and a node_modules folder with the installed npm packages, ready for use in your Zblack project.
 +
 +
==Step 2: Write Code in the .z3 File Using Installed npm Libraries==
 +
 +
Open your .z3 file in ZAP Document editor
 +
 +
Use require() to import any installed npm libraries and write your Node.js code as needed.
 +
 +
==Step 3: Install Zblack and Set the Path Variable==
 +
 +
Download and install Zblack from the [http://downloads.zcubes.com Zblack].
 +
 +
Add the Zblack executable to your system’s PATH environment variable so you can run it from any terminal window.
 +
 +
==Step 4: Run .z3 Files Using Zblack==
 +
 +
In your terminal, navigate to the directory containing your .z3 file.
 +
 +
Run your script with the command:
 +
 +
<pre>
 +
zblack yourfile.z3
 +
</pre>
 +
To keep the process interactive, add the -i flag:
 +
 +
text
 +
zblack -i yourfile.z3
 +
This process enables you to develop and execute JavaScript scripts with full npm support in the Zblack environment.
 +
 +
==How It Works==
 +
 +
Local npm packages: As shown in the node_modules screenshot, you can install any npm package locally to your project and require it in your scripts.
 +
 +
Server-side features: Unlike browser JavaScript, you can use Node.js modules like fs for file/database access.
 +
 +
Zblack execution: Save your code as .z3 files and run with zblack -i yourfile.z3 for an interactive session.
 +
 +
Flexible scripting: Switch between libraries (lodash, underscore, csv-parse, chance, etc.) as needed for your workflow.
 +
 +
==Examples==
 +
Example 1:
 +
[[File:Temp 3.png|thumb]]
 +
<pre>
 +
const _ = require('lodash');
 +
console.log(_.shuffle([1,2,3,4,5]));
 +
</pre>
 +
 +
Example 2:
 +
[[File:Temp 5.png|thumb]]
 +
<pre>
 +
const _ = require('underscore');
 +
 +
let people = [
 +
  { name: 'Alice', group: 'A' },
 +
  { name: 'Bob', group: 'B' },
 +
  { name: 'Carol', group: 'A' },
 +
  { name: 'Dave', group: 'B' },
 +
  { name: 'Eve', group: 'A' }
 +
];
 +
 +
let groups = _.groupBy(people, 'group');
 +
 +
for (let key in groups) {
 +
  groups[key] = _.shuffle(groups[key]);
 +
}
 +
 +
console.log('Grouped and Shuffled:', groups);
 +
</pre>
 +
 +
Example 3:
 +
[[File:Temp 7.png|thumb]]
 +
<pre>
 +
const Chance = require('chance');
 +
const chance = new Chance();
 +
 +
// Generate an array of 5 random names
 +
let names = Array.from({ length: 5 }, () => chance.name());
 +
 +
// Shuffle the names
 +
let shuffled = chance.shuffle(names);
 +
 +
console.log('Random Names:', names);
 +
console.log('Shuffled Names:', shuffled);
 +
</pre>
 +
 +
Example 4:
 +
[[File:Temp 10.png|thumb]]
 +
[[File:Temp 9.png|thumb]]
 +
<pre>
 +
const fs = require('fs');
 +
const { parse } = require('csv-parse');
 +
 +
fs.createReadStream('sample.csv')
 +
  .pipe(parse({ columns: true, skip_empty_lines: true }))
 +
  .on('data', (row) => {
 +
    console.log(row);
 +
  })
 +
  .on('end', () => {
 +
    console.log('Finished parsing the file');
 +
  })
 +
  .on('error', (err) => {
 +
    console.error('Error:', err.message);
 +
  });
 +
 +
</pre>
 +
 +
==See Also==
 +
*[[ZAP]]
 +
*[[Zblack Example of Omniglot]]

Revision as of 11:28, 9 June 2025

Integration of Wolframscript with ZCubes

Wolfram integration in the ZCubes platform enables users to leverage the computational power of the Wolfram Language (Mathematica) directly within ZCubes' Omniglot environment. This integration allows seamless execution of Wolfram code alongside other client and server languages, supporting ZCubes' vision.


Overview

With the release of ZCubes 4.0, Omniglot capability was introduced, allowing users to write and execute intermixed code in practically any programming language. The integration of Wolfram further extends this functionality, enabling symbolic computation, advanced visualization, and data science features from Wolfram within ZCubes.

Prerequisites

Installation Steps

1. Install Wolfram Language or Mathematica

Download and install your Wolfram product from the Wolfram User Portal. Follow the official installation instructions for your operating system.

2. Install Jupyter and Wolfram Kernel

  1. Install Anaconda or Jupyter Notebook if not already available.
  2. Download and install the Wolfram Jupyter Kernel from [1](https://github.com/WolframResearch/WolframLanguageForJupyter).

Method 1: Using `wolframscript`

On macOS/Unix:

git clone https://github.com/WolframResearch/WolframLanguageForJupyter.git
cd WolframLanguageForJupyter
./configure-jupyter.wls add

On Windows:

  • Download and unzip the repository.
  • Open PowerShell in the unzipped folder and run:
.\configure-jupyter.wls add
  • To verify installation, run:
jupyter kernelspec list

You should see a line like:

wolframlanguage14.2   C:\ProgramData\jupyter\kernels\wolframlanguage14.2

3. Add Wolfram as a Server Language in ZCubes

Use the following Z^3 function within ZCubes code editor to register Wolfram as a server language:

File:SS2
Using ZCubes Adding Server Language Function.
ZADDSERVERLANGUAGE("wolframscript", "wolframlanguage14.2")

The second parameter should match the kernel name as registered in Jupyter Notebook.

4. Configure API and Jupyter Settings in ZCubes

Open the Advanced Settings panel and then click on "Config" in ZCubes to enter your Wolfram and Jupyter configuration.

File:SS1
API and Jupyter configuration panel in ZCube
  • Enter your Wolfram Key and Jupyter URL/port as appropriate.
  • Click "APPLY" and "STORE" to save your settings.

5. Using Wolfram in ZCubes

  • Open ZCubes code editor.
  • Select Wolfram as the Server language for your code cell.
  • Write and execute Wolfram Language code, for example:
Plot3D[Sin[x y], {x, -Pi, Pi}, {y, -Pi, Pi}]
  • Results, including graphics and symbolic outputs, will appear inline.
File:SS3
Wolfram 3D plot rendered within ZCubes

Troubleshooting

  • Ensure the Wolfram kernel is correctly installed and registered with Jupyter.
  • Restart Jupyter Notebook or ZCubes if the kernel does not appear.
  • If you encounter a "403 Forbidden" error when accessing Jupyter Notebook:
    • Double-check the URL or IP address for typos.
    • Clear your browser cookies and cache, as old credentials or sessions may cause access issues.
    • Review server configuration files (such as `.htaccess` or Jupyter config) for access restrictions, and ensure XSRF tokens are handled if required.
  • Consult Wolfram and Jupyter documentation for further installation or activation issues.

See also

ZAP and Zblack: Local npm Library Integration

This article demonstrates how ZAP and Zblack enable seamless use of Node.js, npm libraries, and advanced file system/database operations. The workflow allows you to save code, install npm packages, and run scripts interactively, leveraging the full power of server-side JavaScript in your ZAP/Zblack environment.

ZAP: Using Node.js and npm Libraries Overview

On ZAP, Node.js is embedded, allowing you to:

Save code files to a folder.

Use npm install to add external npm libraries to that folder.

require and use those libraries in your Node.js scripts.

Access the full file system, databases, and more (unlike browser JS).

Step 1: Create a .z3 Document and Install npm Libraries

Open ZAP (ZAP Download), and create a new document. Save this document with the .z3 extension in a directory of your choice (for example, test.z3).

Next, open a terminal in that same directory. For each npm package you want to use, run:

 npm install <library-name> 

This command will create a node_modules folder in your directory and install the specified libraries. For example, to install the underscore library, use:

 npm install underscore 

Your directory will now contain your .z3 file and a node_modules folder with the installed npm packages, ready for use in your Zblack project.

Step 2: Write Code in the .z3 File Using Installed npm Libraries

Open your .z3 file in ZAP Document editor

Use require() to import any installed npm libraries and write your Node.js code as needed.

Step 3: Install Zblack and Set the Path Variable

Download and install Zblack from the Zblack.

Add the Zblack executable to your system’s PATH environment variable so you can run it from any terminal window.

Step 4: Run .z3 Files Using Zblack

In your terminal, navigate to the directory containing your .z3 file.

Run your script with the command:

zblack yourfile.z3

To keep the process interactive, add the -i flag:

text zblack -i yourfile.z3 This process enables you to develop and execute JavaScript scripts with full npm support in the Zblack environment.

How It Works

Local npm packages: As shown in the node_modules screenshot, you can install any npm package locally to your project and require it in your scripts.

Server-side features: Unlike browser JavaScript, you can use Node.js modules like fs for file/database access.

Zblack execution: Save your code as .z3 files and run with zblack -i yourfile.z3 for an interactive session.

Flexible scripting: Switch between libraries (lodash, underscore, csv-parse, chance, etc.) as needed for your workflow.

Examples

Example 1:

Temp 3.png
const _ = require('lodash');
console.log(_.shuffle([1,2,3,4,5]));

Example 2:

Temp 5.png
const _ = require('underscore');

let people = [
  { name: 'Alice', group: 'A' },
  { name: 'Bob', group: 'B' },
  { name: 'Carol', group: 'A' },
  { name: 'Dave', group: 'B' },
  { name: 'Eve', group: 'A' }
];

let groups = _.groupBy(people, 'group');

for (let key in groups) {
  groups[key] = _.shuffle(groups[key]);
}

console.log('Grouped and Shuffled:', groups);

Example 3:

Temp 7.png
const Chance = require('chance');
const chance = new Chance();

// Generate an array of 5 random names
let names = Array.from({ length: 5 }, () => chance.name());

// Shuffle the names
let shuffled = chance.shuffle(names);

console.log('Random Names:', names);
console.log('Shuffled Names:', shuffled);

Example 4:

Temp 10.png
Temp 9.png
const fs = require('fs');
const { parse } = require('csv-parse');

fs.createReadStream('sample.csv')
  .pipe(parse({ columns: true, skip_empty_lines: true }))
  .on('data', (row) => {
    console.log(row);
  })
  .on('end', () => {
    console.log('Finished parsing the file');
  })
  .on('error', (err) => {
    console.error('Error:', err.message);
  });

See Also