You have 2 free member-only stories left this month.

Data Science

Productivity Tips for Jupyter Users

Make your workflow with Jupyter Notebook and JupyterLab more efficient

Photo by Ricardo Gomez Angel on Unsplash
Table of ContentsIntroduction1. Startup files
2. Auto reload
3. Multi-cursor
4. Exit confirmation
5. Update you python packages
6. Copying cells from another file

Introduction

To be an efficient Jupyter user, there are few things you need to know. In this article, I am going through six tips that will improve your productivity. These tips work both in Jupyter Notebook and Jupyter Lab.

For more information on Jupyter hacks and version control, please read these articles.

Startup files

If you are typing the same things again and again, startup files are your solution. Jupyter Notebook will access files within the start-up folder automatically. Generally, the default location is /Users/<your_profile>/.ipython/profile_default/startup in Mac.

You can find your startup directory by running this in a Jypyter Notebook cell.

get_ipython().profile_dir.startup_dir

Output can be like this.

'/Users/<your_profile>/.ipython/profile_default/startup'

You can create a file or files that you want to run in this directory when you start Jupyter Notebook.

For example, you create a file called 00-startup.py and add the following codes.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
get_ipython().magic('matplotlib inline')

When you are using magic %, you need to use get_ipython().magic() as seen above.

Restart your Jupyter Notebook. You can use Pandas or Numpy without importing them.

You can add more libraries depending on your needs. Example

Auto reload

When you modify an imported file, you have to restart the kernel. But autoreload reloads modules automatically before entering the execution of your cell.

You can add this to one of the cells and execute it.

%load_ext autoreload
%autoreload 2

By saving the modified file, it enables auto-reload in another file.

If you want to always enable this setting, you can add it to ~/.ipython/profile_default/ipython_config.py.

If you don’t have the file, you can call ipython profile create first.

ipython profile create
[ProfileCreate] Generating default config file: '/Users/yourname/.ipython/profile_default/ipython_config.py'
[ProfileCreate] Generating default config file: '/Users/yourname/.ipython/profile_default/ipython_kernel_config.py'

Around line 31:

# ~/.ipython/profile_default/ipython_config.py

c.InteractiveShellApp.exec_lines = ['%autoreload 2']

c.InteractiveShellApp.extensions = ['autoreload']

Auto-reload in action.

Multi-cursor

You can select multiple positions with your cursor. Click where you want to start, press Option, drag the cursor, start typing. You can use the Cmd+right arrow to jump to the far right of every line.

Exit confirmation

How many times have you typed y and pressed return after the Ctrl+C to shutdown? You can skip it by pressing the Ctrl+C twice.

Update you python packages

Are you updating python packages regularly?

Check which packages you need to update. This will list outdated packages.

pip list -o
# or
pip list --outdated

You can run install -U to update a package.

pip install -U package

If you want to update all packages at once, run this in your terminal.

pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

pip check verifies which installed packages have compatible dependencies. If you find a problem, you need to install the proper version using, pip install 'packagename==0.14.1'.

Other useful commands are:

pip reference

Copying cells from another file

In Jupyter Notebook, you can copy-paste from another file by pressing cmd+v twice. If you press only once you get the warning below. In JupyterLab, you need to use c to copy a cell or cells and v to paste it into another file.

I hope these tips will help your productivity. What is your favorite tip?

Newsletter

Newsletter sign-up link.

Tools and tips for programmers. Math teacher, programmer, husband, father, Japanese. https://bit.ly/3nEaAfr.

Sign up for The Variable

By Towards Data Science

Every Thursday, the Variable delivers the very best of Towards Data Science: from hands-on tutorials and cutting-edge research to original features you don't want to miss. Take a look.

You'll need to sign in or create an account to receive this newsletter.