How to Setup Autocompletion using Hinterland in Jupyter Notebook
Tags: tutorial, python, machinelearning, productivity
Published: 2023-11-04 15:38:08

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:

```bash
$ python3 -m venv <<name of virtual environment>>
```

For example:

```bash
$ python3 -m venv venv
```

You can activate it by typing this into the terminal

```bash
$ source venv/bin/activate
```

## Install Jupyter Notebook

There are different ways to install Jupyter Notebook on your computer. I personally use [Anaconda Navigator](https://docs.anaconda.com/free/navigator/index.html), but you can install it using `pip`:

```bash
(venv)$ pip install notebook
```

You can start notebook by:

```bash
(venv)$ jupyter notebook
```

## 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`.

```bash
(venv)$ pip install ipykernel
```

You can install a kernel with the following command:

```bash
(venv)$ python -m ipykernel install --user --name venv 
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2p3ahi7jp0b9ltonkkn6.png)


## Install Nbextensions

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddpu7juh91lfrgrf5tg1.png)

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:

```bash
(venv)$ pip install jupyter_contrib_nbextensions
(venv)$ pip install jupyter_nbextensions_configurator
```

Once those packages are installed, run following commands:

```bash
(venv)$ jupyter contrib nbextension install --user
(venv)$ jupyter nbextensions_configurator enable --user
```

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](https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/install.html).)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6g6z8rl2jby4ta46p0sx.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vap71jg24wsq7inr9fq7.png)