{ "cells": [ { "cell_type": "markdown", "id": "a7d09bc1-be39-44ba-8cdb-26825d7a79d4", "metadata": {}, "source": [ "# Testing `%wasm`\n", "\n", "### Test environment variables are set" ] }, { "cell_type": "code", "execution_count": 1, "id": "35122e7f-3a2a-4d04-a16a-6a99e6814b99", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/janpf\n", "/home/janpf/jupyter_files/eb626c95\n", "/files/jupyter_files/eb626c95\n" ] } ], "source": [ "%wasm\n", "!echo $GONB_JUPYTER_ROOT\n", "!echo $GONB_WASM_DIR\n", "!echo $GONB_WASM_URL\n", "%goflags --cover --covermode=set" ] }, { "cell_type": "code", "execution_count": 2, "id": "65876109-d2c7-4f71-8c8a-bcf2e2ee674b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/janpf/jupyter_files/eb626c95\n" ] } ], "source": [ "!echo $GONB_WASM_DIR" ] }, { "cell_type": "markdown", "id": "a42ed635-671f-4b7a-9a7f-899ba061bc9f", "metadata": {}, "source": [ "### Tests `github.com/janpfeifer/gonb/gonbui/wasm` and that the `Gonb` variables are created.\n", "\n", "We want to first make sure we are using the `gonbui/wasm` that we want to test (and not downloading\n", "the latest release from the web)." ] }, { "cell_type": "code", "execution_count": 4, "id": "d6a9b79e-dc28-4b4b-90c5-44a98ffb3fc4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ok\n" ] } ], "source": [ "!if [[ \"${GONB_GIT_ROOT}\" == \"\" ]] ; then \\\n", " echo \"Please set GONB_GIT_ROOT before runnig this notebook!\" 1>&2 ; \\\n", "else \\\n", " echo \"ok\" ; \\\n", "fi" ] }, { "cell_type": "code", "execution_count": 5, "id": "70e8e7ae-8adb-437e-bcf4-ca2c96558c9c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\t- Added replace rule for module \"github.com/janpfeifer/gonb\" to local directory \"/home/janpf/Projects/gonb\".\n" ] } ], "source": [ "!*rm -f go.work && go work init && go work use . \"${GONB_GIT_ROOT}\"\n", "%goworkfix" ] }, { "cell_type": "markdown", "id": "3f05e500-f83d-4d47-8a13-06cc58ec3aa2", "metadata": {}, "source": [ "Now we are testing that:\n", "\n", "1. `GonbWasmArgs` is properly set and `ParseFlags` works.\n", "2. The cell's `DIV` is created, and that `GonbWasmDivId` correctly points to it." ] }, { "cell_type": "code", "execution_count": 6, "id": "8eba1e4b-50f3-4a58-840e-ec76efec2b07", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/javascript": [ "\n", "(() => {\n", "\tvar go_eb626c95 = new globalThis.Go();\n", "\tgo_eb626c95.argv = [\"js\"].concat([\"--x=42\", ]);\n", "\tconsole.log(\"argv=\"+go_eb626c95.argv);\n", "\tWebAssembly.instantiateStreaming(fetch(\"/files/jupyter_files/eb626c95/gonb_cell.wasm\"), go_eb626c95.importObject).\n", "\t\tthen((result) => { go_eb626c95.run(result.instance); });\n", "})();\n" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%wasm\n", "import (\n", " \"flag\"\n", " \"github.com/janpfeifer/gonb/gonbui/wasm\"\n", ")\n", "\n", "var flagX = flag.String(\"x\", \"\", \"the meaning of life, the universe, and everything\")\n", "\n", "%% --x=42\n", "flag.Parse()\n", "div := wasm.ById(GonbWasmDivId) // Div created for the wasm program.\n", "var parts []string\n", "parts = append(parts, fmt.Sprintf(\"Wasm File Directory: %s\\n\", GonbWasmDir))\n", "parts = append(parts, fmt.Sprintf(\"Wasm URL: %s\\n\", GonbWasmUrl))\n", "parts = append(parts, fmt.Sprintf(\"Wasm DIV tag id: %s\\n\", GonbWasmDivId))\n", "parts = append(parts, fmt.Sprintf(\"The meaning of life, the universe, and everything: %s\", *flagX))\n", "div.SetInnerText(strings.Join(parts, \"\"))\n" ] }, { "cell_type": "code", "execution_count": 19, "id": "772317b2-8321-43e2-a245-f18106729d2e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* State reset: all memorized declarations discarded.\n" ] } ], "source": [ "%reset" ] }, { "cell_type": "code", "execution_count": 27, "id": "ee22b8b9-1224-4564-ae6d-fa12c40ffdfd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/javascript": [ "\n", "\tvar x = document.getElementById(\"xxx\");\n", "\tx.innerText = \"Hello world!\";\n", "\t" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import (\n", " \"github.com/janpfeifer/gonb/gonbui\"\n", ")\n", "%%\n", "gonbui.DisplayHTML(`
`)\n", "gonbui.ScriptJavascript(`\n", "var x = document.getElementById(\"xxx\");\n", "x.innerText = \"Hello world!\";\n", "`)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Go (gonb)", "language": "go", "name": "gonb" }, "language_info": { "codemirror_mode": "", "file_extension": ".go", "mimetype": "", "name": "go", "nbconvert_exporter": "", "pygments_lexer": "", "version": "go1.21.0" } }, "nbformat": 4, "nbformat_minor": 5 }