juypter notebook tips
===
###### tags: `python` `jupyter-notebook`
juypter tips
===
# hot keys
1. esc+f:find and replace
2. esc+o:折疊輸出結果
3. ctrl+shift+p:show hot keys
# 基本知識
* edit -p:回到上一個編輯的file
* \[file\]?:的到該object 的說明
* \[file\]??:印出該obj的source code
* %quickref:快速介紹
* help(object):得到該obj的詳細說明
## 快捷建

# multiple outpus
```python
line1 = "this is from line 1"
line2 = "this is from line 2"
line1
line2
```
'this is from line 2'
```python
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
```
```python
line1
line2
```
'this is from line 1'
'this is from line 2'
Can be modified in `~/.ipython/profile_default/ipython_config.py`
```
c = get_config()
# Run all nodes interactively
c.InteractiveShell.ast_node_interactivity = "all"
```
# easy link to document
```python
?sum()
```
# magic commands
```python
%lsmagic
```
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 %%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.
## set environment variable
`%env loli="loli"`
## run python
`Jupyter Magic - %run:Excute python code`
可以用來執行py or ipynb
`%run ./LinearRegression.ipynb`
```python
%who
```
InteractiveShell line1 line2
## timing
`%%time`列出cell的執行時間
`%timeit`執行段code100次,然後取最快的三次的平均值(使用python的timeit module)
```python
%%time
import time
for _ in range(1000):
time.sleep(0.01)
```
CPU times: user 64 ms, sys: 32 ms, total: 96 ms
Wall time: 10.2 s
```python
import numpy
% timeit numpy.random.normal(size=100)
```
9.23 µs ± 216 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
```python
%prun print('hello')
```
hello
%prun:Show how much time your program spent in each function
%pdb:debugging with pdb
# Run code from a different kernel in a notebooks
%%bash:bash kernel
%%HTML
%%python2,3
```bash
%%bash
for i in {1..5}
do
echo "$i"
done
```
1
2
3
4
5
# color notes boxes
<div class="alert alert-block alert-info">info</div>
<div class="alert alert-block alert-warning">warning</div>
<div class="alert alert-block alert-success">success</div>
<div class="alert alert-block alert-danger">danger</div>
# horzithonal line
***
# font color
`<font color=blue|red|green|pink|yellow>Text</font>`
<font color=pink> Loli</font>