Awesome jupyter for you!

- 1 note编辑快捷键

  • esc 进入特殊的'命令模式'
  • a 在当cell上面新建一个cell, b则是在下面
  • m 进入markdown编辑模式
  • dd 删除当前cell
  • ctrl + shift + - 从当前鼠标处拆分当前cell
  • shift + up/down或者shift + j/h 选中多个cell,
  • shift + m 合并多个选中的cell
In [1]:
import os

- 2 cell magic

In [9]:
%lsmagic
Out[9]:
Available line magics:
%alias  %alias_magic  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %man  %matplotlib  %mkdir  %more  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %popd  %pprint  %precision  %profile  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.
In [23]:
%pdb
Automatic pdb calling has been turned OFF
In [13]:
%%bash 
echo hhh
pwd
hhh
/notebook/deben
In [5]:
# 快速查文档
?str.replace
Docstring:
S.replace(old, new[, count]) -> str

Return a copy of S with all occurrences of substring
old replaced by new.  If the optional argument count is
given, only the first count occurrences are replaced.
Type:      method_descriptor

- 3 代码编辑

列编辑, alt+左键

In [27]:
x = [
    'one',
    'two',
    'three',
    'four'
]

命令和代码交互

In [26]:
flst = !ls *
flst[:4]
Out[26]:
['api_check.py', 'awesome-jupyter.ipynb', 'dnn_iris.py', 'test_tools.ipynb']

- 4 debug code

%pdb ``

new console for Notebook

In [ ]:
%pdb

def pick_and_take():
    picked = numpy.random.randint(0, 1000)
    raise NotImplementedError()

pick_and_take()

-5 save and load code

In [16]:
%%writefile myfile.py

import numpy
def append_if_not_exists(arr, x):
    if x not in arr:
        arr.append(x)

def some_useless_slow_function():
    arr = list()
    for i in range(10000):
        x = numpy.random.randint(0, 10000)
        append_if_not_exists(arr, x)
Overwriting myfile.py
In [7]:
%pycat myfile.py
import numpy
def append_if_not_exists(arr, x):
    if x not in arr:
        arr.append(x)

def some_useless_slow_function():
    arr = list()
    for i in range(10000):
        x = numpy.random.randint(0, 10000)
        append_if_not_exists(arr, x)
In [12]:
%load myfile.py

- 6 peformance debug

In [17]:
%prun some_useless_slow_function()
 
         26304 function calls in 0.945 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    10000    0.861    0.000    0.862    0.000 <ipython-input-15-180306546217>:4(append_if_not_exists)
    10000    0.067    0.000    0.067    0.000 {method 'randint' of 'mtrand.RandomState' objects}
        1    0.017    0.017    0.945    0.945 <ipython-input-15-180306546217>:8(some_useless_slow_function)
     6300    0.001    0.000    0.001    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.945    0.945 <string>:1(<module>)
        1    0.000    0.000    0.945    0.945 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}