Autocomplete your code in Jupyter Notebook

Autocomplete your code in Jupyter Notebook
Page content

Jupyter Notebook is often used as a Python development environment, but code completion is not enabled by default. That’s why it’s a bit of a problem when importing packages. This time, I introduce some tips for code completion in Jupyter Notebook.

Autocomplete the code

Code completion in the IDE is an important feature for smooth implementation of the program. Some people call it autocomplete , and others call it intelli sense, but the functions are the same.

I investigated how to enable autocompletion when coding in Jupyter Notebook, so I’ll leave it as a reminder.

Method 1: Use IPCompleter.greedy

Here’s the easiest way to do it first. Just open any Notebook file and run the following magic command.

1%config IPCompleter.greedy=True

Press the Tab key to display the input completion.

However, there is a one-tempo delay until the candidate is displayed after the typing key. In my environment, I had to wait about 1-2 seconds.

Method 2: Use autocompletion of Nbextensions

Next, I’ll show you how to use the library to introduce Jupyter Notebook’s extensions at once.

The following command assumes that Jupyter Notebook is already installed.

1# Install the library
2pip install jupyter-contrib-nbextensions
3pip install jupyter-nbextensions-configurator
4
5# Enabling Extensions
6jupyter contrib nbextension install
7jupyter nbextensions_configurator enable

After that, restart Jupyter Notebook and you will see that a new tab labeled “Nbextentions” has been added.

nbextentions.png

Activate Hinterland for code completion .

You can also press the Tab key to display the input suggestions. I recommend Hinterland because the time to display candidates is faster than IPCompleter.greedy.

completion.png

Conclusion

I showed you how to use code completion on Jupyter Notebook. I recommend you to use the autocompletion of Nbextensions in Method 2. It’s quick to complete, and it’s very helpful because it will show you candidates even when you’re typing.

References