DenoJupyterNotebook
Deno Kernel for jupyter notebook¶
By combining the power of Deno’s modern JavaScript runtime with Jupyter Notebook’s interactive environment, you can explore and experiment with awk text processing on JavaScript-generated synthetic data.
A Deno JavaScript kernel is a runtime environment that enables the execution of JavaScript code within a Jupyter Notebook. It brings modern JavaScript features, such as TypeScript, npm, and ES Modules, to the notebook environment. With Deno installed, you can run the Deno Jupyter kernel by executing the following command:
deno jupyter --unstable --install
Javascript Promises¶
JavaScript Promises are a powerful tool for managing asynchronous code in JavaScript. They allow you to write code that can handle asynchronous operations without blocking the main thread of execution.
In Jupyter Notebook, you can use JavaScript Promises within notebook cells to execute asynchronous code. You can create a Promise object using the Promise constructor and then use the then method to handle the result of the Promise. For example, you could use a Promise to load data from an external API and then display the results in another notebook cell .
function loadData() {
return new Promise((resolve, reject) => {
// Load data from an external API
fetch('https://tessarinseve.pythonanywhere.com/apitest')
.then(response => response.json())
.then(data => resolve(data))
.catch(error => reject(error));
});
}
// create a Promise object
const p = loadData()
p.then(data => console.log(data));
Synthetic Data¶
import { faker } from 'npm:@faker-js/faker';
import * as clippy from "https://deno.land/x/clippy/mod.ts";
faker.seed(123);
export function createRandomItem() {
return {
itemId: faker.string.uuid(),
item: faker.commerce.productName(),
description: faker.commerce.productAdjective(),
price: faker.commerce.price(),
purchused: faker.date.past()
};
}
export const ITEMS = Array.from({ length: 10 }, createRandomItem);
function serializeArrayToCsv(array) {
if (!Array.isArray(array) || array.length === 0) {
return '';
}
const keys = Object.keys(array[0]);
const headerRow = keys.join(',');
const bodyRows = array
.map((item) => keys.map((key) => item[key]).join(','))
.join('\n');
return `${headerRow}\n${bodyRows}`;
}
console.log(serializeArrayToCsv(ITEMS))
Text Processing with AWK¶
const cmd = Deno.run({
cmd: ["awk", `BEGIN {
FS = ","
print "Product, Price"
}
NR > 1 {
print $2 ", \u20ac" $4
}`
],
stdin: "piped",
stdout: "piped",
stderr: "piped"
});
const input = new TextEncoder().encode(serializeArrayToCsv(ITEMS));
await Deno.writeAll(cmd.stdin, input);
cmd.stdin.close();
const output = await cmd.output()
const outStr = new TextDecoder().decode(output);
const error = await cmd.stderrOutput();
const errorStr = new TextDecoder().decode(error);
cmd.close(); // Don't forget to close it
// write text to clipboard
await clippy.write_text(outStr);
console.log(outStr, errorStr);
Python¶
Deno.env.set
("DENO_PYTHON_PATH", "C:\\Users\\Seve\\appdata\\local\\programs\\python\\python310\\python310.dll"
);
console.log(Deno.env.get("DENO_PYTHON_PATH"));
const env = Deno.env.toObject();
// console.log("env:", env);
import { python } from "https://deno.land/x/python/mod.ts";
const sys = python.import("sys");
const pd = python.import("pandas");
//positional
const df = pd.read_clipboard(',')
console.log(df.head)