Coding How-To's News

How to Get the Best Autocomplete in Jupyter Notebooks and More

Read this post in other languages:
简体中文

Writing code in a “plain text”-looking Jupyter notebook, without any coding assistance, can be overwhelming and can definitely slow data scientists down. In this blog post we’ll explain 2 ways to avoid this pitfall and access autocompletion and other coding assistance features for your Jupyter notebooks.

Enable autocomplete feature

To enable code autocomplete in Jupyter Notebook or JupyterLab, you just need to hit the Tab key while writing code. Jupyter will suggest a few completion options. Navigate to the one you want with the arrow keys, and hit Enter to choose the suggestion.

Press the Tab key to enable code autocomplete in Jupyter Notebook

Unfortunately, Jupyter doesn’t offer automatic invocation for code completion options, which means you will be pressing the Tab key all the time. 

To avoid this, you can consider trying Datalore – a collaborative data science platform, which is available online for free. 

Code completion in Datalore

Datalore will automatically invoke code completion options, and it will give you contextual help when specifying method parameters. It will also take care of small but important things like putting your caret inside the parentheses, making your code less error-prone. 

A Notebook environment will already be preconfigured for you with the top data science packages preinstalled, so you can start writing code in Datalore right away. 

Try Datalore

Get proper code indentation for Python

In Python, indentation is important. Let’s take a look at how indenting works for a simple if clause.

In the example below, JupyterLab uses the wrong indentation, which leads to an error when executing the cell:

Importance of indentation in Python

Datalore corrects the indentation automatically, ensuring your code will be executed without errors.

Automatic Python indentation in Datalore

Get contextual help

In JupyterLab you can get contextual help by selecting a function and using the context menu. Datalore gives you contextual help when you hover over any function/method, and it also suggests method parameters on the fly while you’re typing code.

Contextual documentation in Jupyter vs Datalore

Other code editing features in Datalore

Quick-fixes

Datalore offers a wide variety of quick-fixes, such as import optimization, which helps your code look more professional and clean.

Import optimization in Datalore

Refactorings 

Datalore lets you rename variables and functions using the context menu. It will rename only the variables or functions, leaving text inputs with the same name unchanged. In JupyterLab, classic Find and Replace acts differently: it will replace all entries just like in a text file. 

Rename refactoring in Datalore

Highlighting errors before you execute the code

Nobody likes to see a red error message in a notebook. Datalore will highlight the errors before the execution of the cell, giving you a heads up about any potential problems.

Error-highlighting in Datalore

SQL, R, Scala, and Kotlin code autocomplete

In addition to Python, Datalore also offers code completion and syntax highlighting for SQL, R, Scala, and Kotlin.

Summary

JupyterLabDatalore
Python code autocompleteInvoked manually by pressing tab; auto-invocation available via Jupyter plugins and extensionsInvoked automatically out of the box
SQL, R, Scala, and Kotlin code autocomplete⛔️
Contextual helpIn a separate tabOn hover
Quick-fixes (e.g. removing unused imports)⛔️
Refactorings (e.g. renaming variables)⛔️
Method specifications when typing code⛔️
Error highlighting⛔️
Code indentationSometimes inaccurate (see examples above)

We hope these tips will help you enjoy a better code editing experience and make you more confident when sharing your work.

Try Datalore

How to try Datalore

If you’d like to use Datalore for yourself, register online for Datalore Community or Datalore Professional. You can also try it for your team by hosting Datalore in your private cloud or on-premises with the Enterprise plans.

That’s all for now! Keep an eye on our blog for useful tips and follow us on Twitter for the latest updates!

Happy data sciencing!

The Datalore team