Jupyter 是個集成文本,數學公式,代碼和可視化的可分享文本。html
Notebooks 很快已經成爲了數據操做不可或缺的工具。它在 大數據清理和探究,可視化,機器學習, 和 大數據分析中都有普遍運用.python
Notebooks 能夠直接在github直接被讀取. 這是一個很是有用的功能,你能夠方便地分享。例如, 自編碼器notebooklinux
Notebooks是Donald Knuth 1984年提出的[文本化編程]的一種形式(http://www.literateprogrammin... 。結合文本化編程, 文本和代碼交錯在一塊兒,而不是分紅兩個獨立地本分。git
代碼文檔是給人寫的,不是給電腦寫的。 這會給別人看你的代碼或者你本身回頭分析你以前的成果都提供了很大的便利。github
Eve真正試圖將文本化編程開發成一種完整的語言.express
From Jupyter documentation
這個 中心點 是 notebook server. 你經過瀏覽器來鏈接notebook這個渲染好的網頁應用. 你寫的代碼經過server傳給kernel。 kernel執行代碼再經過server傳給瀏覽器。當你保存文件爲.ipynb
後綴的JSON 文本 。
這種架構最好的特色是kernel能夠不是基於特定於Python的,你能夠運行任何語言。 例如你也能夠運行R和 Julia 。因此jupyter再也不叫Ipython Notebook。 Jupyter 是 Julia, Python, 和 R的組合. 若是你感興趣,你能夠瀏覽這個 支持的kernel列表.編程
另一個好處是你能夠經過網絡在任何一個地方訪問Server。特別的是,你能夠在本身的電腦上訪問數據存儲的地方並運行代碼。 你能夠在一個遠程服務器上執行代碼 。瀏覽器
這個特徵確實頗有用。服務器
Anaconda是安裝Jupyter最簡便的方式,自帶Jupyter.markdown
在Conda中安裝,能夠用 conda install jupyter notebook
.
Jupyter也能夠用pip安裝 pip install jupyter notebook
.
在控制檯或者teminal裏面啓動Notebook jupyter notebook
. Jupyter須要命令行來啓動。 你在哪一個路徑下啓動jupyter,文本就保存在哪一個路徑下。
當你啓動jupyter,它的主頁會在瀏覽器中自動打開。 默認地址爲http://localhost:8888. localhost
指你的本機,而不是網絡中其餘電腦。8888
是你鏈接的端口. 只要server是在運行的, 你均可以在瀏覽器中訪問 http://localhost:8888 。
若是你另外開了一個server,它會默認想去啓動 8888端口,但由於已經佔用了,它會去默認開啓端口8889. 你能夠訪問http://localhost:8889.
例如這樣:
你看到的文件列表取決於你在哪裏啓動。
咱們從點擊「新建」開始建立notebook,文本文件,文件夾或terminal。這個按鈕下的列表顯示了你已經安裝的kernel種類。咱們如今建立Python 3環境。你能夠看到我還裝了Scala 2.
若是你安裝了kernel,還會涉及到選擇哪一個環境。例如:
conda environments in Jupyter
頂部的標籤有 Files, Running, 和 Cluster. Files 如今有哪些文件. Running 標籤列出瞭如今運行的 notebooks.
Clusters 指多kernel並行計算. 它由 [ipyparallel]支持(https://ipyparallel.readthedo... ) 。這裏就很少講了。
若是你在運行Conda環境,那麼還有一個"Conda" 標籤. 你能夠在這裏安裝管理包和環境。
你能夠保存文件後點擊按鈕 "Shutdown"關閉單個notebook。
另外你能夠在保存後在server裏面按ctrl/command + C 來關閉server。
Feel free to try this yourself and poke around a bit.
綠色的一個方框叫cell. Cells是你寫代碼和執行的地方。 你能夠修改爲文本而後用 Markdown來渲染。
當你運行代碼時,左邊有數字顯示運行的次序 像In [1]:
。
從左向右
The little keyboard is the command palette. This will bring up a panel with a search bar where you can search for various commands. This is really helpful for speeding up your workflow as you don't need to search around in the menus with your mouse. Just open the command palette and type in what you want to do. For instance, if you want to merge two cells:
At the top you see the title. Click on this to rename the notebook.
Over on the right is the kernel type (Python 3 in my case) and next to it, a little circle. When the kernel is running a cell, it'll fill in. For most operations which run quickly, it won't fill in. It's a little indicator to let you know longer running code is actually running.
Along with the save button in the toolbar, notebooks are automatically saved periodically. The most recent save is noted to the right of the title. You can save manually with the save button, or by pressing escape then s on your keyboard. The escape
key changes to command mode and s
is the shortcut for "save." I'll cover command mode and keyboard shortcuts later.
In the "File" menu, you can download the notebook in multiple formats. You'll often want to download it as an HTML file to share with others who aren't using Jupyter. Also, you can download the notebook as a normal Python file where all the code will run like normal. The Markdown
and reST
formats are great for using notebooks in blogs or documentation.
Most of your work in notebooks will be done in code cells. This is where you write your code and it gets executed. In code cells you can write any code, assigning variables, defining functions and classes, importing packages, and more. Any code executed in one cell is available in all other cells.
To give you some practice, I created a notebook you can work through. Download the notebook Working With Code Cells then run it from your own notebook server. (In your terminal, change to the directory with the notebook file, then enter jupyter notebook
) Your browser might try to open the notebook file without downloading it. If that happens, right click on the link then choose "Save Link As..."
As mentioned before, cells can also be used for text written in Markdown. Markdown is a formatting syntax that allows you to include links, style text as bold or italicized, and format code. As with code cells, you press Shift + Enter or Control + Enter to run the Markdown cell, where it will render the Markdown to formatted text. Including text allows you to write a narrative along side your code, as well as documenting your code and the thoughts that went into it.
You can find the documentation here, but I'll provide a short primer.
Headers
You can write headers using the pound/hash/octothorpe symbol #
placed before the text. One #
renders as an h1 header, two #
s is an h2, and so on. Looks like this:
# Header 1 ## Header 2 ### Header 3
renders as
Linking in Markdown is done by enclosing text in square brackets and the URL in parentheses, like this [Udacity's home page](https://www.udacity.com)
for a link to Udacity's home page.
You can add emphasis through bold or italics with asterisks or underscores (*
or _
). For italics, wrap the text in one asterisk or underscore, _gelato_
or *gelato*
renders as gelato.
Bold text uses two symbols, **aardvark**
or __aardvark__
looks like aardvark.
Either asterisks or underscores are fine as long as you use the same symbol on both sides of the text.
There are two different ways to display code, inline with text and as a code block separated from the text. To format inline code, wrap the text in backticks. For example, `string.punctuation` renders as string.punctuation.
To create a code block, start a new line and wrap the text in three backticks
import requests response = requests.get('https://www.udacity.com')
or indent each line of the code block with four spaces.
import requests response = requests.get('https://www.udacity.com')
You can create math expressions in Markdown cells using LaTeX symbols. Notebooks use MathJax to render the LaTeX symbols as math symbols. To start math mode, wrap the LaTeX in dollar signs $y = mx + b$
for inline math. For a math block, use double dollar signs,
$$ y = \frac{a}{b+c} $$
This is a really useful feature, so if you don't have experience with LaTeX please read this primer on using it to create math expressions.
Here's a cheatsheet you can use as a reference for writing Markdown. My advice is to make use of the Markdown cells. Your notebooks will be much more readable compared to a bunch of code blocks.
Notebooks come with a bunch of keyboard shortcuts that let you use your keyboard to interact with the cells, instead of using the mouse and toolbars. They take a bit of time to get used to, but when you're proficient with the shortcuts you'll be much faster at working in notebooks. To learn more about the shortcuts and get practice using them, download the notebook Keyboard Shortcuts. Again, your browser might try to open it, but you want to save it to your computer. Right click on the link, then choose "Save Link As..."
With keyboard shortcuts, it is quick and simple to switch between Markdown and code cells. To change from Markdown to cell, press Y
. To switch from code to Markdown, press M
.
A lot of times it is helpful to number the lines in your code for debugging purposes. You can turn on numbers by pressing L
(in command mode of course) on a code cell.
Deleting cells is done by pressing D
twice in a row so D
, D
. This is to prevent accidently deletions, you have to press the button twice!
You can easily access the command palette by pressing Shift + Control/Command + P
.
Note: This won't work in Firefox and Internet Explorer unfortunately. There is already a keyboard shortcut assigned to those keys in those browsers. However, it does work in Chrome and Safari.
This will bring up the command palette where you can search for commands that aren't available through the keyboard shortcuts. For instance, there are buttons on the toolbar that move cells up and down (the up and down arrows), but there aren't corresponding keyboard shortcuts. To move a cell down, you can open up the command palette and type in "move" which will bring up the move commands.
Magic keywords are special commands you can run in cells that let you control the notebook itself or perform system calls such as changing directories. For example, you can set up matplotlib to work interactively in the notebook with %matplotlib
.
Magic commands are preceded with one or two percent signs (% or %%) for line magics and cell magics, respectively. Line magics apply only to the line the magic command is written on, while cell magics apply to the whole cell.
NOTE: These magic keywords are specific to the normal Python kernel. If you are using other kernels, these most likely won't work.
At some point, you'll probably spend some effort optimizing code to run faster. Timing how quickly your code runs is essential for this optimization. You can use the timeit
magic command to time how long it takes for a function to run, like so:
If you want to time how long it takes for a whole cell to run, you’d use %%timeit
like so:
As mentioned before, notebooks let you embed images along with text and code. This is most useful when you’re using matplotlib
or other plotting packages to create visualizations. You can use %matplotlib
to set up matplotlib for interactive use in the notebook. By default figures will render in their own window. However, you can pass arguments to the command to select a specific "backend", the software that renders the image. To render figures directly in the notebook, you should use the inline backend with the command %matplotlib inline
.
Tip: On higher resolution screens such as Retina displays, the default images in notebooks can look blurry. Use%config InlineBackend.figure_format = 'retina'
after%matplotlib inline
to render higher resolution images.
With the Python kernel, you can turn on the interactive debugger using the magic command %pdb
. When you cause an error, you'll be able to inspect the variables in the current namespace.
Above you can see I tried to sum up a string which gives an error. The debugger raises the error and provides a prompt for inspecting your code.
Read more about pdb
in the documentation. To quit the debugger, simply enter q
in the prompt.
There are a whole bunch of other magic commands, I just touched on a few of the ones you'll use the most often. To learn more about them, here's the list of all available magic commands.
Notebooks are just big JSON
files with the extension .ipynb
.
Notebook file opened in a text editor shows JSON data
Since notebooks are JSON, it is simple to convert them to other formats. Jupyter comes with a utility called nbconvert
for converting to HTML, Markdown, slideshows, etc.
For example, to convert a notebook to an HTML file, in your terminal use
jupyter nbconvert --to html notebook.ipynb
Converting to HTML is useful for sharing your notebooks with others who aren't using notebooks. Markdown is great for including a notebook in blogs and other text editors that accept Markdown formatting.
As always, learn more about nbconvert
from the documentation.
Create slideshows from notebooks is one of my favorite features. You can see an example of a slideshow here introducing Pandas for working with data.
The slides are created in notebooks like normal, but you'll need to designate which cells are slides and the type of slide the cell will be. In the menu bar, click View > Cell Toolbar > Slideshow to bring up the slide cell menu on each cell.
This will show a menu dropdown on each cell that lets you choose how the cell shows up in the slideshow.
Slides are full slides that you move through left to right. Sub-slides show up in the slideshow by pressing up or down. Fragments are hidden at first, then appear with a button press. You can skip cells in the slideshow with Skip and Notes leaves the cell as speaker notes.
To create the slideshow from the notebook file, you'll need to use nbconvert
:
jupyter nbconvert notebook.ipynb --to slides
This just converts the notebook to the necessary files for the slideshow, but you need to serve it with an HTTP server to actually see the presentation.
To convert it and immediately see it, use
jupyter nbconvert notebook.ipynb --to slides --post serve
This will open up the slideshow in your browser so you can present it.
Create slideshows from notebooks is one of my favorite features. You can see an example of a slideshow here introducing Pandas for working with data.
The slides are created in notebooks like normal, but you'll need to designate which cells are slides and the type of slide the cell will be. In the menu bar, click View > Cell Toolbar > Slideshow to bring up the slide cell menu on each cell.
This will show a menu dropdown on each cell that lets you choose how the cell shows up in the slideshow.
Slides are full slides that you move through left to right. Sub-slides show up in the slideshow by pressing up or down. Fragments are hidden at first, then appear with a button press. You can skip cells in the slideshow with Skip and Notes leaves the cell as speaker notes.
To create the slideshow from the notebook file, you'll need to use nbconvert
:
jupyter nbconvert notebook.ipynb --to slides
這個命令並不能生成直接能夠查看的slides
This just converts the notebook to the necessary files for the slideshow, but you need to serve it with an HTTP server to actually see the presentation.
To convert it and immediately see it, use
jupyter nbconvert notebook.ipynb --to slides --post serve
This will open up the slideshow in your browser so you can present it.這樣能夠直接用Jupyter製做幻燈片,很實用