Jupyter lab 사용

Published by onesixx on

https://jupyterlab.readthedocs.io/en/stable/
https://kibua20.tistory.com/184 
https://3months.tistory.com/392 [Deep Play]

로컬에서 다운로드 받기

raw_data = pd.read_csv("./voc_train.csv")
raw_data.to_csv('./voc.csv')

from IPython.display import FileLink
FileLink('./voc.csv')

.

로컬이 아닌, 원격서버에 Jupyter Lab이 설치되어 있다면,
인터넷되는 어디서라도 Web Browser를 통해 Python 개발이 가능하다.  

Shortcut

Cmd+ Shift + L : 새창

< 명령모드에서> ESC  : 편집모드에서 명령모드로 진입

  • [ENTER ]: 명령모드에서 셀 편집
  • [A] Add : 셀 추가 위
    [B] 아래
  • [DD] delete
  • [M]arkdown : 셀변환
    [Y] code
    [R]aw Cell
  • Shift + J or K [ Down or Up] 셀 선택
  • Shift + [M]erge 셀합치기

<편집모드에서> (커서위치 기준으로)

  • Ctrl + Shift + – (커서위치 기준으로) 셀나누기
  • Cmd+[ / ] (커서위치 기준 해당 라인) 주석처리 Ctrl + /
  • Cmd+[D] (커서위치 기준으로) 한줄 삭제
  • Cmd + [ 또는 ] Indentation
  • . + TAB : 함수목록
  • Shift + TAB : 해당함수 설명

*브라우저 단축키 모두 없애기.

Custom Shortcut

move cell up jupyter shortcut

Settings > Advanced Settings Editor > “Keyboard Shortcuts” 

{
    "shortcuts": [
        {
            "command": "notebook:move-cell-up",
            "keys": [
                "Ctrl Shift ArrowUp"
            ],
           "selector": "body"
        },
        {
            "command": "notebook:move-cell-down",
            "keys": [
                "Ctrl Shift ArrowDown"
            ],
            "selector": ".jp-Notebook:focus"
        },
    ]
}
{
    // Move cell up
    "notebook:move-cell-up": {
      "selector": ".jp-Notebook:focus",
      "command": "notebook:move-cell-up",
      "keys": [
        "Ctrl Shift ArrowUp"
      ]
    },

    // Move cell down
    "notebook:move-cell-down": {
      "selector": ".jp-Notebook:focus",
      "command": "notebook:move-cell-down",
      "keys": [
        "Ctrl Shift ArrowDown"
      ]
    }
}

추가 바로실행가능 Ipython 콘솔 창

명시적으로 매번 print()를 쓰지 않고, 연속적으로 값을 출력

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
import numpy as np 
import pandas as pd

df = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))
lst = np.arange(10)

df
lst
"END"
np.arange(10)**2

	A	B	C
0	-0.141083	-1.584214	0.070834
1	-0.674680	0.300282	-0.483465
2	0.216068	0.211681	-0.412534
3	-0.992280	-0.363932	2.183695
4	0.875325	0.406591	-0.509874
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
'END'
array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])

 항상 반영하기 위해서는 configuration file내에 아래 코드 넣어준다.

c.InteractiveShell.ast_node_interactivity = "all"

! %

! : 리눅스 명령어 . 독립된 shell ex) !git clone https://github.com/open-mmlab/detection.git
% : 리눅스 명령어. 현재 shell ex) %cd detection

Markdown font color

some *blue* text

Jupyter Lab 매직 기능 (Magic function)

https://ipython.readthedocs.io/en/stable/interactive/magics.html 해당 링크

%lsmagic 매직 기능에 어떤것들이 있는지 출력해준다. 

  1. %matplotlib inline 플롯을 화면 안에서 보여준다.
  2. %env
    • %env 모든 환경변수를 출력한다.
    • %env var 해당 이름의 환경변수를 출력한다.
    • (%env var val) or (%env var=val) 환경변수를 설정한다.
  3. %run
    • %run file_name 해당 이름의 .py 파일 또는 .ipynb 파일을 셀 안에서 실행한다. 
  4. %load
    • %load source 해당 파일을 셀 안에 로드한다.
  5. %who = will list all variables that exist in the global scope. It can be used to see what all data_frames or any other variable is there in memory.
    • %who: 현재 전역 환경의 모든 변수를 리스트한다. (메모리에 어떤 변수들이 올라와 있나 확인할 수 있다.)
    • %who df: 현재 선언된 dataframe 을 볼 수 있다. 
    • %whos: %who 와 비슷하지만 각 변수들에 대해 상세한 설명을 볼 수 있다. 
  6. %time 한 셀이 실행된 시간을 볼 수 있다. 
  7. %timeit 10만 번 실행하여 평균 시간을 잰다. 
  8. %writefile
    • %writefile file_name 해당 파일의 셀의 아웃풋을 쓴다.
    • %writefile -a file_name 해당 파일의 셀의 아웃풋을 덧붙인다.

python using external source

global.py

If you add to .gitignore:

.ipynb_checkpoints

(no slashes anywhere), any file or directory in the repo with that name will be ignored. Paths are only checked if you include /.

From this answer you can also have a global gitignore for your computer:

git config --global core.excludesfile '~/.gitignore'
echo '.ipynb_checkpoints' >> ~/.gitignore

.ipynb 변환

https://pypi.org/project/ipynb-py-convert/

설치

$pip install ipynb-py-convert

.py to .ipynb

$ipynb-py-convert mypython.py    mynotebook.ipynb

셀구분 # %%
Markdown ”’ 을 사용한 주석

.ipynb to .py

$ipynb-py-convert mynotebook.ipynb    mypython.py

$jupyter nbconvert --to script mynotebook.ipynb   mypython.py
$jupyter nbconvert --to html   mynotebook.ipynb   mypython.html
Categories: Python Basic

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x