jupyter notebook安装

一、进入虚拟环境(python3.8):

(base) [root@localhost ~]# conda activate python

前置条件:已安装Java环境

二、pip 安装

(python) [root@localhost ~]# pip install jupyter

Collecting jupyter
  Downloading jupyter-1.0.0-py2.py3-none-any.whl (2.7 kB)
Collecting notebook
  Downloading notebook-6.1.5-py3-none-any.whl (9.5 MB)
     |████████████████████████████████| 9.5 MB 329 kB/s
Collecting jupyter-console
  Downloading jupyter_console-6.2.0-py3-none-any.whl (22 kB)

...

Successfully built pandocfilters pyrsistent
Installing collected packages: ipython-genutils, traitlets, jupyter-core, ptyprocess, tornado, terminado, argon2-cffi, pyzmq, python-dateutil, jupyter-client, pygments, parso, jedi, pickleshare, backcall, pexpect, wcwidth, prompt-toolkit, decorator, ipython, ipykernel, prometheus-client, pandocfilters, testpath, jupyterlab-pygments, mistune, MarkupSafe, jinja2, defusedxml, pyparsing, packaging, webencodings, bleach, pyrsistent, jsonschema, nbformat, entrypoints, nest-asyncio, async-generator, nbclient, nbconvert, Send2Trash, notebook, jupyter-console, widgetsnbextension, ipywidgets, qtpy, qtconsole, jupyter
Successfully installed MarkupSafe-1.1.1 Send2Trash-1.5.0 argon2-cffi-20.1.0 async-generator-1.10 backcall-0.2.0 bleach-3.2.1 decorator-4.4.2 defusedxml-0.6.0 entrypoints-0.3 ipykernel-5.3.4 ipython-7.19.0 ipython-genutils-0.2.0 ipywidgets-7.5.1 jedi-0.17.2 jinja2-2.11.2 jsonschema-3.2.0 jupyter-1.0.0 jupyter-client-6.1.7 jupyter-console-6.2.0 jupyter-core-4.6.3 jupyterlab-pygments-0.1.2 mistune-0.8.4 nbclient-0.5.1 nbconvert-6.0.7 nbformat-5.0.8 nest-asyncio-1.4.2 notebook-6.1.5 packaging-20.4 pandocfilters-1.4.3 parso-0.7.1 pexpect-4.8.0 pickleshare-0.7.5 prometheus-client-0.8.0 prompt-toolkit-3.0.8 ptyprocess-0.6.0 pygments-2.7.2 pyparsing-2.4.7 pyrsistent-0.17.3 python-dateutil-2.8.1 pyzmq-19.0.2 qtconsole-4.7.7 qtpy-1.9.0 terminado-0.9.1 testpath-0.4.4 tornado-6.1 traitlets-5.0.5 wcwidth-0.2.5 webencodings-0.5.1 widgetsnbextension-3.5.1

三、产生配置文件

(python) [root@localhost ~]# jupyter notebook --generate-config

命令执行后,在当前目录下会产生隐藏目录.jupyter,该目录下有配置文件jupyter_notebook_config.py

(python) [root@localhost ~]# ls .jupyter
jupyter_notebook_config.py

四、配置密码登录

默认为令牌token登录,可修改为密码登录:

(python) [root@localhost .jupyter]# jupyter notebook password
Enter password: 123456
Verify password: 123456
[NotebookPasswordApp] Wrote hashed password to /root/.jupyter/jupyter_notebook_config.json

(python) [root@localhost .jupyter]# vi jupyter_notebook_config.json

{
  "NotebookApp": {
    "password": "argon2:$argon2id$v=19$m=10240,t=10,p=8$5hT5eAGBOAk5SDu3HvSviQ$4jNmHMdsmYCSCpOj9jjM+w"
  }
}

五、运行

(1)修改配置文件,允许远程访问和root用户访问

(python) [root@localhost .jupyter]# vi jupyter_notebook_config.py

...

c.NotebookApp.allow_root = True                    #允许root用户访问

...

c.NotebookApp.ip = '*'                                     #允许远程访问

...

c.NotebookApp.open_browser = False          #启动jupyter notebook时,不打开浏览器

...

修改完成后,运行jupyter notebook命令即可。(注意:配置文件中参数修改后,必须重启jupyter notebook,所作修改才能起作用)

(2)不用修改配置文件,在运行命令中带上参数:

(python) [root@localhost ~]# jupyter notebook --no-browser --ip=0.0.0.0 --allow-root

也可编制运行脚本文件:

(python) [root@localhost ~]# vi jupyternotebook.sh

jupyter notebook --no-browser --ip=0.0.0.0 --allow-root

(python) [root@localhost ~]# chmod +x jupyternotebook.sh

(python) [root@localhost ~]# ./jupyternotebook.sh
[I 15:34:19.556 NotebookApp] 启动notebooks 在本地路径: /root
[I 15:34:19.556 NotebookApp] Jupyter Notebook 6.1.5 is running at:
[I 15:34:19.556 NotebookApp] http://localhost.localdomain:8888/
[I 15:34:19.556 NotebookApp] 使用control-c停止此服务器并关闭所有内核(两次跳过确认).

六、修改防火墙

(base) [root@localhost ~]# firewall-cmd --add-port=8888/tcp --permanent
success

(python) [root@localhost ~]# firewall-cmd --list-ports
445/tcp 8888/tcp

七、安装jupyter nbextensions扩展

(python) [root@localhost ~]# pip install jupyter_contrib_nbextensions
Collecting jupyter_contrib_nbextensions
  Downloading jupyter_contrib_nbextensions-0.5.1-py2.py3-none-any.whl (20.9 MB)

...

Successfully built jupyter-latex-envs pyyaml jupyter-nbextensions-configurator
Installing collected packages: jupyter-latex-envs, jupyter-highlight-selected-word, pyyaml, jupyter-contrib-core, jupyter-nbextensions-configurator, jupyter-contrib-nbextensions
Successfully installed jupyter-contrib-core-0.3.3 jupyter-contrib-nbextensions-0.5.1 jupyter-highlight-selected-word-0.2.0 jupyter-latex-envs-1.4.6 jupyter-nbextensions-configurator-0.4.1 pyyaml-5.3.1

(python) [root@localhost ~]#  jupyter contrib nbextension install

...

[I 17:30:49 InstallContribNbextensionsApp]       - Validating: OK
[I 17:30:49 InstallContribNbextensionsApp] - Editing config: /usr/local/etc/jupyter/jupyter_nbconvert_config.json
[I 17:30:49 InstallContribNbextensionsApp] --  Configuring nbconvert template path
[I 17:30:49 InstallContribNbextensionsApp] --  Configuring nbconvert preprocessors
[I 17:30:49 InstallContribNbextensionsApp] - Writing config: /usr/local/etc/jupyter/jupyter_nbconvert_config.json
[I 17:30:49 InstallContribNbextensionsApp] --  Writing updated config file /usr/local/etc/jupyter/jupyter_nbconvert_config.json

安装code prettify配套插件yapf:

(python) [root@localhost ~]# pip install yapf
Collecting yapf
  Downloading yapf-0.30.0-py2.py3-none-any.whl (190 kB)
     |████████████████████████████████| 190 kB 600 kB/s
Installing collected packages: yapf
Successfully installed yapf-0.30.0

 八、退出jupyter notebook

执行Ctrl+c或Ctrl+z退出,可能无法完全退出,需要使用命令ss -plant | grep 8888查看jupyter的pid号,再使用kill -9 pid号命令完全退出。

九、使jupyter notebook保持虚拟终端中后台运行

方法1:使用nohup功能

(python) [root@localhost ~]# nohup ./jupyternotebook.sh&

方法2:使用screen命令

原文地址:https://www.cnblogs.com/sfccl/p/13948895.html