jupyter lab ์„ค์น˜

Published by onesixx on

PythonShell์ธ REPL (Read-Evaluate-Print-Loop)์„ ์‚ฌ์šฉ
Ipython = pythonShell + ๊ฐœ์„ ์  (Tab Completion, Code Highlighting, Shortcut, Magic Command ๋“ฑ)
==[Web Applicaiton, S/C]==> Jupyter Notebook
Jupyter Lab = Jupyter Notebook + ๊ฐœ์„ ์ (๋‹ค์ค‘ ํƒญ ์ง€์›, ๋‹ค์–‘ํ•œ ํ™•์žฅ ๊ธฐ๋Šฅ…)

miniconda ์„ค์น˜

https://onesixx.com/miniconda

(๊ฐ€์ƒํ™˜๊ฒฝ์—) Jupyterlab์„ค์น˜

(Base) ~$โžœ  conda activate sixxDL


(sixxDL) ~$โžœ  pip install jupyterlab
#$ conda install -c conda-forge jupyterlab
# ์ด๋ฏธ์„ค์น˜๋˜์–ด ์žˆ์Œ
(sixxDL) ~$โžœ  conda list jupyter
# packages in environment at /Users/onesixx/Library/r-miniconda/envs/sixxDL:
#
# Name                    Version                   Build  Channel
jupyter-client            6.1.12                   pypi_0    pypi
jupyter-core              4.7.1                    pypi_0    pypi
jupyter-server            1.8.0                    pypi_0    pypi
jupyterlab                3.0.16                   pypi_0    pypi
jupyterlab-pygments       0.1.2                    pypi_0    pypi
jupyterlab-server         2.5.2                    pypi_0    pypi

์‹คํ–‰

(base) ~$โžœ  conda activate sixxDL
(sixxDL) ~$โžœ  jupyter lab

Jupyter lab ์„ค์ • ํŒŒ์ผ

~/.jupyter/ ์•„๋ž˜ jupyter_lab_config.py ํŒŒ์ผ์ƒ์„ฑ

~$โžœ  jupyter lab --generate-config
Writing default config to: /Users/onesixx/.jupyter/jupyter_lab_config.py

~$โžœ  subl /Users/onesixx/.jupyter/jupyter_lab_config.py
~$โžœ  jupyter lab --config=<์›ํ•˜๋Š”ํŒจ์Šค>
###### ์™ธ๋ถ€ ์ ‘์† ํ—ˆ์šฉ 170
#------------------------------------------------------------------------------
# ServerApp(JupyterApp) configuration
#------------------------------------------------------------------------------
...
#  Takes precedence over allow_origin_pat.
c.ServerApp.allow_origin = '*'
#  Local IP addresses (such as 127.0.0.1 and ::1) are allowed as local, along
#  with hostnames configured in local_hostnames.
c.ServerApp.allow_remote_access = True
## Whether to allow the user to run the server as root.
c.ServerApp.allow_root = True

#------------------------------------------------------------------------------
# ServerApp(JupyterApp) configuration
#------------------------------------------------------------------------------
...

## Specify what command to use to invoke a web browser when starting the server.
#  If not specified, the default browser will be determined by the `webbrowser`
#  standard library module, which allows setting of the BROWSER environment
#  variable to override it.
# WHALE์„ ๊ธฐ๋ณธ ๋ธŒ๋ผ์šฐ์ €๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„๋•Œ.
# c.ServerApp.browser = '/Applications/Whale.app/Contents/MacOS/Whale --app=%s'
c.ServerApp.browser = '/Applications/Whale.app/Contents/MacOS/Whale %s'

####### start whale http://onesixx.com  
# /Applications/Whale.app/Contents/MacOS/Whale --app=http://onesixx.com
# "C:\Program Files (x86)\Naver\Naver Whale\Application\whale.exe"
# /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --app=http://onesixx.com


###### ip:port 315
## The IP address the Jupyter server will listen on.
c.ServerApp.ip = 'localhost'
## The port the server will listen on (env: JUPYTER_PORT).
c.ServerApp.port = 7777

###### ๋ธŒ๋ผ์šฐ์ € ์ž๋™ ๊ตฌ๋™ (default browser false) 377
## Whether to open in a browser after starting. The specific browser used is
#  platform dependent and determined by the python standard library `webbrowser`
#  module, unless it is overridden using the --browser (ServerApp.browser)
#  configuration option.
#c.ServerApp.open_browser = False

###### ํ† ํฐ ์ž…๋ ฅ
c.ServerApp.token = ''
#$ jupyter lab --ip=0.0.0.0 --port=7777 &
$ jupyter lab &
$ kill $(ps aux |grep jupyter | awk '{print $2}')

ํ™˜๊ฒฝ์„ค์ •

Code์˜ ๊ธ€์žํฌ๊ธฐ ๋ณ€๊ฒฝ

๊ฐ€์ƒํ™˜๊ฒฝ ์„ ํƒ

Jupyter lab์— ๊ฐ€์ƒํ™˜๊ฒฝ Kernel ์—ฐ๊ฒฐํ•˜๊ธฐ 

conda install ipykernel

python -m ipykernel install –user –name <๊ฐ€์ƒํ™˜๊ฒฝ๋ช…> [–display-name “VirtualEnvforPy”]

python -m ipykernel install --user --name Torch --display-name "Torch"

conda remove๋กœ ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์‚ญ์ œํ•ด์„œ kernel์€ ๋‚จ๋Š”๋‹ค.

conda remove –name <์ปค๋„๋ช…> –all
jupyter kernelspec list # ์ปค๋„์ŠคํŽ™๋ชฉ๋ก์—์„œ ์‚ญ์ œํ•œ ์ปค๋„๋ช… ํ™•์ธ
jupyter kernelspec uninstall <์ปค๋„๋ช…>

์œ ์šฉํ•œ Extension

Extension ๊ธฐ๋Šฅ์€ใ„ด nodejs๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

Mac

$ brew install node

Ubuntu 

$ sudo apt install nodejs

 sudo apt install npm

$ node -v 

  โ†’ v10.19.0  (๋ฒ„์ „ ํ™•์ธ)

$ sudo npm cache clean -f

  โ†’ cache ์‚ญ์ œ

$ sudo npm install -g n

  โ†’ nodejs ๋ฒ„์ „ ๊ด€๋ฆฌ ํ”Œ๋Ÿฌ๊ทธ์ธ (n) ์„ค์น˜

$ sudo n lts 

  โ†’  nodejs์˜ LTS ๋ฒ„์ „ (v14.17.0)  ์„ค์น˜ ,  n stable, n latest ๋ช…๋ น์–ด๋กœ ์›ํ•˜๋Š” ๋ฒ„์ „ ์„ค์น˜ ๊ฐ€๋Šฅ

nodejs์„ ์ตœ์‹  ๋ฒ„์ „์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ›„์—๋Š” Jupyter Lab Extenstion์€ ์™ผ์ชฝ ์‚ฌ์ด๋“œ๋ฐ”์— ํ•˜๋‹จ Extension ์•„์ด์ฝ˜์„ ์„ ํƒ ํ›„ Extension ๊ฒ€์ƒ‰ ๋ฉ”๋‰ด๋ฅผ ํ†ตํ•ด์„œ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  Server Extension์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์„œ๋ฒ„์—์„œ ์„ค์น˜ํ•˜๋ฉด ๊ตฌ๋™์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 

1. Git ์—ฐ๋™ @jupyterlab/git

git ์—ฐ๋™, branch๊ด€๋ฆฌ ๋“ฑ. ์™ผ์ชฝ ์‚ฌ์ด๋“œ ๋ฉ”๋‰ด์— Git ๋ฉ”๋‰ด ์ถ”๊ฐ€๋จ 
์„ค์น˜ ๋ช…๋ น์–ด: 
$ sudo pip3 install jupyterlab-git 
(sixxDL) ~$โžœ pip install –upgrade jupyterlab jupyterlab-git
(sixxDL) ~$โžœ conda install -c conda-forge jupyterlab jupyterlab-git

Jupyterlab-git ์‹คํ–‰ ํ™”๋ฉด

2. Table of content ์ƒ์„ฑ @jupyterlab/toc-extension

md ํŒŒ์ผ์— ๋Œ€ํ•œ ๋ชฉ์ฐจ ์ƒ์„ฑ   JupyterLab < 3.0 ์ดํ•˜ ๋ฒ„์ „์—์„œ๋Š” ์•„๋ž˜ extension ์„ค์น˜ํ•ด์•ผ ํ•˜๊ณ , 3.0 ์ด์ƒ ๋ฒ„์ „์€ JupyterLab core์— ์ด๋ฏธ ํฌํ•จ๋˜์–ด ์žˆ์Œ

์„ค์น˜ ๋ช…๋ น์–ด: 
$ sudo jupyter labextension install @jupyterlab/toc

Jupyterlab-toc ์‹คํ–‰ ํ™”๋ฉด

3. Go To Definition ๊ธฐ๋Šฅ @krassowski/jupyterlab_go_to_definition

Alt + click to jump to a definition
Ctrl + Alt + B keyboard-only

์„ค์น˜ ๋ช…๋ น์–ด
$ sudo jupyter labextension install @krassowski/jupyterlab_go_to_definition

4.  Audo Complete @krassowski/jupyterlab-lsp

Code ์ž๋™ ์™„์„ฑ
์„ค์น˜ ๋ช…๋ น์–ด:
$ sudo pip3 install jupyterlab-lsp

์œ ์šฉํ•œ extension ์†Œ๊ฐœ

awesome jupyter (jupyter extensions ์†Œ๊ฐœ)

extension ์„ค์น˜๋ฐฉ๋ฒ•

jupyterlab extension์„ ์„ค์น˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” nodejs๊ฐ€ ํ•„์ˆ˜๋กœ ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

์ฐธ๊ณ  : How to Install Node.js and npm on Ubuntu 20.04 & 18.04

CLI๋ฅผ ํ†ตํ•œ ์„ค์น˜

jupyter labextension install [ extension ๋ช…]
jupyter_ext_2

JupyterLab์—์„œ ์„ค์น˜

Settings ์„ค์ •์„ ํ•ด์ฃผ๋ฉด, ์›ํ•˜๋Š” extension์„ ๊ฒ€์ƒ‰ํ•ด์„œ ๊ฐ„๋‹จํžˆ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

jupyter_ext_1

0. ์ผ๋ฐ˜์ ์ธ ์šฉ๋„์˜ extension

extension ๋ช…์„ค ๋ช…
jupyter-widgets/jupyterlab-manageripywidget ์‹คํ–‰์„ ์œ„ํ•œ extension
jupyterlab/jupyterlab-toctable of contents ๊ธฐ๋Šฅ์„ ์œ„ํ•œ extension
krassowski/jupyterlab-go-to-definitiongo to definition ๊ธฐ๋Šฅ์„ ์œ„ํ•œ extension
dnanhkhoa/nb_blackblack formatter ์ž๋™์ ์šฉ์„ ์œ„ํ•œ extension
kiteco/jupyterlab-kiteKite Autocomplete Extension for JupyterLab
hadim/jupyter-archive์ž‘์—…์ค‘์ธ ํด๋” ๋‹ค์šด๋กœ๋“œ(zip) ๊ธฐ๋Šฅ์„ ์œ„ํ•œ extension

0-1. jupyterlab-execute-time

github.com/deshaw/jupyterlab-execute-time

1.0.0 ๊ธฐ์ค€

์…€์„ ์‹คํ–‰ํ•œ ์‹œ๊ฐ„๊ณผ ๊ฒฝ๊ณผํ•œ ์‹œ๊ฐ„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์„ค์น˜ ํ›„ settings์—์„œ recordTiming์„ true๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

jupyter_exec_time

1. jupyterlab-system-monitor

github.com/jtpio/jupyterlab-system-monitor

0.4.1 ๊ธฐ์ค€

jupyterlab์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ƒฅ ์„ค์น˜๋งŒ ํ•˜๋ฉด ๋‹จ์ˆœ ์‚ฌ์šฉ๋Ÿ‰๋งŒ ์ˆซ์ž๋กœ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๋ ค๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ ์ œํ•œ์„ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. ํ˜„์žฌ, ์‹ค์ œ๋กœ ์ž‘๋™ํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. ๊ทธ๋ƒฅ ์ž„์˜์˜ ์„ค์ •๊ฐ’์ผ ๋ฟ์ด๋‹ค.

jupyter_notebook_config.py์— ์•„๋ž˜ ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.

( config๋ฅผ ๊ทธ๋™์•ˆ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด jupyter lab โ€“generate-config๋กœ ๋จผ์ € ์ƒ์„ฑํ•œ๋‹ค. )

๊ธฐ๋ณธ๊ฐ’์œผ๋กœ 5์ดˆ(5000ms)๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ ์—…๋ฐ์ดํŠธ ๋˜๋Š”๋ฐ Settings์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

#-----------------------------------------------------------
# NotebookApp(JupyterApp) configuration
#----------------------------------------------------------

c.NotebookApp.ResourceUseDisplay.mem_limit= (Size Of GB) *1024*1024*1024
jupyter_ext_5

์ฐธ๊ณ  : open_browser=True (default option) ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ๋ธŒ๋ผ์šฐ์ € ์‹คํ–‰ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํฌํ•จ๋  ์ˆ˜ ์žˆ๋‹ค.

jupyter_ext_6

2. jupyterlab-googledrive

github.com/jupyterlab/jupyterlab-google-drive

1.0 ๊ธฐ์ค€

๊ตฌ๊ธ€ ๋“œ๋ผ์ด๋ธŒ์— ํŒŒ์ผ ์—…๋กœ๋“œ ๋ฐ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•ด์ค€๋‹ค. ๋…ธํŠธ๋ถ ํŒŒ์ผ์„ ํด๋ผ์šฐ๋“œ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ์šฉ๋„๋กœ ์œ ์šฉํ•˜๋‹ค.

googleapi9

์•„๋ž˜ Post์—์„œ ์„ค์น˜ ํ›„ ์—ฐ๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Post – JupyterLab๊ณผ GoogleDrive ์—ฐ๋™ํ•˜๊ธฐ

3. nbdime – Diff and Merge tool

github.com/jupyter/nbdime

Notebook ํŒŒ์ผ๋กœ Diff and Merge ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์ค€๋‹ค.

2.0.0 ๊ธฐ์ค€
pip install nbdime
jupyter serverextension enable --py nbdime
jupyter labextension install nbdime-jupyterlab
  • nbdiff ๋ช…๋ น์–ด๋กœ ์ˆ˜์ •ํ•œ ๋‚ด์šฉ์„ ํ™•์ธ ๊ฐ€๋Šฅ
nbdime_1
  • git repo๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด git์˜ ์ •๋ณด์™€ ํ˜„์žฌ ์ €์žฅํ•œ ๋…ธํŠธ๋ถ์„ ๋น„๊ต ๊ฐ€๋Šฅ
nbdime_2

์ฐธ๊ณ  : Working with nbdime – in JupyterLab Quick Start Guide

4. jupyterlab-github

github.com/jupyterlab/jupyterlab-github

1.0 ๊ธฐ์ค€

github์— ์˜ฌ๋ฆฐ ๋…ธํŠธ๋ถ ํŒŒ์ผ์„ ์ฐธ๊ณ ํ•˜๋Š” ์šฉ๋„๋กœ ์ ํ•ฉํ•˜๋‹ค. github์— ์—…๋กœ๋“œ ๋œ ํŒŒ์ผ์„ ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. ๋‹จ, ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์—…๋กœ๋“œ๋Š” ๋ถˆ๊ฐ€ํ•˜๋‹ค.

๋กœ์ปฌ ๋…ธํŠธ๋ถ์œผ๋กœ ์…€์„ ๋ณต์‚ฌํ•˜๊ณ  ๋ถ™์—ฌ๋„ฃ๊ธฐ๋Š” ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜๋ฆ„ ์œ ์šฉํ•˜๋‹ค.

USER ๋ช…/repo ๋ช…์œผ๋กœ ๊ฒ€์ƒ‰ ๊ฐ€๋Šฅํ•˜๋‹ค.

jupyter_ext_3

5. lckr/jupyterlab_variableinspector

github.com/lckr/jupyterlab-variableInspector

0.3 ๊ธฐ์ค€
jupyter labextension install @lckr/jupyterlab_variableinspector

RStudio์ฒ˜๋Ÿผ inspector ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ•˜๋Š” extension์ด๋‹ค. ํœด์ง€ํ†ต ์•„์ด์ฝ˜์œผ๋กœ ๋ณ€์ˆ˜ ์‚ญ์ œ๋ฅผ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

Name์„ ํด๋ฆญํ•˜๋ฉด array๋‚˜ dataframe์€ value๋ฅผ ํ…Œ์ด๋ธ” ํ˜•ํƒœ๋กœ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

์‚ฌ์šฉ๋ฐฉ๋ฒ• : ๋…ธํŠธ๋ถ ํŒŒ์ผ ๋นˆ ๊ณต๊ฐ„์—์„œ ์šฐํด๋ฆญ ํ›„ Open Variable Inspector๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ƒˆ๋กœ ์ฐฝ์ด ๋‚˜ํƒ€๋‚œ๋‹ค.

jupyter_ext_4

6. pbugnion/jupyterlab-sql

github.com/pbugnion/jupyterlab-sql

0.3.1 ๊ธฐ์ค€

์„ค์น˜๋ฐฉ๋ฒ•
pip install jupyterlab_sql
jupyter serverextension enable jupyterlab_sql --py --sys-prefix
jupyter lab build

# PostgreSQL ์—ฐ๊ฒฐ์„ ์œ„ํ•œ psycopg2 ์„ค์น˜
pip install psycopg2-binary

# MySQL ์—ฐ๊ฒฐ์„ ์œ„ํ•œ pymysql ์„ค์น˜
pip install PyMySQL
pip install cryptography

์„ค์น˜ ํ›„ ์žฌ์‹คํ–‰ํ•˜๋ฉด SQL ์•„์ด์ฝ˜์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ณ  ์—ฐ๊ฒฐํ•  DB ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

jupyter_sql_1
PostgreSQL - postgres://user:password@ip:port/database
MySQL - mysql+pymysql://user:password@ip:port/database

์—ฐ๊ฒฐํ•˜๋ฉด DB์˜ ํ…Œ์ด๋ธ”์ด ๋ณด์ด๊ณ  ๋”๋ธ”ํด๋ฆญํ•˜๋ฉด 1000๊ฐœ๊นŒ์ง€์˜ row๋ฅผ ํ™•์ธํ•  ์ˆ˜์žˆ๋‹ค.

jupyter_sql_2

Custom SQL query๋ฅผ ํด๋ฆญํ•˜์—ฌ ์ฟผ๋ฆฌ๋ฌธ์„ ์ž…๋ ฅํ•˜๋ฉด(Ctrl+Enter) ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ์ดํ„ฐ ๋ณต์‚ฌ ๋“ฑ์˜ ์ถ”๊ฐ€๊ธฐ๋Šฅ์€ ํ˜„์žฌ ์—†๋‹ค.

jupyter_sql_3

DB์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ„๋‹จํžˆ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋„์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๋…ธํŠธ๋ถ ํŒŒ์ผ์—์„œ ํ™œ์šฉ๋˜๋Š” ipython-sql์ด ์•„์ง์€ ๋” ์œ ์šฉํ•ด๋ณด์ธ๋‹ค.



Categories: Python Basic

onesixx

Blog Owner

Subscribe
Notify of
guest

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