How to Use Go in Jupyter Notebooks with Gophernotes

Apr 1, 2022 | Data Science

Welcome to the world of Gophernotes, where Go programming meets the versatility of Jupyter notebooks! In this guide, we’ll walk you through the installation and usage of Gophernotes, a Go kernel for Jupyter notebooks and nteract, allowing you to write and execute Go code interactively. So grab your virtual pen and let’s dive in!

What is Gophernotes?

Gophernotes is an innovative tool that allows you to use Go interactively in a browser-based notebook or desktop application. Whether you’re creating advanced data science applications or simply documenting your coding journey, Gophernotes lets you combine live Go code with rich visualizations, equations, and explanatory text. These notebooks can be easily shared through email, GitHub, Dropbox, or even the Jupyter Notebook Viewer.

Installation

Before you can start using Gophernotes, you need to install it. Here’s a breakdown of the installation process based on your operating system:

Prerequisites

For Linux or FreeBSD

Follow these steps for a quick installation:

sh
go install github.com/gopherdata/gophernotes@v0.7.5
mkdir -p ~/.local/share/jupyter/kernels/gophernotes
cd ~/.local/share/jupyter/kernels/gophernotes
cp $(go env GOPATH)/pkg/mod/github.com/gopherdata/gophernotes@v0.7.5/kernel* .
chmod +w .kernel.json
sed -i 's|gophernotes|$(go env GOPATH)/bin/gophernotes|' kernel.json.in kernel.json

For Mac

Install Gophernotes using the same steps as Linux, but use the following path:

sh
mkdir -p ~/Library/Jupyter/kernels/gophernotes
cd ~/Library/Jupyter/kernels/gophernotes
cp $(go env GOPATH)/pkg/mod/github.com/gopherdata/gophernotes@v0.7.5/kernel* .
chmod +w .kernel.json
sed -i 's|gophernotes|$(go env GOPATH)/bin/gophernotes|' kernel.json.in kernel.json

For Windows

Due to limitations on Windows, it’s recommended to use Docker for running Gophernotes. Here’s a brief approach:

sh
set GOPATH=temp.txt
mkdir %GOPATH%/src/github.com/gopherdata
cd %GOPATH%/src/github.com/gopherdata
git clone https://github.com/gopherdata/gophernotes
cd gophernotes
git checkout -f v0.7.5
go install
mkdir %APPDATA%/jupyter/kernels/gophernotes
xcopy %GOPATH%/src/github.com/gopherdata/gophernotes/kernel %APPDATA%/jupyter/kernels/gophernotes /s

Getting Started

After installation is complete:

  • If you installed locally, run jupyter notebook and select “Go” from the new drop-down menu.
  • If you are using nteract, launch the app and select “Go” from the language options.

Special Commands

Gophernotes also supports several special commands in addition to standard Go code:

  • %cd [path] – Change the current directory.
  • %go111module on|off – Switch between module-aware processing.
  • %help – Access built-in help.
  • $ shell_command [args...] – Run shell commands directly.

Troubleshooting

  • gophernotes not found: Ensure the path to the gophernotes executable in the kernel configuration JSON file points correctly to your installation location.
  • Kernel error: If you encounter an “OSError: [Errno 2] No such file or directory” message, it indicates that Jupyter can’t find gophernotes. Try adding a symlink as follows: sudo ln -s $HOME/go/bin/gophernotes /usr/local/bin/gophernotes and restart Jupyter.
  • Package import issues: If you receive an error indicating a package import issue in Windows, use %go111module off to switch to non-module mode. To re-enable it, execute %go111module on.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

By following this guide, you now have the tools to utilize Go programming within Jupyter notebooks seamlessly. Use this capability to create rich documents that include live code, which can be shared and collaborated on far and wide.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox