当前位置:网站首页>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 .
边栏推荐
- If xn > 0 and X (n+1) /xn > 1-1/n (n=1,2,...), Prove that the series Σ xn diverges
- MySQL index details
- IO管脚配置和pinctrl驱动
- Object含有Copy方法?
- 静态代码块Vs构造代码块
- Redis的事务
- Modify the contents of /etc/crontab file directly, and the scheduled task will not take effect
- MySQL索引详解
- 关于放大器失真的原因你了解多少呢?
- Matlab tips (19) matrix analysis -- principal component analysis
猜你喜欢
随机推荐
Understanding mvcc in MySQL transactions is super simple
SPARQL基础入门练习
0号进程,1号进程,2号进程
Pin details in rust
支付宝微信支付业务流程图
[11. two dimensional difference]
Binary tree structure and heap structure foundation
Helix QAC更新至2022.1版本,将持续提供高标准合规覆盖率
【每日一练】产品卡片动画效果的实现
March into machine learning -- Preface
Redis installation under Linux
How Oracle converts strings to multiple lines
[batch dos-cmd command - summary and summary] - how to distinguish the internal command and external command of CMD, and the difference between CMD command and run (win+r) command,
Etcd教程 — 第五章 Etcd之etcdctl的使用
05 observer mode
How much do you know about the cause of amplifier distortion?
L'enquête en aveugle a montré que les femmes étaient meilleures que les hommes.
2022.6.26-----leetcode. seven hundred and ten
CLassLoader
Rust async: SMOL source code analysis -executor









