当前位置:网站首页>Tips for using Jupiter notebook
Tips for using Jupiter notebook
2022-06-27 08:29:00 【User 8336546】
Preface
For more convenient and fast use Jupyter Notebook, This article will introduce some related skills .
Related skills
Use the question mark to view information about the object
For all objects in the code , Include : Variable 、 function 、 Method 、 modular 、 wait , Can be in the... Of an object front or Back add to One ?, To view information about the object .
def aaa():
print('Hello World')?aaa
The effect is as follows :
For our Customize Function of , It can be found in the function name front or Back add to Two ?, such , The related information displayed will contain the function's Source code .
def aaa():
print('Hello World')aaa??
The effect is as follows :
Execute with exclamation mark Shell command
In the use of Jupyter Notebook When writing code , Only need Shell Add a... Before the command !, You can go to notebook In the implementation of Shell command , There is no need to switch back and forth .
give an example : View the current local network status :
!ipconfig
The effect is as follows :
In addition to executing Shell Besides the command , You can also assign the execution result to Python Variable :
a = !ipconfig print(a) print(type(a))
About output
stay notebook When running code in a cell in , By default , In the cell The last line The value of is automatically output :
def aaa():
print('Hello World')
18The output effect is shown in the figure below :
If you don't want to automatically output the value of the last row in the cell , You can add a semicolon at the end of the last line ;
def aaa():
print('Hello World')
18;The output effect is shown in the figure below :
stay At present notebook in , If you want cells to Every line The values of are automatically output , You can add the following code :
from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = 'all'
The output effect is shown in the figure below :
If you want to all Of notebook All of them are automatically output to cells Every line Value , You can modify or create ipython_config.py The configuration file .
File path :
- Windows:C:\Users\ user name .ipython/profile_default/ipython_config.py
- MacOS:/Users/ user name /.ipython/profile_default/ipython_config.py
- Linux:/home/ user name /.ipython/profile_default/ipython_config.py
Open the configuration file and add the following code :
c = get_config() c.InteractiveShell.ast_node_interactivity = "all"
Cross cell encoding
It should be noted that , stay notebook in :
- The cells in the previous run will save the relevant variables to Memory in , So that the cells running later can Continue to use These variables
- close notebook After the page , Variables stored in memory Will not be lost
- If at present notebook
ShutdownorRestart了 , Then all variables stored in memory will The loss of
Suggest : By cell From top to bottom Write code in the order of and In turn, run Code , otherwise , Not only is it not readable , And it's easy to make mistakes .
Support Markdown grammar
Jupyter Notebook Support Markdown grammar , Adjust the cell to... In the drop-down option of the toolbar Markdown that will do .
About Markdown You can see the article about 《Markdown grammar 》
Here's a reminder , When inserting a picture , You can directly click... In the menu bar Edit, Click... In the open options Insert Image, This operation mode advantage yes : Combine the picture with notebook Bind together , In the face of notebook There is no need to process the picture separately when moving or performing other operations ; shortcoming yes :notebook The file gets bigger , Cannot control picture size .
Shortcut key
stay notebook There is Two kinds of cells , Respectively :
- Code Cell : To write code
- Markdown Cell : Used to format plain text into rich text
Each cell has Two modes , Respectively :
- Edit mode : Press
EscKey orClick on the outside of the cell, Can switch to command mode - Command mode : Press
EnterKey orClick inside the cell, You can switch to edit mode ( For the after run Markdown Cell , wantDouble click inside the cell, To switch to edit mode )
Edit mode Next shortcut key :
Shortcut key | function |
|---|---|
Tab | Increase Indent (4 A space )、 Code auto completion |
Shift+Tab | Reduce indent (4 A space )、 View information about the object |
Ctrl+A(Command+A) | Future generations |
Ctrl+Z(Command+Z) | revoke |
Ctrl+Y(Shift+Command+Z) | recovery |
Ctrl+/(Command+/) | notes 、 uncomment |
Ctrl+D(Command+D) | Delete row |
Ctrl+Home(Command+Home) | Jump to the beginning of the cell |
Ctrl+End(Command+End) | Jump to the end of the cell |
Esc | Exit edit mode and enter command mode |
Ctrl+S(Command+S) | Save and update the log point file |
↑ | Move the cursor up |
↓ | Move the cursor down |
Ctrl+Enter | Run the current cell |
Shift+Enter | Run the current cell , And select the lower cell ( If there is no cell below , Then a cell will be automatically inserted below and selected ) |
Alt+Enter(Option+Enter) | Run the current cell , Automatically insert a cell below and select |
Command mode Next shortcut key :
Shortcut key | function |
|---|---|
↑ | Select the upper cell |
↓ | Select the lower cell |
Shift+↑ | Expand to select the upper cell |
Shift+↓ | Expand the selected lower cell |
A | Insert a cell above |
B | Insert a cell below |
M | take Code Cells are converted to Markdown Cell |
Y | take Markdown Cells are converted to Code Cell |
S | Save and update the log point file |
Press twice in a row D | Delete the selected cell |
Z | Undo the deletion of cells |
F | Find and replace |
X | Cut the selected cells |
C | Copy selected cells |
V | Paste cells below |
Shift+V | Paste cells above |
Enter | Exit command mode and enter edit mode |
Ctrl+Enter | Run the selected cell |
Shift+Enter | Run the selected cell , And select the lower cell ( If there is no cell below , Then a cell will be automatically inserted below and selected ) |
Alt+Enter(Option+Enter) | Run the selected cell , Automatically insert a cell below and select |
Extension set nbextensions
Extension set nbextensions Contains a series of very easy-to-use plug-ins , Thus, it can greatly expand Jupyter Notebook The function of , And enhance Jupyter Notebook Interactive experience .
setup script :
1. Install third party libraries jupyter_nbextensions_configurator
It is jupyter notebook Configurator for extension set .
pip Installation command :
pip install jupyter_nbextensions_configurator
conda Installation command :
conda install jupyter_nbextensions_configurator
2. Boot Configurator
Start command :
jupyter nbextensions_configurator enable --user
Start at this time jupyter notebook, You will find an extra menu named Nbextensions The tab of .
3. Install third party libraries jupyter_contrib_nbextensions
It is jupyter notebook Set of extensions for .
pip Installation command :
pip install jupyter_contrib_nbextensions
conda Installation command :
conda install jupyter_contrib_nbextensions
3. Install related files
So that you can pass the 2 Step enabled configurator , Right. 3 Step to access the installed extension set .
Installation command :
jupyter contrib nbextension install --user
You will see , stay Nbextensions Many plug-ins are displayed in the configurator of this tab , Click the small box at the top left of the page , Tick off the right , All the plug-ins have become available , It can be used as needed .
nbextensions Medium Common plug-ins :
The plugin name | function | remarks |
|---|---|---|
Hinterland( Code hinting ) | Each letter entered , The menu of automatic code completion will pop up | |
Codefolding( Code folding ) | Yes Code The code in the cell adds the function of folding | |
Collapsible Headings( Collapsible title ) | Collapse cells by title | |
Table of Contents( A table of contents ) | Display all the titles in a floating window and a navigation menu | |
Autopep8( Automatically pep8) | according to pep8 Specification pair of Code In a cell Python Format the code | Need to install autopep8 This third-party library . pip Installation command :pip install autopep8 conda Installation command :conda install autopep8 |
Code prettify( Code beautification ) | format Jupyter Notebook Code for all supported programming languages | Before formatting code for a programming language , First, you need to install the corresponding third-party library or package , To format Python Code , You need to install yapf This Google Open source third-party libraries . pip Installation command :pip install yapf conda Installation command :conda install yapf |
Code Font Size( Code font size ) | On the toolbar , Provide buttons to increase and decrease the code font | |
ExecuteTime( execution time ) | In the process of execution , It will show when the execution starts ; After execution , The execution duration and the time at which the execution ends will be displayed | |
AutoSaveTime( Auto save time ) | Set up notebook Auto save interval | notebook When automatically saved , The corresponding log point file will not change |
highlighter( Highlight the tool ) | Yes Markdown Highlight the text in the cell | |
Live Markdown Preview( real time Markdown preview ) | stay Markdown Preview the running results in real time at the bottom of the cell | |
Tree Filter( File tree filter ) | stay Files Tab , Just enter the keywords , You can filter the following files and folders |
Export to pdf file
Here are two types of export pdf file Methods :
** Method 1 :** Use the menu to export .
Click on File tab , choice Download as, choice Markdown(.md), Export the file first as .md Format , Reuse Typora open , And exported as pdf file .
** Method 2 :** Use the command to export ( need Pandoc and XeLatex Support ).
First installation Pandoc:
pip install pandoc
install MikTex:
The official website link is thrown here :https://miktex.org/download
The installation process is simple , All the way Next, No more details here .( After installation cmd Test it xelatex Can you use )
Click on File tab , choice Download as, choice LaTeX(.tex), Export the file first as .tex Format , In order to make the exported file display Chinese normally , Open the file ( I use Sublime Text 3), Find the code \documentclass[11pt]{article}, Add the following code after :
\usepackage{fontspec, xunicode, xltxtra}
\setmainfont{Microsoft YaHei}
\usepackage{ctex} Get into .tex file In the directory , Open the command prompt window , Input :
xelatex yourNotebookName.tex
If it's the first run , A large number of dependent files will be installed , choice Install that will do ( It's a long process ).
( You can uncheck the pop-up box , Make it automatically install all required dependencies )
At the end of the run , You will find that the directory has been generated pdf file .
The second method produces pdf file , Better than method 1 .
Conclusion
There are many practical skills , For the time being, I can't use it , There is no summary , There will be time to continue to add .
边栏推荐
- How much do you know about the cause of amplifier distortion?
- 关联GIS:条条道路通UE5城
- Matlab tips (19) matrix analysis -- principal component analysis
- ArrayList和LinkedList的区别
- Zabbix部署说明(Server+Win客户端+交换机(H3C))
- 100% understanding of 5 IO models
- MATLAB小技巧(18)矩阵分析--熵权法
- ZABBIX deployment instructions (server+win client + switch (H3C))
- Associated GIS: all roads lead to ue5 City
- Pin details in rust
猜你喜欢
随机推荐
If xn > 0 and X (n+1) /xn > 1-1/n (n=1,2,...), Prove that the series Σ xn diverges
JVM常见的垃圾收集器
Lvgl GUI guide porting code to stm32
参考 | Win11 开启热点之后电脑不能上网
ServletConfig与ServletContext
MySQL lock details
【论文阅读】Intrinsically semi-supervised methods
The difference between ArrayList and LinkedList
關聯GIS:條條道路通UE5城
Reference | upgrade win11 mobile hotspot can not be opened or connected
Creation process and memory layout of objects at JVM level
八大误区,逐个击破(终篇):云难以扩展、定制性差,还会让管理员失去控制权?
CLassLoader
经典的一道面试题,涵盖4个热点知识
若xn>0,且x(n+1)/xn>1-1/n(n=1,2,...),证明级数∑xn发散
关联GIS:条条道路通UE5城
[12. maximum continuous non repeating subsequence]
【每日一练】产品卡片动画效果的实现
Etcd教程 — 第五章 Etcd之etcdctl的使用
不会初始化类的几种Case









