{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "gonb", "display_name": "Go (gonb)" } }, "cells": [ { "cell_type": "markdown", "source": [ "# GoNB - A Go Notebook Kernel for Jupyter\n", "\n", "See the [tutorial in github](https://github.com/janpfeifer/gonb/blob/e15ac2e8e3fe/examples/tutorial.ipynb). The repository is in [github.com/janpfeifer/gonb](https://github.com/janpfeifer/gonb).\n", "\n", "\n", "## Installation in Google's Colab\n", "\n", "1. Run the installation cell just below once only. It will install Go, GoNB and a couple of Go tools needed to provide auto-complete.\n", "\n", " It takes a couple of minutes ... but only needs to be done once.\n", "\n", " When creating other GoNB notebooks with Google's Colab, you will have to copy the cell below (or do something similar).\n", "\n", "2. Restart the session: under `Runtime` menu, click on `Restart Session`\n", "\n", "3. After it restarts, if you should be able to change the runtim to **GoNB**. Under the `Runtime` menu, click on the entry `Change Runtime Type`. Don't change the hardware accelerator (otherwise you'll have to restart the installation), but change the runtime type to `Go (gonb)`\n", "\n", "4. Restart the session again: under `Runtime` menu, click on `Restart Session`\n", "\n", "**Disclaimer**: this is highly not documented or official, but seems to be supported by Colab (it works). This method has broken before without notice. Please reach out to project in [github.com/janpfeifer/gonb](https://github.com/janpfeifer/gonb) if there are any issues.\n", "\n", "\n" ], "metadata": { "id": "FWzjioUI63tT" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "3PaXSu67xkrg", "outputId": "bc7a90b5-cd54-4091-8c26-d69212559a13", "colab": { "base_uri": "https://localhost:8080/" }, "cellView": "form" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Installing go ...env: GOROOT=/content/go\n", " done.\n", "go version go1.23.1 linux/amd64\n", "Installing gonb ... done.\n", "Installing goimports ... done.\n", "Installing gopls ... done.\n", "I0923 20:27:38.817564 7307 install.go:121] \u001b[7;39;32m[a14e2312]\u001b[0m Go (gonb) kernel configuration installed in \"/root/.local/share/jupyter/kernels/gonb/kernel.json\".\n", "Done!\n" ] } ], "source": [ "#@title Install Go, `goimports` and GoNB code.\n", "\n", "# Install Go and goimports.\n", "!echo -n \"Installing go ...\"\n", "!mkdir -p cache\n", "!wget -q -O cache/go.tar.gz 'https://go.dev/dl/go1.23.1.linux-amd64.tar.gz'\n", "!tar xzf cache/go.tar.gz\n", "%env GOROOT=/content/go\n", "!ln -sf \"/content/go/bin/go\" /usr/bin/go\n", "!echo \" done.\"\n", "!go version\n", "\n", "# Install gonb, goimports, gopls.\n", "!echo -n \"Installing gonb ...\"\n", "!go install github.com/janpfeifer/gonb@latest >& /tmp/output || cat /tmp/output\n", "!echo \" done.\"\n", "!ln -sf /root/go/bin/gonb /usr/bin/gonb\n", "\n", "!echo -n \"Installing goimports ...\"\n", "!go install golang.org/x/tools/cmd/goimports@latest >& /tmp/output || cat /tmp/output\n", "!echo \" done.\"\n", "!ln -sf /root/go/bin/goimports /usr/bin/goimports\n", "\n", "!echo -n \"Installing gopls ...\"\n", "!go install golang.org/x/tools/gopls@latest >& /tmp/output || cat /tmp/output\n", "!echo \" done.\"\n", "!ln -sf /root/go/bin/gopls /usr/bin/gopls\n", "\n", "# Install gonb kernel configuration.\n", "!gonb --install --logtostderr\n", "!echo \"Done!\"" ] }, { "cell_type": "markdown", "source": [ "The cell above shoudl be run only once, and then followed by a change of runtime to `Go (gonb)`. See detailed instructions above." ], "metadata": { "id": "lbPHZ_rjOavo" } }, { "cell_type": "markdown", "source": [ "## Demo\n", "\n", "From here below you can delete and put your own code.\n", "\n", "\n", "First ..." ], "metadata": { "id": "izIU9VTs-Fc4" } }, { "cell_type": "code", "source": [ "%%\n", "fmt.Printf(\"Hello World!\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "XH7zQH3D7TmR", "outputId": "362d4597-e91d-491f-c72a-fc08e3685abf" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Hello World!" ] } ] }, { "cell_type": "code", "source": [ "import \"bytes\"\n", "import svgo \"github.com/ajstarks/svgo\"\n", "import \"github.com/janpfeifer/gonb/gonbui\"\n", "\n", "func Shining(width, height int) string {\n", " buf := bytes.NewBuffer(nil)\n", " canvas := svgo.New(buf)\n", " xp := []int{50, 70, 70, 50, 30, 30}\n", " yp := []int{40, 50, 75, 85, 75, 50}\n", " xl := []int{0, 0, 50, 100, 100}\n", " yl := []int{100, 40, 10, 40, 100}\n", " bgcolor := \"rgb(227,78,25)\"\n", " bkcolor := \"rgb(153,29,40)\"\n", " stcolor := \"rgb(65,52,44)\"\n", " stwidth := 12\n", " stylefmt := \"stroke:%s;stroke-width:%d;fill:%s\"\n", " canvas.Start(width, height)\n", " canvas.Def()\n", " canvas.Gid(\"unit\")\n", " canvas.Polyline(xl, yl, \"fill:none\")\n", " canvas.Polygon(xp, yp)\n", " canvas.Gend()\n", " canvas.Gid(\"runit\")\n", " canvas.TranslateRotate(150, 180, 180)\n", " canvas.Use(0, 0, \"#unit\")\n", " canvas.Gend()\n", " canvas.Gend()\n", " canvas.DefEnd()\n", " canvas.Rect(0, 0, width, height, \"fill:\"+bgcolor)\n", " canvas.Gstyle(fmt.Sprintf(stylefmt, stcolor, stwidth, bkcolor))\n", " for y := 0; y < height; y += 130 {\n", " for x := -50; x < width; x += 100 {\n", " canvas.Use(x, y, \"#unit\")\n", " canvas.Use(x, y, \"#runit\")\n", " }\n", " }\n", " canvas.Gend()\n", " canvas.End()\n", " return buf.String()\n", "}\n", "\n", "%%\n", "gonbui.DisplaySVG(Shining(500, 500))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 522 }, "id": "nqvhyQ-F_0kA", "outputId": "ca3add57-fee4-48bd-a4db-e725b46a1c83" }, "execution_count": null, "outputs": [ { "output_type": "display_data", "data": { "text/html": [ "