How to Setup Autocompletion using Hinterland in Jupyter Notebook

facepalm

Chris

Posted on November 4, 2023

How to Setup Autocompletion using Hinterland in Jupyter Notebook

In this tutorial, I will show you how to setup extensions and enable Hinterland in Jupyter Notebook. There is probably a way to set it up using conda but I will use pip in this tutorial to set it up.

For this tutorial, I will assume following:

  • using Python 3.9 or higher
  • running macOS or linux (may require different command for Windows)

Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. It is commonly used for data analysis, data visualization, machine learning, and scientific computing, but it can be employed for various other tasks as well. Jupyter Notebook provides an interactive and user-friendly environment for writing and running code, making it popular among data scientists, researchers, and educators.

Hinterland is an extension for Jupyter Notebook that provides code autocompletion suggestions in code cells as you type. It aims to enhance your coding experience by suggesting completions for variable names, functions, methods, and other code elements, which can save you time and reduce potential coding errors. It provides suggestions without you having to press tab key.

Create Virtual Environment

I like to use venv to set up my virtual environment. It goes like this:

$ python3 -m venv <<name of virtual environment>>
Enter fullscreen mode Exit fullscreen mode

For example:

$ python3 -m venv venv
Enter fullscreen mode Exit fullscreen mode

You can activate it by typing this into the terminal

$ source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Install Jupyter Notebook

There are different ways to install Jupyter Notebook on your computer. I personally use Anaconda Navigator, but you can install it using pip:

(venv)$ pip install notebook
Enter fullscreen mode Exit fullscreen mode

You can start notebook by:

(venv)$ jupyter notebook
Enter fullscreen mode Exit fullscreen mode

Use Virtual Environment as Kernel

In Jupyter Notebook, a "kernel" is used as a computational engine that executes the code contained within the notebook. Each notebook can be associated with a specific kernel. I will show you how to use our virtual environment as kernel.

First, make sure you have activated venv by source venv/bin/activate. Then we need to install ipykernel.

(venv)$ pip install ipykernel
Enter fullscreen mode Exit fullscreen mode

You can install a kernel with the following command:

(venv)$ python -m ipykernel install --user --name venv 
Enter fullscreen mode Exit fullscreen mode

You should replace venv with your python environment.

Now, you should be able to see your kernel when you start up Jupyter Notebook:

Image description

Install Nbextensions

When you start up the Jupyter Notebook, you may see an environment like below:

Image description

Right now, you may not have the Nbextensions tab. We need that tab to enable extensions, so let's install it now.

(Make sure you are not running Jupyter Notebook.)

You need to install few more Python packages:

(venv)$ pip install jupyter_contrib_nbextensions
(venv)$ pip install jupyter_nbextensions_configurator
Enter fullscreen mode Exit fullscreen mode

Once those packages are installed, run following commands:

(venv)$ jupyter contrib nbextension install --user
(venv)$ jupyter nbextensions_configurator enable --user
Enter fullscreen mode Exit fullscreen mode

The first command copies the nbextensions’ javascript and css files into the jupyter server’s search directory, and edits some jupyter config files.

The second command enables the nbextension.

(You can find more information about these steps here.)

Now, when you start up the Notebook, you should be able to see the Nbextensions tab.

We are almost done. We just need to enable Hinterland now.

Enable Hinterland

Go to Nbextensions tab.

Make sure checkbox beside "disable configuration for nbextensions without ..." is unchecked.

In the filter, type 'hinterland'. Make sure checkbox beside the Hinterland is checked to enable it:

Image description

And that is it!

If you create a notebook (make sure to use your virtual environment as a kernel), and start typing things, you will see code suggestion showing up automatically without pressing tab key all the time! :)

Image description

💖 💪 🙅 🚩
facepalm
Chris

Posted on November 4, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related