Make code into notes - Jupyter Notebook

This article was first published on the WeChat public account Python for Finance
link: https://mp.weixin.qq.com/s/KDCmpgwPbvrkRIuojtLpNg

What are Jupyter Notebooks?

Spyder

  • Spyder code editing area: file-based programming, after editing all the code, let the interpreter execute it together; the file can be saved in the form of py.
  • Spyder's Ipython console: interactive programming, that is, there are questions and answers, and the running results will be returned after entering the code. Using Spyder's Ipython console is like making a draft. The running record of the code cannot be saved in the form of a file. You can only view the recently run historical code in the history.
  • You can make a draft in Spyder's Ipython console, write code in the code editing area, and finally submit a satisfactory answer sheet.

Jupyter Notebook is a web-based application for interactive computing. It can be applied to the whole process of computing: development, documentation, running code and displaying results. Code can be written and run at the same time. The code can be run directly through the browser, and the running results are displayed below the code block. When writing documentation or statements for the code, it supports Markdown syntax, which is convenient for taking notes and viewing.

Advantages of Jupyter Notebook:

  • It is convenient to run the code in blocks, and the result of the operation is displayed directly under the code block
  • Interactive programming, that is, there are questions and answers, and the operation result will be returned after entering the code
  • Markdown can be inserted, and documentation can be written to facilitate the formation of detailed notes
  • Jupyter Notebook is essentially a notebook, which can integrate code, text, pictures, etc. into a document, and the document is saved in the form of ipynb

Start Jupyter Notebook

Click on the start menu -> Anacodna 3 -> Jupyter Notebook

Click on Jupyter Notebook, and a command line window will appear. After a short run, the browser page will pop up automatically. At this time, we have entered the web interface of Jupyter Notebook. In some cases, the browser page will not pop up automatically, you can copy the URL of the command line window and paste it into the browser URL to open it.

The code running on the Jupyter Notebook page opened in the browser is actually run in the command pane. The web page is displayed. The command pane in the above figure cannot be closed. After closing, Jupyter Notebook will prompt [Link failed] and cannot continue to run.

Jupyter Notebook main page

After executing the startup command, the browser will enter the main page of Jupyter Notebook, as shown in the figure below.

The file list displayed on the main page of Jupyter Notebook is the file list of the default file storage location of Jupyter Notebook.

You can view its default file storage location in the command line window of Jupyter Notebook.

Set the storage location of Jupyter Notebook files under windows

If you do not want all documents written in Jupyter Notebook to be directly saved in the default directory, you need to modify the file storage path of Jupyter Notebook.

Get the path of the configuration file

Open Anaconda Prompt, enter the command in the window jupyter notebook --generate-config, and get the path where the configuration file is located.

open configuration file

The "jupyter_notebook_config.py" file can be opened and edited with a document editing tool or IDE. Commonly used document editing tools and IDEs include Notepad, Notepad++, Spyder, PyCharm, VSCode, etc.

For example, open it with Notepad:

Modify the configuration file

Search keywords after entering the configuration file# c.NotebookApp.notebook_dir = ''

image-20230116125337708

Copy the target path (to set the storage location of the Jupyter Notebook file), for example , remove it D:\4 公众号from the configuration file , put the target path in single quotes, and add an escape character before the single quotes, that is, modify it to: , and save the configuration file.# c.NotebookApp.notebook_dir = ''#rc.NotebookApp.notebook_dir = r'D:\4 公众号'

image-20230116125403061

Change shortcut properties

"Start menu-Anaconda3-Jupyte Notebook" shortcut, right-click - more - open file location

Find the "Jupyte Notebook" shortcut icon, right-click - Properties - Target, remove the following "%USERPROFILE%/", click "Apply", "OK"

Finally restart Jupyter Notebook.

Basic use of Jupyter Notebook

create a new file

As shown in the figure, click "New" in the upper right corner, select "Python3" to create a new Notebook file, and select "Folder" to create a new folder.

After selecting "Python3" to create a file, the page is as shown below:

Write code

Code can be written in the cell. After writing, click the run button "Run" on the toolbar to run the current cell.

After the cell runs, it will be numbered (ln[1]: is displayed on the left), and the number can let us know the running code and the running order.

write text

"Cell format", including Code, Markdown, Heading, Raw NBconvert. Among them, the first two are the most commonly used, namely the code format and the Markdown writing format.

Switch the format of the cell from Code to Markdown to write text

save document

Click the save icon on the toolbar to save the file.

file rename

Click File-Rename to rename.

Guess you like

Origin blog.csdn.net/mfsdmlove/article/details/129396171