Jupyter
Start Jupyter notebook
ipython notebook
jupyter notebook
Run cell
Shift-Enter, implement the current cell and move to the next cell
Ctrl-Enter, implment the current cell and stay in the current cell
Alt-Enter, implement the current cell and insert a new cell below
Ipython Display System
from IPython.display import display
from IPython.display import Image
i = Image(filename='Python.jpeg'); # create an image from a filename
i2 = Image(url='http://www.ecsu.edu/_resources/images/logos/ecsulogo_original_450px.png'); # create an image from an url
display(i)
display(i2)
from IPython.display import YouTubeVideo
YouTubeVideo('_GtFn3h6uDM')
from IPython.core.display import HTML
HTML('http://www.google.com')
from IPython.display import Math
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')
from IPython.display import Latex
Latex(r"""\begin{align} e^{i\pi} + 1 = 0 \end{align}""")
%%latex
\begin{align} e^{i\pi} + 1 = 0 \end{align}
Latex under markdown mode
Inline expressions, $e^{i\pi} + 1 = 0$
Expressions on their own line, $$e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i$$
Code blocks
```python
import scipy
```
Ipython built-in commands
object?, print all sorts of details about any object
%alias, define an alias for a system command
%env, get the environment variables
%history, print input history
%load, load code into the current frontend
%lsmagic, list currently available magic functions
%pylab, load numpy and matplotlib to work interactively
%quickref, get the ipython reference
%run hello.py, run a python script
%timeit, measure the running time
%who, print all interactive variables
%%bash, run cells with bash in a subprocess
%%html, render the cell as a block of HTML
%%javascript, run the cell block of Javascript code
%%js, run the cell block of Javascript code
%%latex, render the cell as a block of latex
%%markdown, render the cell as Markdown text block
%%python2, %%python2 script magic
%%python3, %%python3 script magic
%%sh, %%sh script magic
%%writefile, Write the contents of the cell to a file
System shell commands
!, run command
!ping www.google.com, ping a ip address
d = !ls, save the outputs of the command to a variable
Caching system
Input caching system
- In, a list of cached inputs
Output caching system
- Out, a list of cached outputs
Checkpoint and revert
Click "Save" button to create a checkpoint
File -> Revert to Checkpoint
%autosave 1000 # automatically save every 1000 seconds
Convert Jupyter Notebook to Python Script
File -> Download as -> Python (.py)
Run Jupyter Notebook from Command Line
# support 'asciidoc', 'custom', 'html', 'latex', 'markdown', 'notebook', 'pdf', 'python', 'rst', 'script', 'slides', 'webpdf'
jupyter nbconvert --execute notebook.ipynb --allow-errors --to html
Jupyter Notebook Module
pip install import-ipynb
# p1.ipynb
def hello():
print('Hello World!')
# .ipynb or .py
import import_ipynb
import p1
p1.hello()
pip install ipynb
# p1.ipynb
def hello():
print('Hello World!')
# .ipynb or .py
import ipynb.fs.full.p1 as p
p.hello()
Jupyter Notebook Package
# p1.ipynb
def hello():
print('Hello World!')
# .ipynb or .py
import import_ipynb
import mypackage.p1 as p1
p1.hello()
# p1.ipynb
import import_ipynb
from mypackage.p2 import *
def hello():
print('Hello World, '+get_name())
# p2.ipynb
def get_name():
return 'Lin'
# .ipynb or .py
import import_ipynb
import mypackage.p1 as p1
p1.hello()
Reference