{ "cells": [ { "cell_type": "raw", "metadata": {}, "source": [ "---\n", "published: true\n", "layout: post\n", "title: Golang to Jupyter\n", "tags: coding golang jupyter\n", "featured: coding golang python\n", "mast: juypter\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Jupyter Notbooks](http://jupyter.org/) have been a popular technology in the Python data science community for a while now, especially in academics. Jupyter Notebooks are a way to mix inline, executable code with documentation in a presentation format. Best practices in organizing source code are not always the most efficient at communicating it's functionality to a user. \n", "\n", "While the intention of a programming language is the abstraction of computational complexity into a simplified language humans can read and write, they must always weigh toward the efficiency of the primary interpreter, the computer. Jupyter Notebooks are intended to communicate source code to humans first and computers second. We can use Jupyter Notebooks to communicate to humans not only the source but the interpreted result.\n", "\n", "Jupyter can use a growing number of interpreters (called [kernels](https://github.com/jupyter/jupyter/wiki/Jupyter-kernels)) to run and render output, including my favorite, [Golang](https://github.com/gopherdata/gophernotes#examples). \n", "\n", "Although data science expressed in Python is the first and most popular use of Jupyter Notebooks, I see a lot of other beneficial applications, including:\n", "\n", "- Programming tutorials in a variety of languages.\n", "- Coding Best practices and style guides.\n", "- Technical articles and blogs.\n", "- Leveraging static site generators like [Jekyll](https://jekyllrb.com/) and [Hugo](https://gohugo.io/)\n", "\n", "Beyond the ability to execute code in-line, the most useful features to me are exporting these notebooks as Markdown. Site builders like [Jekyll](https://jekyllrb.com/) and [Hugo](https://gohugo.io/) use [Markdown](https://daringfireball.net/projects/markdown/syntax) to generate [beautiful static websites and blogs](https://gohugo.io/showcase/).\n", "\n", "![Notebook Screenshot](https://mk.imti.co/images/content/GolangToJupyter-screen.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example\n", "\n", "In the sections below I demonstrate simple HTTP client call in Golang with Golang code executed directly in the Jupyter Notebook.\n", "\n", "First I'll set a variable using Golang's implicit assignment operator \":=\"." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "post := \"2\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, I'll use the new **post** variable in the call below." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Getting Post #2\n", "Raw JSON: {\n", " \"userId\": 1,\n", " \"id\": 2,\n", " \"title\": \"qui est esse\",\n", " \"body\": \"est rerum tempore vitae\\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\\nqui aperiam non debitis possimus qui neque nisi nulla\"\n", "}\n" ] }, { "data": { "text/plain": [ "289 " ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import (\n", " \"net/http\"\n", " \"io/ioutil\"\n", " \"fmt\"\n", ")\n", "\n", "// append the post variable assigned earlier in the notebook\n", "// try changing post := \"2\" to post := \"1\" or overwriting it\n", "// remember to re-execute the code block after any changes.\n", "resp, err := http.Get(\"https://jsonplaceholder.typicode.com/posts/\" + post)\n", "if err != nil {\n", " panic(err)\n", "}\n", "\n", "body, err := ioutil.ReadAll(resp.Body)\n", "if err != nil {\n", " panic(err)\n", "}\n", "\n", "fmt.Printf(\"Getting Post #%s\\n\", post)\n", "fmt.Printf(\"Raw JSON: %s\\n\", body)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you are reading this article on [https://mk.imti.co](https://mk.imti.co) then what you see here was exported from Jupyter Notebooks on my Mac in the Markdown format and saved to Github repository for this site. [https://mk.imti.co](https://mk.imti.co) is a static website [automatically generated by Github](https://pages.github.com/) using [Jekyll](https://jekyllrb.com/). \n", "\n", "![Notebook Screenshot Markdown](https://mk.imti.co/images/content/GolangToJupyter-markdown.png)\n", "\n", "Although I could easily save this Notebook as HTML, site builders like [Jekyll](https://jekyllrb.com/) and [Hugo](https://gohugo.io/) use Markdown to transform the content into the structure and style of my site. Markdown along with a sitebuilder allows me to present the notebook in a format that fits my site. Other options include:\n", "\n", "- [providing the Notebook for download](https://raw.githubusercontent.com/cjimti/mk/master/notebooks/GolangToJupyter.ipynb).\n", "- linking to Jupyter's [nbviewer](https://nbviewer.jupyter.org/github/cjimti/mk/blob/master/notebooks/GolangToJupyter.ipynb)\n", "- exported Markdown: [2018-06-10-golang-to-jupyter.md](https://github.com/cjimti/mk/blob/master/_posts/2018-06-10-golang-to-jupyter.md)\n", "- ...and of course [the page you are reading](https://mk.imti.co/golang-to-jupyter/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can run, modify or extend [this article](https://mk.imti.co/notebooks/GolangToJupyter.ipynb) in a Jupyter Notebook server on your local workstation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting Started with GO in Jupyter Notebooks\n", "\n", "1. Install the [Anaconda](https://www.anaconda.com/download/#macos) package manager.\n", " - Download [Anaconda for Python 3.6 on Mac](https://repo.anaconda.com/archive/Anaconda3-5.2.0-MacOSX-x86_64.pkg)\n", "2. Install the [gophernotes](https://github.com/gopherdata/gophernotes) golang kernel.\n", " - `go get github.com/gopherdata/gophernotes`\n", " - `mkdir -p ~/Library/Jupyter/kernels/gophernotes`\n", " - `~/src/github.com/gopherdata/gophernotes/kernel/* ~/Library/Jupyter/kernels/gophernotes`\n", "\n", "**NOTE:** Your path to Go source may be $GOPATH/src and not ~/src/. \n", "\n", "From a directory containing (or will contain) your notebooks, run:\n", "```bash\n", "jupyter notebook\n", "```\n", "\n", "This will fire up a Jupyter Notebooks server running on port 8888. You should be able to browse to [http://localhost:8888/](http://localhost:8888/)\n", "\n", "You can test out the [gophernotes](https://github.com/gopherdata/gophernotes) Golang kernel by [downloading and altering this article here.](https://raw.githubusercontent.com/cjimti/mk/master/notebooks/GolangToJupyter.ipynb)\n", "\n", "- [https://raw.githubusercontent.com/cjimti/mk/master/notebooks/GolangToJupyter.ipynb](https://raw.githubusercontent.com/cjimti/mk/master/notebooks/GolangToJupyter.ipynb)\n", "\n", "## Resources\n", "\n", "- Jupyter Notbooks golang kernel: [gophernotes](https://github.com/gopherdata/gophernotes)\n", "- [Installing](http://jupyter.readthedocs.io/en/latest/install.html) Jupyter Notbooks\n", "- [Jupyter Notbooks Documentation](http://jupyter.org/documentation)\n", "- [nteract - desktop application](https://nteract.io/)\n", "- [Markdown-Cheatsheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)" ] } ], "metadata": { "kernelspec": { "display_name": "Go", "language": "go", "name": "gophernotes" }, "language_info": { "codemirror_mode": "", "file_extension": ".go", "mimetype": "", "name": "go", "nbconvert_exporter": "", "pygments_lexer": "", "version": "go1.10.2" } }, "nbformat": 4, "nbformat_minor": 2 }