Skip to main content

Python package to run JavaScript/TypeScript(Deno backend) in Jupyter notebooks and Google Colab.

Project description

Deno Magic Command

This fork simplifies the magic command; instead of %%run_deno, we change it to %%d. And also translate all Japanese to English.

Overview

This is a magic command to write and execute Deno (JavaScript/TypeScript) code in code cells in Jupyter (notebook/lab) or Google Colab.

Usage

Installation

In Jupyter environments, you need to install Deno in advance and add it to your PATH. For installation methods, please refer to Deno's official website. If you're using Jupyter (extension) via VSCode, additionally install the Deno extension and set Deno.enable to true in the settings.

In the Google Colab environment, there's an installation function provided in the package, so you don't need to install it separately.

Adding the Magic Command

Paste the following code into a code cell and execute it to register the magic command. You need to re-run it each time you restart the kernel or runtime.

%pip install denomagics
import denomagics

Install Deno (for Google Colab; calling this in other environments won't install Deno)

denomagics.install_deno_colab()
# Register the magic command
denomagics.register_deno_magics()

How to Use the Magic Command

At the beginning of a code cell, write the magic command as follows. When executed, the JavaScript/TypeScript code in the code cell will be run by Deno.

%%d

console.log("Hello, world!");

or with old syntax

%%run_deno

console.log("Hello, world!");

We also provide a magic command that transpiles the JavaScript/TypeScript code in the code cell using Deno and runs it inside an iframe.

Below is an example using p5.js, a library for browsers.

%%run_deno_iframe 830 430
import "https://cdn.jsdelivr.net/npm/p5@1.9.4/lib/p5.js";

const sketch = (p: any) => {
  let x = 0;
  let y = 0;
  let speed = 2;
  let color: [number, number, number] = [0, 0, 0];

  p.setup = () => {
    p.createCanvas(800, 400);
    x = p.width / 2;
    y = p.height / 2;
  };

  p.draw = () => {
    p.background(220);
    p.fill(color);
    p.ellipse(x, y, 50, 50);
    if (p.keyIsDown(p.LEFT_ARROW) === true) {
      x -= speed;
    }

    if (p.keyIsDown(p.RIGHT_ARROW) === true) {
      x += speed;
    }

    if (p.keyIsDown(p.UP_ARROW) === true) {
      y -= speed;
    }

    if (p.keyIsDown(p.DOWN_ARROW) === true) {
      y += speed;
    }
  };

  p.mousePressed = () => {
    color = [p.random(255), p.random(255), p.random(255)];
  };
};

new p5(sketch);

Magic Command

%%d

%%run_deno [userval]

Executes the JavaScript/TypeScript code in the code cell using Deno.

  • userval: Specifies whether to use Jupyter user variables in Deno. The default is False.

When userval is set to True, you can access Jupyter's user variables through globalThis.jupyter, allowing variable exchange across cells.
Internally, data is exchanged between Jupyter and Deno using a temporary JSON file, so objects that cannot be converted to JSON cannot be used.
Behavior is undefined if such objects are used.

If the code is being executed within a Jupyter code cell, globalThis.isJupyterCell is defined. By checking whether this is not undefined, you can determine if the code is being executed from a Jupyter code cell.

If you want to terminate the execution in the middle of a code cell while using Jupyter user variables, use jupyterExit() instead of Deno.exit. If you exit with Deno.exit, Jupyter's user variables will not be updated.

%%run_deno_iframe

Transpiles the JavaScript/TypeScript in the code cell using Deno and runs it inside an iframe.

%%run_deno_iframe [width] [height] [srcs]
  • width: Specifies the width of the iframe. The default is 500.
  • height: Specifies the height of the iframe. The default is 500.
  • srcs: Specifies the URLs of external JavaScript files. If specifying multiple URLs, separate them with spaces.

%%run_deno_bundle_iframe

Transpiles and bundles the JavaScript/TypeScript in the code cell, including the imported code, and runs it inside an iframe.

The arguments are the same as %%denoiframe.

%%view_deno_iframe

Outputs the HTML generated after transpiling the JavaScript/TypeScript in the code cell using Deno.

The arguments are the same as %%run_deno_iframe.

%%view_deno_bundle_iframe

Outputs the HTML generated after transpiling and bundling the JavaScript/TypeScript in the code cell, including the imported code.

The arguments are the same as %%run_deno_iframe.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

denomagics-1.1.10.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

denomagics-1.1.10-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file denomagics-1.1.10.tar.gz.

File metadata

  • Download URL: denomagics-1.1.10.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for denomagics-1.1.10.tar.gz
Algorithm Hash digest
SHA256 cbb683d5d74a1f6971f0588373379f1ab4732f5a78a298304df1db2540f51dbd
MD5 316ede80a5b2f9b191ca5580a54375b6
BLAKE2b-256 cffee4f0b96c44c3b0d22d37f90ce9cfcacd5da72cd83d79d2667b28036a6b37

See more details on using hashes here.

File details

Details for the file denomagics-1.1.10-py3-none-any.whl.

File metadata

  • Download URL: denomagics-1.1.10-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for denomagics-1.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 d00e4fe6bd09287c8ab9caa35e622383c8cdf9b9621e61e752e57a91aee7e419
MD5 f73c1aeb3ffe7f94237009c506d658bf
BLAKE2b-256 0b83ac0bd0530462c4e8c307789497c1ce73e769eb7d306ef47629e941459cd4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page