Jupyter Lab entry to proficiency

Jupyter Lab && Jupyter Notebook

Jupyter Lab can be understood as an upgraded version of Jupyter Notebook, which adds many functions. It supports multiple programming languages ​​such as python, R, and java, as well as writing languages ​​such as markdown and letex, and formula input. Compared with the notebook , the biggest update of jupyter lab 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 elegant to use than jupyter notebook.

Jupyter installation and startup

After installing Anaconda, both jupyter notebook and jupyter lab can be used. You can find the corresponding execution program in the Scripts directory of the Anaconda installation path.

Jupyter-lab function introduction

After starting, you will enter the modular management interface of jupyter-lab. From the management interface, you can choose to create jupyter's ipynb document, pure markdown document, txt document, etc., or you can directly open the python3 console to execute the python command line. Directly start the command line terminal (Terminal) to execute Windows command line operations.

Tips for using Jupyter-lab

The use of Jupyter-lab is very convenient, the interactive interface is very friendly, and it will be very efficient after mastering some common usage skills. Here is a general summary of the several directions of the technique.

  1. Jupyter-lab is very expandable. Currently, there are relatively rich plug-ins available, such as code completion plug-ins, code debugger plug-ins, github-related plug-ins, Excel-related plug-ins, drawing-related plug-ins, etc., and the installation is also very easy. convenience.

  2. Jupyter-lab has magic functions, which can easily realize some complex functions. If you want to call an external python script, display the running progress and running time of the python command line in real time, you can use magic functions.

  3. Jupyter-lab shortcut keys, any tool, being able to use the shortcut keys proficiently will greatly improve the efficiency of use. Jupyter-lab has also set up a lot of shortcut keys, and some commonly used ones are still necessary to use.

Jupyter-lab has linked a lot of official documentation for software and modules, you can click Help in the file window of ipynb. You can see that there are more than a dozen official documentations including Jupyter, Markdown, and Python3 linked, which is convenient for everyone to view and learn.

Jupyter Lab uses the conda virtual environment

1. Add ipykernel to the virtual environment

Method 1: Add ipykernel directly when creating the environment

  • Method: conda create -n [virtual environment name] python=3.8 ipykernel
  • Examples are as follows:
conda create -n tensorflow_cpu python=3.8 ipykernel

Method 2: Add ipykernel to the created virtual environment

  • Method: conda install -n [virtual environment name] ipykernel
  • Examples are as follows:
conda install -n tensorflow_cpu ipykernel

2. Activate the virtual environment you want to use

  • Examples are as follows:
conda activate tensorflow_cpu

3. Write the virtual environment into the kernel of jupyter notebook

  • Method: python -m ipykernel install --user --name virtual environment name --display-name virtual environment name
    • The first virtual environment name indicates the created virtual environment name
    • The second virtual environment name indicates that you want it to display its name in the kernel option of jupyter notebook
  • Example:
python -m ipykernel install --user --name tensorflow_cpu --display-name "tensorflow_cpu_env"

4. Run Jupyter lab

  • Method: jupyter lab --port port number

  • Example:

jupyter lab --port 8888

In this way, we can use multiple python environments in one jupyter-lab

Using Shell && magic commands

Shell command

Commands can be used directly in the notebook  shell , as long as they are in  code cell ,  ! everything starting with will be treated as a  shell command, which is very useful when processing data or files, and managing Python packages.

magic order 

Magic commands are very convenient and useful commands built into the IPython core, and they are specially designed to handle specific tasks. Although they look similar to unixcommands , they are actually Pythonimplemented through . There are many magic commands, but only some of them are introduced in this article.

There are two types of magic commands:

  • line magics
  • cell magics

As you can tell from the name, it is mainly divided according to its scope of action, some are executed in a single line, and some can be used in multiple lines or in the entire unit.

If you want to know the available magic commands, you can enter the commands  %lsmagic , and the output results are as follows. You can see that they are indeed divided into  line two  cell categories, and the numbers of commands are given respectively.

The usage forms of line magic command and unit magic command are also different, the line magic command is  % at the beginning, while the unit magic command is  %% at the beginning .

1. Code execution time (Timing Execution)

%time Usually we need to consider the execution time of the code. There can be two time magic sums  in the notebook  %timeit, and they all have two modes: line and unit

For  %time , usage examples are as follows:

 %timeit The difference between and  %time is that it will run the given code multiple times and calculate an average time. You can  -n specify the number of runs by adding parameters. If not specified, an optimal number will be automatically selected. Examples are as follows:

2. Implement different programming languages

Different programming languages ​​can be executed in Jupyter notebook. Although the selected core has an established language, such as the one selected in the example of this article,  Python3 different programming languages ​​can be executed through magic commands, and  %lsmagic the output results can also be found.

plugin installation

Chinese language package installation

Install the Chinese dependency package with pip in the terminal (terminal), restart JupyterLab, and you can see one more language option in the settings (of course, it is recommended to practice English more).

pip install jupyterlab-language-pack-zh-CN

 

references:

https://www.jianshu.com/p/ad8dcf75db40

Jupyter Lab uses conda virtual environment_csdn-WJW的博客-CSDN Blog_jupyter lab uses virtual environment

Advanced Jupyter Tutorial - Nuggets

Guess you like

Origin blog.csdn.net/chenxy02/article/details/124377754