Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

sjsong08

Jupyter server install 본문

개발 관련

Jupyter server install

SJSong 2018. 3. 29. 17:48

Jupyter 서버 설치 및 실행법


Data Sceince 분야와 과학분야에서 편하게 Python이 쓰이도록 개발된
Notebook에 대해서 살펴본다.

사용된 설치환경은 아래와 같다.

  • VMware 11
  • Ubuntu 14.04

실행 명령어 (개인 환경) -나의 경우 이대로 따라하면 된다-

source ~/tensorflow/bin/activate # virtualEnv 활성화를 통한 TensorFlow library 연동
#활성화 성공

#활성화 한다음 아래의 config 파일이 있는 디렉터리로 이동한다음 jupyter를 실행 해야 한다!!!
(tensorflow)root@jemin-virtual-machine:~/.jupyter# 
# jemin_config.py 환경으로 Jupyter 실행
# jemin_config.py는 SSL 설정을 Disable 한것이다.
jupyter notebook --config jemin_config.py

설치 및 실행

Ipython Notebook 보다 더 발전된 Interative IDE이다.
Jupyter가 다중 kernel을 지원한다고 한다.
즉, python 2,3 모두를 지원 한다.

python2는 pip이고 python3은 pip3이다.

pip install jupyter

실행 방법

jupyter notebook

도움말 실행 방법

(tensorflow)root@jemin-virtual-machine:~/.jupyter# jupyter notebook --h
usage: jupyter-notebook [-h] [--certfile NOTEBOOKAPP.CERTFILE]
                        [--ip NOTEBOOKAPP.IP] [--pylab [NOTEBOOKAPP.PYLAB]]
                        [--log-level NOTEBOOKAPP.LOG_LEVEL]
                        [--port-retries NOTEBOOKAPP.PORT_RETRIES]
                        [--notebook-dir NOTEBOOKAPP.NOTEBOOK_DIR]
                        [--config NOTEBOOKAPP.CONFIG_FILE]
                        [--keyfile NOTEBOOKAPP.KEYFILE]
                        [--port NOTEBOOKAPP.PORT]
                        [--transport KERNELMANAGER.TRANSPORT]
                        [--browser NOTEBOOKAPP.BROWSER] [--script] [-y]
                        [--no-browser] [--debug] [--no-mathjax] [--no-script]
                        [--generate-config]

optional arguments:
  -h, --help            show this help message and exit
  --certfile NOTEBOOKAPP.CERTFILE
  --ip NOTEBOOKAPP.IP
  --pylab [NOTEBOOKAPP.PYLAB]
  --log-level NOTEBOOKAPP.LOG_LEVEL
  --port-retries NOTEBOOKAPP.PORT_RETRIES
  --notebook-dir NOTEBOOKAPP.NOTEBOOK_DIR
  --config NOTEBOOKAPP.CONFIG_FILE
  --keyfile NOTEBOOKAPP.KEYFILE
  --port NOTEBOOKAPP.PORT
  --transport KERNELMANAGER.TRANSPORT
  --browser NOTEBOOKAPP.BROWSER
  --script
  -y, --y
  --no-browser
  --debug
  --no-mathjax
  --no-script
  --generate-config

주피터의 다중 커널 개념 지원은 매우 간단하고, 강력하다. IPython이 실행되는 파이썬 실행기에 종속되지 않기 때문에, 다른 언어들의 커널을 지원할 뿐만 아니라, 환경만 갖춰져 있다면 같은 언어의 다양한 버전에 대해서도 별도의 커널을 만들어 사용할 수 있다. 나아가 파이썬의 virtualenv와 결합하면 환경별로도 커널을 분리해 사용할 수 있다. 이와 같이 다중 커널 개념은 Jupyter의 핵심 개념이며, 이를 통해서 좀 더 자유롭게 Notebook 생활이 가능해질 것이다.

참고사이트: http://jupyter-notebook.readthedocs.org/en/latest/public_server.html

설정 방법

실행방법

jupyter notebook

시작 디렉터리 위치 변경

  • Window
    아이콘 오른쪽버튼 -> 시작 경로 변경

  • Linux
    To launch Jupyter Notebook App:

    • Click on spotlight, type terminal to open a terminal window.
    • Enter the startup folder by typing cd /some_folder_name.
    • Type ipython notebook to launch the Jupyter Notebook App (it will appear in a new browser window or tab).

원격 서버로 설정

우분투환경

Securing a notebook server

jupyter notebook --generate-config

profile 생성없이 defalut내용을 바로 변경한다. 전체 Jupyter에 영향을 미친다.

Writing default config to: /root/.jupyter/jupyter_notebook_config.py

Preparing a hashed passwrod

In [1]: from notebook.auth import passwd

In [2]: passwd()
Enter password: 
Verify password: 
Out[2]: 'sha1:f24baff49ac5:863dd2ae747212ede58125302d227f0ca7b12bb3'

jupyter_notebook_config.py를 열어서 아래의 내용을 입력한다.

# Password to use for web authentication
c = get_config()
c.NotebookApp.password =
u'sha1:f24baff49ac5:863dd2ae747212ede58125302d227f0ca7b12bb3'

패스워드를 사용할 때 SSL을 사용하는것은 좋은 생각이다.
SSL을 이용해서 브라우저에서 패스워드가 암호화되서 보내지기 때문이다.
이를 위해서 self-signed certificate를 생성 한다.

OpenSSL을 이용해서 certificate을 생성할 수 있다. 유효기간은 365일이다.

openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

jupyter_notebook_config.py를 열어서 아래의 내용을 입력한다.

c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'

서버 설정 내용을 jupyter_notebook_config.py를 열어서 아래의 내용을 입력한다.

# The IP address the notebook server will listen on.
# c.NotebookApp.ip = 'localhost'
c.NotebookApp.ip = '192.168.174.131'
# c.NotebookApp.port_retries = 50
c.NotebookApp.port_retries = 8888

또는 아래의 명령어로 수작업으로 설정해 줄수도 있다.

jupyter notebook --ip=* --no-browser

특정 config 파일로 실행 하기

명렁어 --config jemin_config.py를 설정해 준다.

(tensorflow)root@jemin-virtual-machine:~/.jupyter# jupyter notebook --config jemin_config.py 
[I 00:46:15.432 NotebookApp] Serving notebooks from local directory: /root/DataScience/
[I 00:46:15.432 NotebookApp] 0 active kernels 
[I 00:46:15.432 NotebookApp] The IPython Notebook is running at: http://192.168.174.132:8888/
[I 00:46:15.432 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

기타설정

#실행과 동시에 web-browser를 실행하지 않게 한다.
c.NotebookApp.open_browser = False
#시작 디렉터리를 변경한다.
c.NotebookApp.notebook_dir = u'/root/DataScience/'

c.NotebookApp.matplotlib='inline' 옵션은 사용하지 못하도록 변경되었다.
IPython shell에서 직접 magic 함수를 이용해야 한다.

%matplotlib inline

다중 Kernel 사용 하기

현재 설치된 커널 종류 확인
jupyter kernelspec list

(tensorflow_py2) jemin@jemin-desktop:~$ jupyter kernelspec list
Available kernels:
  python3    /home/jemin/.local/lib/python3.5/site-packages/ipykernel/resources

python2 커널 설치

python2 -m pip install ipykernel
python2 -m ipykernel install --user

커널 설치 확인

(tensorflow_py2) jemin@jemin-desktop:~$  jupyter kernelspec list
Available kernels:
  python3    /home/jemin/.local/lib/python3.5/site-packages/ipykernel/resources
  python2    /home/jemin/.local/share/jupyter/kernels/python2

추가 설정 자료
https://songyunseop.github.io/post/2016/09/Using-Jupyter-inside-virtualenv/

업데이트 방법

버전어 맞춰서 아래 명령어를 이용한다.
python 3.5는 pip3
python 2.7는 pip

pip install --upgrade pip # pip update

pip install -U jupyter

Trouble Shooting

1. IP 변경에 따른 에러

(tensorflow)root@jemin-virtual-machine:~/.jupyter# jupyter notebook --profile=jemin_config.py 
[W 20:34:44.745 NotebookApp] Unrecognized alias: '--profile=jemin_config.py', it will probably have no effect.

Traceback (most recent call last):
  File "/usr/local/bin/jupyter-notebook", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/jupyter_core/application.py", line 267, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 591, in launch_instance
    app.initialize(argv)
  File "<string>", line 2, in initialize
  File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 75, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/notebook/notebookapp.py", line 1007, in initialize
    self.init_webapp()
  File "/usr/local/lib/python2.7/dist-packages/notebook/notebookapp.py", line 873, in init_webapp
    self.http_server.listen(port, self.ip)
  File "/usr/local/lib/python2.7/dist-packages/tornado/tcpserver.py", line 126, in listen
    sockets = bind_sockets(port, address=address)
  File "/usr/local/lib/python2.7/dist-packages/tornado/netutil.py", line 196, in bind_sockets
    sock.bind(sockaddr)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Cannot assign requested address

2. SSL 사용 오류

config file에서 아래의 SSL 설정관련 코드들이 문제가 된다.
보안상 문제가 있지만 그냥 주석처리하고 실행 한다.

파일위치: /root/.jupyter

c.notebookApp.certfile = u'/root/.jupyter/mycert.crt'
c.notebookApp.certfile = u'/root/.jupyter/mycert.key'

아래는 에러 코드이다.

(tensorflow)root@jemin-virtual-machine:~/.jupyter# jupyter notebook --profile="jemin_config.py"
[W 22:43:24.515 NotebookApp] Unrecognized alias: '--profile=jemin_config.py', it will probably have no effect.
[W 22:43:24.520 NotebookApp] Error loading config file: jupyter_notebook_config
    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/jupyter_core/application.py", line 202, in load_config_file
        path=path
      File "<string>", line 2, in load_config_file
      File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 75, in catch_config_error
        return method(app, *args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 564, in load_config_file
        collisions = loaded[0].collisions(loaded[1])
      File "/usr/local/lib/python2.7/dist-packages/traitlets/config/loader.py", line 212, in collisions
        for key in mine:
    TypeError: 'LazyConfigValue' object is not iterable
[I 22:43:24.545 NotebookApp] Serving notebooks from local directory: /root/DataScience/
[I 22:43:24.545 NotebookApp] 0 active kernels 
[I 22:43:24.545 NotebookApp] The IPython Notebook is running at: http://192.168.174.132:8888/
[I 22:43:24.545 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

참고사이트:
http://jdfreder-notebook.readthedocs.org/en/docs/examples/Notebook/Configuring%20the%20Notebook%20and%20Server.html

공식사이트: http://jupyter-notebook.readthedocs.io/en/latest/public_server.html#using-lets-encrypt
다중 커널 설치법 (공식): http://ipython.readthedocs.io/en/stable/install/kernel_install.html



출처: http://goodtogreate.tistory.com/entry/IPython-Notebook-설치방법 [GOOD to GREAT]

'개발 관련' 카테고리의 다른 글

Ubuntu Disk Mount 관련  (0) 2018.03.29
Linux 폴더명 영문으로 바꾸기  (0) 2018.03.29
Ubuntu Remote Desktop  (0) 2018.03.29
tensorflow install  (0) 2018.03.29
ubuntu ssh 설정  (0) 2018.03.29
Comments