Jupyter Notebook的15个技巧和窍门,可简化您的编码体验
Jupyter Notebook is a browser bases REPL (read eval print loop) built on IPython and other open-source libraries, it allows us to run interactive python code on the browser.
Jupyter Notebook是基于IPL和其他開源庫構(gòu)建的基于REPL(讀取評估打印循環(huán))的瀏覽器,它使我們能夠在瀏覽器上運(yùn)行交互式python代碼。
It not only runs python code but also has many interesting plugins and magic commands which enhances the python coding experience greatly.
它不僅運(yùn)行python代碼,還具有許多有趣的插件和魔術(shù)命令,從而極大地增強(qiáng)了python的編碼體驗(yàn)。
1.計(jì)算單元執(zhí)行時(shí)間: (1. Calculate the time of execution of a cell:)
One can calculate the time of execution of a jupyter notebook cell using magic command at the beginning of the cell. It calculates the wall time that can be referred to as the total time required to execute that cell.
可以在單元格的開頭使用魔術(shù)命令來計(jì)算jupyter筆記本計(jì)算機(jī)單元的執(zhí)行時(shí)間。 它計(jì)算墻壁時(shí)間 ,可以將其稱為執(zhí)行該單元所需的總時(shí)間。
2.進(jìn)度欄: (2. Progress Bar:)
One can use a python external library to create a progress bar, that can give live updates of the progress of code. It keeps the user informed about the status of a running code script. You can get the Github repository of library here.
可以使用python外部庫創(chuàng)建進(jìn)度條,該進(jìn)度條可以實(shí)時(shí)更新代碼進(jìn)度。 它使用戶隨時(shí)了解正在運(yùn)行的代碼腳本的狀態(tài)。 您可以在此處獲取Github庫信息庫。
First, you need to install tqdm library,
首先,您需要安裝 tqdm庫,
pip3 install tqdmOr you can also install it in a jupyter notebook cell using ! .
或者,您也可以使用!將其安裝在jupyter筆記本電腦中! 。
The tqdm function can be used by importing its package and the usage and implementation can be observed below:
可以通過導(dǎo)入其軟件包來使用tqdm函數(shù),其用法和實(shí)現(xiàn)如下所示:
3.自動(dòng)代碼格式化程序: (3. Automatic Code Formatter:)
Using nb_black library, one can format a code snippet in a cell to a proper format. Sometimes the code snippet in a jupyter notebook cell is not well-formatted, this library helps to attain proper formatting of the code snippet.
使用nb_black庫,可以將單元格中的代碼片段格式化為適當(dāng)?shù)母袷健?有時(shí),在jupyter筆記本電腦單元中的代碼段格式不正確,該庫有助于實(shí)現(xiàn)代碼段的正確格式。
nb_black is a simple extension for Jupyter Notebook and Jupyter Lab to beautify Python code automatically.
nb_black是Jupyter Notebook和Jupyter Lab的簡單擴(kuò)展,可以自動(dòng)美化Python代碼。
Installation of the library:
庫的安裝:
pip3 install nb_blackUsage for Jupyter Notebook:
Jupyter Notebook的用法:
%load_ext nb_black(Image by Author), Unformatted cell(作者提供的圖片),未格式化的單元格 (Image by Author), Formatted cell using nb_black(作者提供的圖像),使用nb_black格式化的單元格4.安裝軟件包: (4. Install Packages:)
Jupyter Notebook can install any python package in the notebook itself. To install any python package using the pip command in jupyter notebook cell enter a ! before the command.
Jupyter Notebook可以在筆記本本身中安裝任何python軟件包。 要在jupyter筆記本單元中使用pip命令安裝任何python軟件包,請輸入! 在命令之前。
For installing the pandas package: Enter ! pip install pandas and run the cell.
要安裝pandas軟件包:輸入! pip install pandas ! pip install pandas并運(yùn)行單元。
5.說明文件: (5. Documentation:)
Jupyter Notebook can show that documentation of the function you are calling. Press Shift+Tab to view the documentation. This is very helpful as you don’t need to open the documentation website every single time. This feature also works for the local custom functions.
Jupyter Notebook可以顯示您正在調(diào)用的函數(shù)的文檔。 按Shift+Tab查看文檔。 這非常有用,因?yàn)槟恍枰看味即蜷_文檔網(wǎng)站。 此功能也適用于本地自定義功能。
Usage:
用法:
- Write the name of the function you want to implement 寫下您要實(shí)現(xiàn)的函數(shù)的名稱
Press Shift+Tab to view the documentation.
按Shift+Tab查看文檔。
Click on ^ on the top right corner of documentation to view it in a pager.
單擊文檔右上角的^以在尋呼機(jī)中查看它。
Click on + to grow the docstring vertically.
單擊+垂直增加文檔字符串。
Click on x to close the docstring.
單擊x關(guān)閉文檔字符串。
6.自動(dòng)完成: (6. Autocomplete:)
Jupyter Notebook can show suggestions for any function name or variable. To view suggestions writing typing the code press Tab in your keyboard and the suggestion will appear in a top-down menu. Press arrow-up or arrow-down key to scroll up or down the menu. You can also scroll using your mouse. Click on the keyword or hit enter on the selected keyword to confirm your suggestion.
Jupyter Notebook可以顯示有關(guān)任何函數(shù)名稱或變量的建議。 要查看鍵入代碼的建議,請按鍵盤上的Tab鍵,該建議將顯示在自上而下的菜單中。 按arrow-up或arrow-down鍵向上或向下滾動(dòng)菜單。 您也可以使用鼠標(biāo)滾動(dòng)。 單擊關(guān)鍵字或在選定的關(guān)鍵字上按Enter鍵以確認(rèn)您的建議。
You will also get suggestions for custom functions and variables.
您還將獲得有關(guān)自定義函數(shù)和變量的建議。
(Image by Author), Suggestions from the pandas package(作者提供的圖片),來自熊貓包裝的建議7.調(diào)整輸出視圖: (7. Adjust the view of Output:)
Jupyter Notebook can print the output of each cell just below the cell. When you have a lot of output you can reduce the amount of space it takes up by clicking on the left side panel of the output. This will turn the output into a scrolling window. Double click on the left side of the output to completely collapse the output panel.
Jupyter Notebook可以在單元格正下方打印每個(gè)單元格的輸出。 當(dāng)您有很多輸出時(shí),可以通過單擊輸出的左側(cè)面板來減少占用的空間。 這會(huì)將輸出轉(zhuǎn)換為滾動(dòng)窗口。 雙擊輸出的左側(cè)以完全折疊輸出面板。
You can repeat the process of a single click or double click to change the format of viewing the output panel.
您可以重復(fù)單擊或雙擊的過程來更改查看輸出面板的格式。
(Gif by Author), Click on the left side of the panel to change the view of the output screen(按作者顯示),點(diǎn)擊面板左側(cè)以更改輸出屏幕的視圖8.單元執(zhí)行功能: (8. Cell Execution Features:)
Jupyter Notebook has certain cell execution features that ease the programmer’s performance.
Jupyter Notebook具有某些單元執(zhí)行功能,可減輕程序員的性能。
- Shit+Enter will run the current cell and highlight the next cell, if no cell is present it will create a new cell. Shit + Enter將運(yùn)行當(dāng)前單元格并突出顯示下一個(gè)單元格,如果不存在任何單元格,則會(huì)創(chuàng)建一個(gè)新單元格。
- Alt+Enter will run the current cell and insert a new cell and highlight it. Alt + Enter將運(yùn)行當(dāng)前單元格并插入一個(gè)新單元格并突出顯示它。
9.降價(jià): (9. Markdown:)
Jupyter notebook cells can not only run code snippets but also be used to write text. Markdown cells can be used to write text descriptions. It is a better way to express than using comments.
Jupyter筆記本單元不僅可以運(yùn)行代碼段,還可以用于編寫文本。 Markdown單元格可用于編寫文本描述。 與使用注釋相比,這是一種更好的表達(dá)方式。
Usage:
用法:
- Click on the cell to convert it to markdown. 單擊單元格以將其轉(zhuǎn)換為降價(jià)。
- Choose the Markdown option from the drop-down menu 從下拉菜單中選擇Markdown選項(xiàng)
10.在筆記本中運(yùn)行來自不同語言的代碼: (10. Run code from different languages in the notebook:)
Jupyter Notebook cells can also be used to compile and run code from different languages using IPython magic commands. Use IPython Magics with the name of your kernel at the start of each cell that you want to use that cell for:
Jupyter Notebook單元還可以用于使用IPython magic命令編譯和運(yùn)行來自不同語言的代碼。 在每個(gè)要用于該單元格的單元格的開頭,將IPython Magics與內(nèi)核名稱一起使用:
%%bash
%%bash
%%HTML
%%HTML
%%python2
%%python2
%%python3
%%python3
%%ruby
%%ruby
%%perl
%%perl
11.多光標(biāo): (11. Multiple Cursor:)
Jupyter Notebook supports editing code using multiple cursors at once. To select the code to edit at once press Alt key and select the code snippet using your mouse. After selection, you can now edit the code using multiple cursors at once.
Jupyter Notebook支持一次使用多個(gè)光標(biāo)編輯代碼。 要立即選擇要編輯的代碼,請按Alt鍵,然后使用鼠標(biāo)選擇代碼段。 選擇后,您現(xiàn)在可以一次使用多個(gè)光標(biāo)編輯代碼。
12.從Jupyter Notebook創(chuàng)建演示文稿: (12. Creating Presentation from Jupyter Notebook:)
Jupyter Notebook can be used to create a PowerPoint-style presentation. Here each cell or group of cells of the notebook can be treated as each slide.
Jupyter Notebook可用于創(chuàng)建PowerPoint樣式的演示文稿。 在這里,筆記本的每個(gè)單元格或單元格組都可以視為每個(gè)幻燈片。
Firstly, install RISE using conda: conda install -c damianavila82 rise
首先,使用conda install -c damianavila82 rise安裝RISE : conda install -c damianavila82 rise
- Enter/Exit RISE Slideshow button appears in the notebook toolbar. A slideshow option will also appear under View>Cell Toolbar>Slideshow 輸入/退出RISE幻燈片顯示按鈕出現(xiàn)在筆記本工具欄中。 幻燈片選項(xiàng)也會(huì)出現(xiàn)在“視圖”>“單元格工具欄”>“幻燈片”下
- To prepare Slideshow click on View>Cell Toolbar>Slideshow and select the jupyter notebook cells for each slide. 要準(zhǔn)備幻燈片,請單擊“視圖”>“單元格工具欄”>“幻燈片”,然后為每張幻燈片選擇jupyter筆記本單元。
- After selecting each slide click on the RISE Slideshow button in the notebook toolbar. 選擇每張幻燈片后,單擊筆記本工具欄中的“ RISE幻燈片”按鈕。
Visit here for detailed video guide usage.
請?jiān)L問此處以獲取詳細(xì)的視頻指南用法。
Source)來源 )13.共享Jupyter筆記本: (13. Share Jupyter Notebooks:)
After code completion, you have several options to share your jupyter notebook.
代碼完成后,您可以選擇幾種方式來共享jupyter筆記本。
- Download your jupyter notebook as HTML, pdf, ipynb, py file, etc. 將您的jupyter筆記本下載為HTML,pdf,ipynb,py文件等。
You can use JupyterHub that can create a multi-user Hub which spawns, manages, and proxies multiple instances of the single-user Jupyter notebook server.
您可以使用JupyterHub創(chuàng)建一個(gè)多用戶集線器,該中心可以生成,管理和代理單用戶Jupyter筆記本服務(wù)器的多個(gè)實(shí)例。
You can publish to medium directly from the jupyter notebook. Read this to know the steps.
您可以直接從jupyter筆記本發(fā)布到中型媒體。 閱讀本文以了解步驟。
14.在筆記本中繪圖: (14. Plotting in Notebook:)
Jupyter Notebook is the best tool used for data analysis and visualization. It can be used to generate different types of plots using different python or R libraries. Some of the python libraries used to generate plots are:
Jupyter Notebook是用于數(shù)據(jù)分析和可視化的最佳工具。 它可以用于使用不同的python或R庫生成不同類型的圖。 用于生成圖的一些python庫是:
Matplotlib
Matplotlib
Seaborn
Seaborn
bokeh
散景
plot.ly
情節(jié)
15.鍵盤快捷鍵: (15. Keyboard Shortcuts:)
Shortcuts are used to save a lot of programmer’s time and ease the coding experience. Jupyter notebook has plenty of inbuilt keyboard shortcuts that you find under the Help menu bar: Help>Keyboard Shortcuts .
快捷方式用于節(jié)省大量程序員的時(shí)間并簡化編碼體驗(yàn)。 Jupyter Notebook具有許多內(nèi)置的鍵盤快捷鍵,您可以在“ Help菜單欄下找到:“ Help>Keyboard Shortcuts 。
Jupyter Notebook also provides functionality to edit the keyboard shortcuts as per the programmer’s convenience. You can edit keyboard shortcuts: Help>Edit Keyboard Shortcuts .
Jupyter Notebook還提供了根據(jù)程序員的方便來編輯鍵盤快捷鍵的功能。 您可以編輯鍵盤快捷鍵: Help>Edit Keyboard Shortcuts 。
(Image by Author), Command Mode Keyboard Shortcuts(作者提供的圖像),命令模式鍵盤快捷鍵 (Image by Author), Edit Mode Keyboard Shortcuts(作者提供的圖像),編輯模式鍵盤快捷鍵結(jié)論: (Conclusion:)
Jupyter Notebook is one of the best tools extensible used by folks working in the data science domain due to interactive UI. The above-discussed 15 tips and tricks will help you to ease your jupyter notebook coding experience. It has a lot more built-in magic commands that are not discussed in this article, you can have a read here. Let me know your favorite tips and comment if know more tricks.
由于交互式UI,Jupyter Notebook是數(shù)據(jù)科學(xué)領(lǐng)域的人們使用的最佳可擴(kuò)展工具之一。 上面討論的15個(gè)技巧和竅門將幫助您減輕Jupyter筆記本的編碼體驗(yàn)。 它具有許多內(nèi)置的魔術(shù)命令,本文不會(huì)討論,您可以在此處閱讀。 讓我知道您最喜歡的技巧,如果有更多技巧,請發(fā)表評論。
The images used in the article are either cited or generated by the author
本文中使用的圖像由作者引用或生成
Thank You for Reading
謝謝您的閱讀
翻譯自: https://towardsdatascience.com/15-tips-and-tricks-for-jupyter-notebook-that-will-ease-your-coding-experience-e469207ac95c
總結(jié)
以上是生活随笔為你收集整理的Jupyter Notebook的15个技巧和窍门,可简化您的编码体验的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到害虫什么预兆
- 下一篇: 梦到下血雨是什么征兆