jupyter实验环境启动

常用的Jupyter实验环境有Jupyter Lab和Jupyter Notebook. Jupyter Lab中除了提供Jupyter Notebook, 还提供了更方便的文件和进程管理, 以及Console工具. 但无论使用哪种实验环境, 它们的配置和启动方法几乎是一模一样的.

创建配置文件

使用以下两个指令任意一个都可以, 且创建的同一个文件, 同时影响Lab和Notebook:

jupyter notebook --generate-config
jupyter lab --generate-config
garnet@DESKTOP-DH5TCF8:~/.pip$ jupyter lab --generate-config
Writing default config to: /home/garnet/.jupyter/jupyter_notebook_config.py

编辑配置文件

只说明几个重要字段.

网络设置

## The IP address the notebook server will listen on.
c.NotebookApp.ip = '0.0.0.0'

## The port the notebook server will listen on.
c.NotebookApp.port = 8888

默认使用localhost域名. 如果要暴露给内网其他机器使用, 将ip地址设置为0.0.0.0或本机对应的ip地址.

Root权限

可以在启动Jupyter服务时使用--allow-root参数开启root权限, 也可以直接在配置文件中设置.

## Whether to allow the user to run the notebook as root.
c.NotebookApp.allow_root = True

目录

实验环境的根目录.

## The directory to use for notebooks and kernels.
c.NotebookApp.notebook_dir = '/home/user/notebook'

密码

首先在Python环境中查看指定密码加密后的结果:

garnet@DESKTOP-DH5TCF8:~$ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from notebook.auth import passwd
>>> passwd()
Enter password:
Verify password:
'sha1:6cd6bac1f498:58710eca31eebd1c3937125135a81836b43e9916'
>>>

这里输入的密码是123456, 得到加密的结果. 将加密后的字符串写入jupyter的配置文件中:

## Hashed password to use for web authentication.
#
#  To generate, type in a python/IPython shell:
#
#    from notebook.auth import passwd; passwd()
#
#  The string should be of the form type:salt:hashed-password.
c.NotebookApp.password = 'sha1:6cd6bac1f498:58710eca31eebd1c3937125135a81836b43e9916'

然后在浏览器里第一次打开实验环境时, 就需要输入密码进入.

浏览器

是否在启动环境成功后, 自动在浏览器里打开实验环境. 默认是打开的, 最好关闭掉.

## 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 (NotebookApp.browser)
#  configuration option.
c.NotebookApp.open_browser = False

启动Jupyter

使用nohup后台启动Jupyter服务, 注意日志输出.

nohup jupyter lab > /home/garnet/log/jupyter.log 2>&1 &

最后更新于