Public account: You and the cabin by: Peter Editor: Peter

Hello, I’m Peter

Many readers have asked me what editor Peter wrote the Python code in his article with.

Today: Jupyter Notebook, no Pycharm, no Vscode, no Sublime Text. Just Jupyter Notebook. Since 2019, it has been used for more than two and a half years. Today, we will talk about it

To do a good job, he must sharpen his tools

Jupyter.org/

Directions for use: jupyter – notebook. Readthedocs. IO/en/stable/n…

Zhihu good tutorial: zhuanlan.zhihu.com/p/33105153

directory

Who’s using Jupyter Notebook

Maybe you still don’t know who uses Jupyter Notebook?

Here are the users from the official website: Google, Microsoft, NASA, IBM and other internationally renowned companies are among them. So, are you sure you don’t need it yet?

What’s in the Jupyter Notebook

So what does Peter write with the Notebook? (Ipynb files are completed in Jupyter Notebook)

1. I like Pandas

2. Plotly

3. Use of visual library Pyecharts

Summary: Peter has done all of his articles in Jupyter Notebook

What is Jupyter Notebook

Introduction to the

Jupyter Notebook is a Web-based application for interactive computing. It can be applied to the entire computing process: from developing documentation to writing, running code, and presenting results.

Simply put, Jupyter Notebook is opened in the form of a web page. You can write and run the code directly in the web page, and the running results of the code will be directly displayed under the code block.

If you need to write an explanatory document during the programming process, you can use Markdown syntax to write it directly on the same page for timely explanation and explanation.

composition

Jupyter Notebook consists of two main parts: web pages and documents

Jupyter Notebook’s Web-based application is a combination of documentation, calculation formulas, documentation, visualization, and even powerpoint slides. In short, the web app’s programs can do a variety of things

All programs in Jupyter Notebook can be input and output in various forms: formulas, documents, rich media forms, etc.

The files of Jupyter are saved in JSON format with a suffix of. Ipynb, which is convenient for version control and sharing with others.

In addition, documents can also be exported to HTML, LaTeX, PDF, Markdown and other formats, very convenient and Internet!

The characteristics of

  • Easy programming: Programming in Jupyter Notebook, with syntax highlighting, indentation, TAB completion and other functions; You can also run code directly in the browser
  • Markdown support: Support Markdown syntax in Jupyter Notebook, can directly write Markdown documents
  • Memory function: Information generated in the jupyter notebook (run at the same time) has memory function
  • Document output diversification: In addition to Markdown, it also supports PDF, PPT, Latex formulas, HTML, PY files and other formats
  • Dynamic Visualization: The graphics generated in Jupyter Notebook can be dynamically visualized

The installation

It is recommended to install Jupyter Notebook directly through Anaconda to save a lot of trouble in the future.

Anacoda is the world’s largest data science platform, a Python-based environmental management tool that is better suited to data workers than other library management tools.

In addition to installing The Anaconda notebook, many python libraries such as Pandas and Numpy will be installed automatically.

Anaconda installation searchable tutorial, foolproof installation

Recommend several installation tutorials:

1, the Windows system: zhuanlan.zhihu.com/p/75717350

2. MacOS: blog.csdn.net/lq_54776298…

3, Linux system (Ubuntu) : zhuanlan.zhihu.com/p/84544848

The installation package download address: www.anaconda.com/products/in…

Start the

(1) After installing the Jupyter Notebook, we can start it from any location, directly enter the Jupyter Notebook to start.

After executing this command, a list of Notebook server information will be displayed on the terminal, and the browser will automatically launch Jupyter Notebook:

(base) Peter: pandas series Peter $PWD - view the current directory/Users/Peter/Desktop/WeChat/article/pandas series (base) Peter: pandas series Peter $ Jupyter notebook - Enter Jupyter notebook to startCopy the code

!!!!!!!!! Note: After starting Jupyter, do not shut down the terminal for all operations, otherwise the Notebook will disconnect from the local server.

(2) The initial page contains all the files (folders and documents) in your current directory:

Note that the default address for the notebook is localhost:8888, which is the default native port 8888. When we open multiple notebooks, ports 8889,8890 and so on will automatically change.

Of course, we can also customize ports:

jupyter notebook --port 9988   Use port 9988
Copy the code

(3) After startup, we can create a new ipynb file:

use

After launching the Jupyter Notebook and creating a new file, we can start coding and documenting inside. Here’s how to use Jupyter Notebook.

First, we need to understand that there are two modes in Jupyter Notebook

  • Command mode: Keyboard input runs program commands with cells in blue
  • Edit mode: allows you to type code or text into cells that are green

The main page

The main page is divided into four parts:

  • The notebook name, which can be renamed
  • The menu bar
  • The toolbar
  • Code cell

Help document

If you’re new to notebook, check out the help documentation:

! jupyter notebook -h# exclamation point! Cannot little
Copy the code

rename

The default name is “unnamed”

file

[File] Menu is mainly for the input and output operations of various files, especially ipynb files downloaded into various forms for output, very convenient:

Run the code

When we enter code in a cell of code, how does it work?

1. Click “Run” button

2. Shortcut keys

Place the cursor in the code box we want to run, then Shift+Enter to run ~

3. Run all the code

If we want to run the entire notebook code from scratch, do the following: service to Restart &Run All

introspection

Use question marks around an object? You can display summary information about the object, which can be either self-built or native to Python:

Introspection summary of print function and custom function:

Use double question marks? Another use is to display the source code of the function:

%run

The %run command can be used to run any Python file:

  • Python files have the suffix “.py”
  • “%run” is followed by the absolute path to the Python file
%run Specifies the absolute path to the Python fileCopy the code

Suppose we have a Python file in the current directory:

  • Three variables are defined
  • Given a return value result

Let’s run the py file in the notebook and look at the three variables and return values

Extend the functionality

The installation

Jupyter Notebook itself doesn’t do much, so it usually installs an extension and then takes off.

The installation and startup commands are as follows:

Install Jupyter's configurator
pip install jupyter_nbextensions_configurator

Start the configurator
jupyter nbextensions_configurator enable --user
Copy the code

Displays the number of lines of code

When there are many lines of code in a cell, we want to display the number of lines:

Encode Markdown documents

When Peter needs to take notes in notebook, he uses Markdown. In Jupyter Notebook, the contents can be written in Markdown syntax directly. There are two ways to achieve this.

As for what Markdown is and the grammar of Markdown, you can directly reply Markdown on the official account to get free PPT for learning.

Method 1: Select [tag] from the drop-down list.

Method 2: Use of shortcut keys: [blue +M key on the left], commonly used by Peter

Generated directory

When Markdown is used, there are different levels of directories that can be displayed in the notebook. First you need to enable this functionality in the extension:

Click Enable directory generation: Directories are on the left by default and are implemented hierarchically

We place the cursor over the directory and display the + symbol, which can be dragged to the right:

In addition to generating directories on the left, you can also generate directories at the top of the entire ipynb file:

When you click on a heading in either the left or the top directory, it automatically jumps to the corresponding location, making it super easy to view and locate documents or code.

The title highlighting

Once the extension is installed, we can also highlight the title:

  • Available in 3 colors
  • When you highlight it, the title will be highlighted

LaTex formula

Since the Jupyter Notebook can use Markdown, we can edit the LaTex formula in the Jupyter Notebook. For example, enter the following text in the Markdown cell:

$$ P(A \mid B) = \frac{P(B \mid A) \, P(A)}{P(B)} $$
Copy the code

LaTeX formulas are generated:

Output multiple results

Normally, the notebook prints only one result. You can set it to print more than one result at a time.

By default, only the value of the most recent variable ** is printed, in this case b**.

By setting the InteractiveShell astnodeinteractivity parameter to all, can let all variables or statement can be displayed, add two lines of code output can achieve the effect of multiple variables:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'
Copy the code

Spell check

This is an extension to check for spelling mistakes, which can quickly catch mistakes in our spelling. The main thing needed is that this plugin works in Markdown format:

This is wrong question

Hidden input

When we only want to see the output out section, we can hide the input code:

Here is the effect before it is hidden:

shortcuts

1. Command mode shortcut key (press Esc to open) :

  • The command mode combines the keyboard commands with the Jupyter Notebook commands. You can run the Notebook commands by using the combination of different keys on the keyboard.
  • According to theescKey to enter the command mode.
  • In command mode, the cell border is gray and the left border line is a thick blue line.
shortcuts role
Enter Go into edit mode
Shift-Enter Run this cell and select the next cell
Ctrl-Enter Run this unit
Alt-Enter Run this cell and insert a new cell under it
Y The unit goes into code state
M The unit goes into markdown state
R The unit enters the RAW state
One, two, six Set level 1, 2, and 6 headings
Up Select the top cell
K Select the top cell
Down Select the unit below
J Select the unit below
Shift-K Select the upper cell continuously
Shift-J Select the unit below continuously
A Insert a new cell at the top
B Insert a new cell below
X Cut the selected cell
C Copy the selected unit
Shift-V Paste it into the top cell
V Paste it into the bottom cell
Z Restores the last deleted cell
D,D Deletes the selected cell
Shift-M Merges the selected units
Ctrl-S Save the current NoteBook
S Save the current NoteBook
L Switch the line Numbers
O The transformation output
Shift-O Conversion output scroll
Esc Close the page
Q Close the page
H Displays shortcut key help
I,I Interrupt the NoteBook kernel
0, 0 Restart the NoteBook kernel
Shift ignore
Shift-Space Scroll up
Space Scroll down

2. Shortcut key for editing mode (press Enter to start)

  • Edit mode allows users to edit code or documents within cells.
  • According to theenterorreturnKey to enter editing mode.
  • In edit mode, both the cell border and the left border line are green.
shortcuts role
Tab Code completion or indentation
Shift-Tab prompt
Ctrl-] The indentation
Ctrl-[ Remove the indentation
Ctrl-A select all
Ctrl-Z undo
Ctrl-Shift-Z redo
Ctrl-Y redo
Ctrl-Home Skip to the beginning of a cell
Ctrl-Up Skip to the beginning of a cell
Ctrl-End Skip to end of cell
Ctrl-Down Skip to end of cell
Ctrl-Left Skip to the first word on the left
Ctrl-Right Jump to a word on the right
Ctrl-Backspace Delete the preceding word
Ctrl-Delete Delete the following word
Esc Switch to command mode
Ctrl-M Switch to command mode
Shift-Enter Run this cell and select the next cell
Ctrl-Enter Run this unit
Alt-Enter Run this unit and insert a unit below
Ctrl – Shift – Segmentation unit
Ctrl-Shift-Subtract Segmentation unit
Ctrl-S Save the current NoteBook
Shift ignore
Up Cursor moves up or to the previous cell
Down The cursor moves down or to the next cell
Ctrl-/ Comment an entire line/uncomment it

Magic command

IPython’s special commands are called magic commands, and they are designed to simplify common tasks.

Website address: ipython. Readthedocs. IO/en/stable/I…

Magic commands are prefixed with the percent sign %, such as %timeit used to check the execution time of a piece of Python code. The magic command can be used directly or assigned to a variable:

Here are a few common IPython magic commands recorded at 👇 :

Magic command role
%pwd View the current working directory
%cd Change the current working directory
%ls View the directory file list
%writefile Written to the file
%run Run the script; % run can execute Python code from a.py file or specify an ipynb file
%whos View current variable
%reset Know current variables
%timeit Test single line of code run time
%hist Print command input history
%paste Execute preformatted Python code from the clipboard
%cpaste Open a prompt to manually paste executed Python code
%debug Enter the interactive debugger from the bottom where the last error occurred

Jupyter Notebook change theme

The default theme used in the above graphics is white, but the theme can actually be changed:

Install and use different themes in Jupyter Notebook

# installation
pip install jupyterthemes
pip install --upgrade jupyterthemes
Copy the code

Switch themes

# See all color themes --list
jt -l

# Select theme --themeJt-t Topic name# restore the default theme --recover
jt -r
Copy the code

1. View themes

$jt -t monokai  # Select a theme
$jt -t oceans16

Then restart the Jupyter Notebook

$jupyter notebook   Start command
Copy the code

If you want to go back to the original theme type Jt-r