{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# javascript kernel based on xeus\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple code execution\n", "The last line is printed if its an expression, except if the line ends with a semicolon." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "let a = 1 + 1;\n", "let b = a * 2;\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple pretty printing" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "// explicit call of pprint\n", "pprint([{fubar:[]}])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "// implicit printing\n", "[{fubar:[]}]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# display stuff" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "ijs.display.json({fuu:\"baR\"});" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "let svg = ` `\n", "ijs.display.svg(svg);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# run async code" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "const sleep = ms => new Promise(r => setTimeout(r, ms));\n", "console.log(\"pre sleep\");\n", "await sleep(1000);\n", "console.log(\"post sleep\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# auto completion\n", "press tab (ijs.display.svg or ijs.display.html are some of many possible completions)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "ij" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Scope\n", "all toplevel variables (const/let/var) are accessible from other cells." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "var fubar = 42;\n", "let fubar_local=43;\n", "function call_me(arg){\n", " return arg;\n", "}\n", "let [fu,bar] = ['fu','bar'];\n", "const {ping,pong} = {ping:1, pingpong:2, pong:3}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "console.log(fubar);\n", "console.log(fubar_local);\n", "console.log(call_me(42));\n", "console.log(fu,bar);\n", "console.log(ping,pong);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Const reassignment\n", "within a cell, const cannot be reassigned, but it can be reassigned in another cell, or\n", "when the cell is re-executed." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "const some_const_var = 42;\n", "some_const_var" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "const some_const_var = 43;\n", "some_const_var" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "import * as math from \"https://cdn.jsdelivr.net/npm/mathjs@12.3.0/+esm\"\n", "\n", "let d = math.derivative('x^2 + x', 'x');\n", "`${d}`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "const {default: OpenAI} = await import('https://cdn.jsdelivr.net/npm/openai@4.26.0/+esm');\n", "console.log(OpenAI.OpenAI);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "import OpenAI from \"https://cdn.jsdelivr.net/npm/openai@4.26.0/+esm\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "import {default as fubar} from \"https://cdn.jsdelivr.net/npm/openai@4.26.0/+esm\"\n", "new fubar.OpenAI()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "import {\n", " atan2, chain, derivative, e, evaluate, log, pi, pow, round, sqrt\n", "} from 'https://cdn.jsdelivr.net/npm/mathjs@12.3.0/+esm'\n", "\n", "sqrt(16)" ] }, { "cell_type": "markdown", "metadata": { "vscode": { "languageId": "plaintext" } }, "source": [ "# Magic Imports\n", "\n", "To make live easier, we have some magic imports.\n", "To use magic import, they need to be enabled (they are enabled by default)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "ijs.magic_imports.enabled = true" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once enabled, the import sources are automatically transformed to\n", "jsdelivr cdn links." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "// no version\n", "import * as math from 'mathjs'\n", "math.sqrt(9)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [ "// explicit version\n", "import * as math_12_3_0 from 'mathjs@12.3.0'\n", "math_12_3_0.sqrt(9)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python ES6 (xjavascript)", "language": "text/x-javascript", "name": "xjavascript" }, "language_info": { "file_extension": "js", "mimetype": "text/x-javascript", "name": "xjavascript", "version": "ES6" } }, "nbformat": 4, "nbformat_minor": 4 }