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 .

In [10]:
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));
  });
}
Out[10]:
In [2]:
// create a Promise object 
const p = loadData()
Out[2]:
In [3]:
p.then(data => console.log(data));
Out[3]:
Promise { undefined }
{
  status: "ok",
  code: 200,
  messages: [],
  result: { test: { working: true } }
}

Synthetic Data

In [4]:
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);
Out[4]:
123
In [5]:
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))
Out[5]:
itemId,item,description,price,purchused
bb463b8b-b76c-4f6a-a972-665ab5730b69,Generic Wooden Table,Intelligent,84.00,Mon Mar 20 2023 15:38:48 GMT+0000 (Greenwich Mean Time)
b86a4d7b-b95b-465a-8553-34fac1c60627,Modern Steel Gloves,Refined,427.00,Fri Dec 30 2022 18:43:10 GMT+0000 (Greenwich Mean Time)
ebf88799-1457-46ad-ae41-77f3869c17dc,Incredible Granite Table,Fantastic,10.00,Sun Jun 11 2023 16:56:19 GMT+0100 (Irish Standard Time)
56bade48-7a49-4d94-aa8d-215ce3237931,Refined Bronze Tuna,Unbranded,724.00,Sun Oct 02 2022 19:03:26 GMT+0100 (Irish Standard Time)
07948824-2cb1-458b-9e83-65eddb560346,Bespoke Metal Towels,Tasty,5.00,Tue May 23 2023 19:13:21 GMT+0100 (Irish Standard Time)
ac090be2-46c3-455d-b80a-8132e57a3d48,Luxurious Granite Shirt,Handcrafted,317.00,Fri Dec 09 2022 08:05:14 GMT+0000 (Greenwich Mean Time)
5d26d057-8899-4840-9efa-e5384784e4f9,Handmade Granite Hat,Licensed,353.00,Tue May 09 2023 18:21:18 GMT+0100 (Irish Standard Time)
abd229fd-5fd1-4160-83e7-e83104de3907,Oriental Cotton Fish,Sleek,49.00,Mon May 01 2023 13:54:40 GMT+0100 (Irish Standard Time)
b4d62ccc-4a4f-4a61-83a7-e8b67270c994,Refined Granite Soap,Licensed,478.00,Tue May 30 2023 16:20:07 GMT+0100 (Irish Standard Time)
2e0c295c-71ad-43d9-9ee2-1172b616d861,Intelligent Cotton Bacon,Fantastic,468.00,Sat Oct 29 2022 03:11:23 GMT+0100 (Irish Standard Time)

Text Processing with AWK

In [6]:
const cmd = Deno.run({
      cmd: ["awk", `BEGIN {
      FS = ","
      print "Product, Price"
      }
      NR > 1 {
                print $2  ", \u20ac" $4
            }`
           ],
      stdin: "piped",
      stdout: "piped",
      stderr: "piped"
});
 
In [7]:
const input = new TextEncoder().encode(serializeArrayToCsv(ITEMS));
await Deno.writeAll(cmd.stdin, input);
cmd.stdin.close();
Out[7]:
In [8]:
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);
Out[8]:
Product, Price
Generic Wooden Table, €84.00
Modern Steel Gloves, €427.00
Incredible Granite Table, €10.00
Refined Bronze Tuna, €724.00
Bespoke Metal Towels, €5.00
Luxurious Granite Shirt, €317.00
Handmade Granite Hat, €353.00
Oriental Cotton Fish, €49.00
Refined Granite Soap, €478.00
Intelligent Cotton Bacon, €468.00
 

Python

In [ ]:
Deno.env.set
("DENO_PYTHON_PATH", "C:\\Users\\Seve\\appdata\\local\\programs\\python\\python310\\python310.dll"
);
console.log(Deno.env.get("DENO_PYTHON_PATH"));
In [ ]:
const env = Deno.env.toObject();
// console.log("env:", env);
In [ ]:
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)
In [ ]: