---
# System prepended metadata

title: Jupyter Notebook

---

# Jupyter Notebook 

## 安裝

``` shell
$ pip install jupyter # Python 2 - pip, Python 3 - pip3
$ conda install jupyter # anaconda or miniconda 
```

## 插件安裝
* [參考資料](https://github.com/ipython-contrib/jupyter_contrib_nbextensions)
* 以 anaconda 當範例
```shell
$ conda install -c conda-forge jupyter_contrib_nbextensions
$ jupyter contrib nbextension install --user
```

* 我打開的插件
	- Hinterland 
	- Latex
	- Pyhton MarkDown
	- Codefolding 
	- Header (看個人，我覺得很醜ＸＤ)
![](https://i.imgur.com/DOJnwPD.png)


## Jupyter 遠端連線
* [參考文件](https://coderwall.com/p/ohk6cg/remote-access-to-ipython-notebooks-via-ssh)
```shell
---> Localhost
$ ssh -Y username@lab.ip -p portNum 
---> Server
$ screen
$ jupyter notebook --no-browser --port=8998
---> Local
$ ssh -N -f -L localhost:8888:localhost:8889 remote_user@remote_host -p portNume
=== 連不上的情形 ===
* port 被佔用
---> Local 
$ ps aux | grep "8998"
$ kill number # number(你找到的數字)
* 其他
```
```shell
=== Screen 相關指令 ===
* 退出 screen
Step 1: Ctrl + a
Step 2: d
* 重新命名該 screen
Step 1: Ctrl + a
Step 2: 打 :sessionname Jupyter #(你喜歡的名字)
* 重新進去到該 screen
Step 1: screen -r (Tab) 
* 離開 screen
$ exit 
```

```shell
=== tmux version ===
* new session
$ tmux
$ tmux new -s [session_name]

* rename the session
$ tmux rename-session -t [original_name] [new_name]

* attach to an existed session
$ tmux attach -t [session_name]

* dettach the tmux session
$ Ctrl+b, then d

* kill the session
$ tmux kill-session -t [session_name]

* list the existed sessions
$ tmux ls

* shortcut in the session
$ Ctrl+b, then % (open a new vertical pane)
$ Ctrl+b, then " (open a new horizon pane)
$ Ctrl+b, then x (close a pane)
$ Ctrl+b, then direction keys (move within panes)
```
 
 ## PDB (Python Debugger)
 
 ```python
# 直接在code中打pdb.set_trace()
# 跑程式之後便會停在那裡
# 可以看變數當下數值
x = 1
pdb.set_trace()
x = x + 1
```
```shell
Terminal 執行程式碼之後：
$ python handsome_alex.py
(Pdb) x 
Output: 1
    * 如果要執行下一行打n，全部結束打c(或跑到下一個pdb.set_trace())
(Pdb) n
(Pdb) x
Output: 2
    * 也可以在pdb中打任意指令
(Pdb) x**2
Output: 4
 ```
