10 Useful Tips for Jupyter Lab

JupyterLab is the "new" interface for Jupyter Notebook. It contains all the functions of jupyter notebook, and has been upgraded to add many functions. Its biggest update is the modular interface, which can open several documents in the form of tabs in the same window at the same time. At the same time, the plug-in management is very powerful, and it is much taller and more advanced than jupyter notebook.

1. Spell check

Spell checking keeps our documents at a higher quality. By default, misspelled words are highlighted with a red background, but here I set it to be underlined.

To use this feature just install the following plugin:

pip install jupyterlab-spellchecker

2. Code formatting

Several packages need to be installed here, one is to provide formatting functions, and the other is our jupyterlab plug-in

# Install the formatters
$ pip install black isort
# Install the extension
$ pip install jupyterlab-code-formatter

Many formatting functions can be obtained through the Jupyter Lab Code Formatter extension

Once installed, a weird but cool-looking icon will appear at the top. If you don't want to use this button, you can configure the extension to automatically format on save (set in the Advanced Settings Editor).

3. Multi-line selection

This function is the same as jupyter notebook, hold down the Ctrl key when clicking to select multiple. Multiple selections can only be made within a single cell.

4. Add a virtual environment

Add the virtual environment as a kernel to Jupyter Lab with the following command, so it appears as an option in the upper right corner of the Launcher or kernel listings:

$ pip install ipykernel
$ ipython kernel install --user --name=new_or_existing_env_name

Note: The above code needs to be used in the virtual environment you need to add, not the jupyter lab environment

5. Run the notebook like a script

Jupyter notebooks for exploration and interactive output. But using the jupyter run command, each notebook cell can be executed sequentially like a Python script.

jupyter run path_to_notebook.ipynb

The command will return the output of each cell in the form of JSON, so if there is a lot of text output, it may freeze. We can save different hyperparameters into a single notebook and run it, which keeps a record of the run.

6. Split editor window

The windows of Jupyter Lab are displayed in the form of tabs. We can open several editing windows at a time, and we can drag the windows to split the editor window. The demonstration is as follows:

7. View documents at any time

There are three ways to find documentation for almost any function or magic command directly from the editor.

The first is to use the Shift+Tab keyboard shortcut (the default), which displays a popup with documentation for the function or class the cursor is under:

If you don't like the popup disappearing after you click somewhere else, you can also use contextual help, which can be accessed through the help menu or the Ctrl+I keyboard shortcut. Context help displays live documentation for the function or class pointed to by the cursor.

Finally, one way to do this is simply to add a question mark (without parentheses) to the end of the function or class name:

8. Mixed development of terminal commands and Python code

This feature may seem a bit quirky, but it's very useful. Using an exclamation point (!), you can run any terminal command in a code cell.

It is also possible to store the output of these commands in Python variables. For example use the output of !pwd to store the current working directory in the path variable:

path = !pwd

Here's a more practical example. Suppose you have a data folder that contains images for model training. All images are sorted into categories according to their classes.

The problem is that there are so many categories of images that we cannot count them manually. Need a quick way to count the number of directories inside data/raw/train and store its output in number_of_classes:

number_of_classes = !ls -1 data/raw/train | wc -l

>>> print(number_of_classes)
43

A shell command can solve the problem, so we don't need to write python directory traversal code

9. Notify execution

Even if you're not a Google Colab user, you'll love its cell execution notifications. Using the winsound built-in Python library, this functionality can be mimicked on Jupyter Lab:

import winsound

# Create a beep that lasts five seconds
duration = 5000
frequency = 440

winsound.Beep(frequency, duration)

10. Automatic reloading of modified Python scripts

It's very bad to mix scripts with notebooks, but sometimes we do need to do this, and if we update imported scripts, Jupyter won't automatically detect the changes unless we restart the kernel, which creates a lot of problems. So we can use the autoreload command to avoid this problem:

%load_ext autoreload
%autoreload 1

The above code will detect and refresh the kernel every second. It will not only detect script changes, but also changes to all files.

For python scripts, we can also use the pycat command to display the content of the Python script in syntax highlighting:

For other file formats, you can also use the cat command, which is a standard command for linux

Summarize

This article introduces several common and useful jupyter techniques. If you want to know more magic commands, you can run lsmagic. It will list all inline and cell magic commands.

https://avoid.overfit.cn/post/5f262543e01045cbab0aaafc5f6966c5

Author: BEXGBoost

Guess you like

Origin blog.csdn.net/m0_46510245/article/details/128713399