The basic usage of jupyter notebook and some pits --- learning record 2.23


1. Basic operation of jupyter notebook

1.1 Basic opening operation of jupyter notebook

1.1.1 Change the default open path of jupyter notebook

Since I use the jupyter notebook in anaconda, and there are multiple virtual environments in my anaconda, I only record the effective methods I use here.

First, find the jupyter notebook in the corresponding virtual environment, as shown in the figure: Insert picture description here
then right-click, select properties, and replace %USERPROFILE% in the target column with the path you want to open, as shown:
Insert picture description here

After the change is completed, open the jupyter notebook in the corresponding environment, and it will be the working path just set.
Note: I have also tried another way to change the default open path on the Internet, which is to open cmd, enter the jupyter notebook --generate-config command, and then change the #c.NotebookApp.notebook_dir =''this One path, but unsuccessful, may be because I have multiple environments, and I need to enter the jupyter notebook in the specified environment to change it successfully.

1.1.2 Open a file in a non-default path

First, open the anaconda prompt corresponding to the virtual environment tf2.3. If you want to open a file in a non-default path, such as "D:\", you need to enter the corresponding directory first, and then open jupyter notebook to enter the non-default path The file, as shown:
Insert picture description here

2. Introduction to the jupyter notebook interface

2.1 Create a notebook

After entering the default path of jupyter notebook, you will see the following interface. If you want to create a new notebook, click new in the upper right corner, it will display all the environments in the computer, select the desired virtual environment to create the corresponding notebook, take tf2.3 as an example:
Insert picture description here

2.2 Introduction to the menu bar

Click the conda column in the menu bar, you can see all the virtual environments in this computer, and the packages installed in each virtual environment, here you can download, delete, update and other operations on these packages. You can also copy and delete the existing virtual environment, and you can also export the environment's txt file. as the picture shows:Insert picture description here

2.3 notebook page introduction

After creating a new notebook, you can see the following interface. The editing page of this notebook consists of the name of the notebook (double-click to rename), the menu bar (file, edit, view, insert, cell, kernel, help, all functions can be found in Found in the menu bar), toolbar (select frequently used functions for easy operation) and cell (write code in the cell).
Insert picture description here

2.3.1 Menu bar

2.3.1.1 File column

Insert picture description here
new notebook: create a new notebook
open: open the main panel in a new page
make a copy: copy the current notebook and generate a new notebook
save as: save as, enter the save address in the pop-up interface, the address cannot be in the toot environment outside
rename: rename
save and checkpoint: the checkpoint save and generate
revert to checkpoint: the notebook will now return to the last checkpoint generated, if not now save the resulting checkpoint, at the moment the file will disappear
print preview: Print preview
download as: convert to other formats to download, such as .txt, .py, etc.
close and halt: stop running and launch the notebook

2.3.1.2 Edit bar

Insert picture description here
cut cells: cut cell
copy cells: copy cell
paste cells above: paste above the selected cell
paste cells below: paste below the selected cell
paste cells & replace: paste and replace the selected cell
delete cells: delete cell
undo delete cells : Undelete cell
split cell: split cell
merge cell above: merge the selected cell with the upper cell
merge cell below: merge the selected cell with the lower cell
move cell up: move up the selected cell
move cell down: move down the selected cell
edit notebook metadata: edit interface metadata to modify some settings of the web interface
find and replace: find and replace, just find and replace in the selected cell, similar to the replacement in word
cut cell attachments: cut cell attachment
copy cell attachments: copy cell attachment
paste cell attachments: paste cell attachment
insert image: insert image

2.3.1.3 View bar

Insert picture description here
toggle header: hide/show the title bar
toggle toolbar: hide/show toolbar
toggle line number: hide/show the number before each line of code
cell toggle: cell toolbar
edit metadata: edit the metadata of each cell
raw cell format: original In cell mode, there is no
slideshow: slide
attachments: edit cell attachment
tags: manage cell tags

2.3.1.4 Insert column

Insert picture description here
insert cell above: insert a new cell above the selected cell
insert cell below: insert a new cell below the selected cell

2.3.1.5 Cell column

Insert picture description here
run cell: run the selected cell
run cells and select below: run the selected cell, select the cell below after the run is completed
run cells and insert below: run the selected cell, insert a new cell below after the run is complete
run all: run all cells
run all above: run all the cells above the selected cell
run all below: run all the cells below the selected cell
Insert picture description here
cell type: cell type, which are code, Markdown, Raw NBConvert three types
Insert picture description here
current outputs: the output of the currently selected cell can be Operation mode (toggle: hide/display mutual switching, toggle scrolling: for cells with longer output content, switch between display with drop-down bar and display without drop-down bar, clear: clear the output of this cell)
all output: all cells Operable mode of output (same as above)

2.3.1.6 Kernel column

In the kernel column, you can see that there are different kernels in the change kernel option, as shown in the figure, respectively corresponding to the environment of my computer itself, so you can change different kernels to run the program here.
Insert picture description here
interrupt: interrupt the connection with the server
restart: restart, the variable may be lost after restarting the server
restart&clear output: restart and clear all cell output
restart&run all: restart and run all cells
reconnect: reconnect
shutdown: shut down, need to reconnect after shutting down, Variables may be lost after closing
change kernel: different virtual environments for kernel
conda packages in different environments : the same content as the conda column of the jupyter notebook page
visit anaconda.org: share packages, notebooks, projects and environments

2.3.1.7 Help bar

Insert picture description here
user interface tour: understand the interface
keyboard shortcuts: view all shortcuts
edit keyboard shortcuts: edit keyboard shortcuts
notebook help: notebook help document
markdown: markdown help document
python reference: python help document
ipython reference: ipython help document
numpy reference: numpy help document
scipy reference: scipy help document
matplotlib reference: matplotlib help document
sympy reference: sympy help document
pandas reference: pands help document
about: information about the notebook version

2.3.2 Toolbar

The functions of the toolbar can be found in the menu bar. Here, the frequently used functions are displayed as icons for easy operation.

2.3.3 Basic cell

Two modes of the
cell : When the leftmost color of the cell is blue , it is the command mode (the cell can be operated at this time, such as: copy, paste, cut, move, etc.) When
the leftmost color of the cell is green , it is the edit mode (At this time, you can edit the code and text in the cell.)
ESC key to enter the command mode.
Enter key for editing mode.

3. Introduction to notebook shortcut keys and commands

3.1 Shortcut keys in notebook

The notebook comes with a set of shortcut keys, which will greatly improve the efficiency of usual code writing. You can press the'H' key to view when the cell is in edit mode, as shown in the figure:
Insert picture description here
Shortcut keys summary (convenient to read in Chinese):
1. The cell is the command mode
. The shortcut key for querying the cell: H (the'H' here does not need to be capitalized to use the shortcut key.'H' only represents the H key on the keyboard, the same below.)
Add a cell above the current cell: A
Add a cell below the current cell: B
Cut the current cell: X
Copy the current cell: C
Paste the current cell: V
Delete the current cell: DD
Convert the current cell from code type or RAW NBConvert type to markdown type: M
to current cell Convert from markdown type or RAW NBConvert type to code type: Y
convert the current cell to markdown with a first-level title: 1
convert the current cell to markdown with a second-level title: 2
convert the current cell to a third- level title Title markdown: 3
Convert the current cell to markdown with a four-level heading: 4
Convert the current cell to a markdown with a five-level heading: 5
Convert the current cell to a markdown with a six-level heading: 6
is the current Cell join line number: L (cancel line number: LL)
Cancel the deletion of a cell: Z
select the cell above the current cell: K or Up key
Select the next cell of the current cell: J or Down key to
execute the current cell, and automatically jump to the next cell: Shift-Enter
executes the current cell, after the execution does not automatically transfer to the next cell: Ctrl-Enter to
run the cell, in the current cell Insert a new cell under the cell, and the default is the editing mode: Alt-Enter
is the current cell to enter the editing mode: Enter to
exit the editing mode of the current cell: Esc to
quickly jump to the last cell: Ctrl End to
quickly jump to the first cell: Ctrl Home
2.cell is the edit mode.
Code completion: Tab
indent to the right: Tab or Ctrl-]
Cancel indent: shift-Tab or Ctrl-[
Select all: Ctrl-A
undo: Ctrl-Z
is one or more lines Add/uncomment: Ctrl /

3.2 The magic command in the notebook

The magic command is some advanced usage of ipython, you can control the notebook through commands. The magic command is preceded by% or %%, which respectively represent the row magic command and the cell magic command. The row magic command is only used to write the row where the magic command is located, and the cell magic command applies to the entire cell.
All magic commands can be viewed here: https://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb#The-cell-magics-in-IPython

3.2.1 %lsmagic: show all magic commands

Insert picture description here
A total of 93 magic commands used for the current line (starting with %)
28 magic commands used for the current cell (starting with %%)

3.2.2 %run: call script file

First create a script file named hello.py, the code in the file is:

def hello():
    print('hello world!')
    
hello()

Put the script file and the .ipynb file you want to call the script file in the same folder, you can use the %run command to call the hello.py file, and the running result is shown in the following figure:
Insert picture description here

Record the pits stepped on:

1. About checkpoint

There are options for checkpoint in the file column of the notebook, as shown in the figure:
Insert picture description here
verify what this checkpoint is. First write two lines of code, and click save to checkpoint after running. As shown in the figure, you can see that the checkpoint has been created (actually equivalent to saving, archiving the file at a certain moment):
Insert picture description here
Then write two more Line code (for comparison later), just write whatever you want.
Insert picture description here
After clicking the revert to checkpoint option, there will be the time node of the checkpoint you just saved, as shown in the figure:
Insert picture description here
click on that time node, and it will return to the following interface, as shown in the figure Note: You
Insert picture description here
will find that it is exactly the same as when you saved it before. Now you should be able to understand the meaning of checkpoint. But the checkpoint must be manually saved and created before it can be reverted. If you directly revert to checkpoint without saving and creating checkpoint, the revert operation cannot be undone! Do remember that you need to create a checkpoint manually. The system will not automatically create it because it will be difficult to manage. For specific reasons, you can see the figure below.
Insert picture description here

2. About logout

If you accidentally click on the logout like me, you Insert picture description here
will see this interface, what to do, maybe just be a little bit blind!
Insert picture description here
After clicking the login page, the following interface will appear, prompting you to require a password
Insert picture description here

According to the prompt on the page, open the anaconda prompt (tf2.3), be sure to enter the corresponding virtual environment, and then enter the command

jupyter notebook list

A URL can be found, as shown in the figure.
Insert picture description here
**First method: **Directly copy the URL in the above image to the browser to open it, which is the page where the code was written before.

**The second method: ** is to copy a string of codes after the token in the URL, as shown in the figure below, enter it into the password or token column of the interface that requires a password before, and you can directly return to it. You can click logout The previous page.
Insert picture description here
It can be found that after clicking logout, the server did not close the program, but it was disconnected on the front-end, and you can re-establish a connection with the back-end program, so you can open this page again through the URL.

Record some conda commands:

(Continually updated)

###环境管理
activate env_name  #进入环境
deactive  #退出环境
conda env list  #查看有哪些环境
conda create -n env_name python=3.7  #创建python版本为3.7,名字为env_name的环境
conda remove -n env_name  #删除环境
conda create --name new_env_name --clone old_env_name  #将旧环境克隆为新环境
conda remove -n old_env_name --all  #彻底旧删除环境

###包管理
conda list  #查看环境下的所有包
conda list -n env_name  #查看指定环境下的包
conda update package_name  #更新包
conda install package_name  #下载包l
conda uninstall package_name  #删除个包

###清理无用包
conda clean -p  #删除没有用的包
conda clean -t  #删除tar包
conda clean -y --all  #删除所有的包及cache

Guess you like

Origin blog.csdn.net/weixin_45870475/article/details/113972795