jupyter-notebook, IPython, jupyter-lab

jupyter-notebook是一个用以编程python代码的笔记本环境。

具有安全机制,其认证令牌(token)会在启动过程中在打印到标准输出。

Install 安装

# install with conda
conda install -c conda-forge jupyterlab
# install with pip
pip install jupyterlab
# classic notebook
#pip install jupyter    # or pip install notebook

Jupyter notebook的运行需要ipython kernels,必要时(如在notebook web界面中找不到可用kernel时)手动安装ipython kernel,若报错no module named IPython.paths,则升级ipython(通过pip或conda)。

启动服务

jupyter-lab
# binding all network interfaces
jupyter-lab --ip 0.0.0.0
# with listening port, a new port with higher number will be used if the given port is already in use
jupyter-lab --port 8080
# with listening port and no retrying new ports
jupyter-lab --port 8080 --port-retries=0
# classical notebook
#jupyter-notebook   # or jupyter notebook

默认服务地址: http://127.0.0.1:8888

common used commands:
# list running servers for 'jupyter-lab' and 'jupyter-notebook'
jupyter server list
jupyter server stop     # stop the server listened on the default port
jupyter server stop <PORT>  # stop the server listened on the given port
A .desktop file for startup jupyter notebook

Windows更新classic notebook启动快捷方式来启动jupyter-lab:
通过查看快捷方式的属性可知其最终通过%CONDA_HOME%\Scripts\jupyter-notebook-script.py启动,修改该.py文件,原本的内容为:

import re
import sys
from notebook.notebookapp import main
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

修改为

import re
import sys
from jupyterlab.labapp import main
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

修改jupyter-lab工作空间目录为用户主目录:
jupyter-lab默认工作目录为~/Documents,可将其修改为同classic notebook的~,首先运行命令

jupyter notebook --generate-config

其会生成一个.py文件~/.jupyter/jupyter-notebook-config.py,打开该文件编辑,找到这一行c.NotebookApp.notebook_dir = ,取消该行注释,编写python代码使其指向到用户主目录,如

import pathlib
c.NotebookApp.notebook_dir = str(pathlib.Path().home())
#或者
import os
c.NotebookApp.notebook_dir = os.path.expanduser('~')

保存,启动jupyter-lab即可生效。

代码自动补全
尽管内置可通过补全的功能,但其比较简单。通过安装和配置nbextensions来实现比之更丰富的代码自动补全功能。

pip install jupyter_contrib_nbextensions

#jupyter notebook服务需处于关闭状态,再执行以下命令
jupyter contrib nbextension install --user --skip-running-check

在jupyter notebook主页(http://127.0.0.1:8888)下的菜单栏,在内置的菜单"Files", "Running", "Clusters"旁增加一个"Nbextensions",点击之,从其中展示的列表中启用"HinterLand"。

主题
安装依赖包jupyterthemes

pip install jupyterthemes

jt -l       #列举可用主题

jt -t <主题名> -T -N
#其中参数
#   -T:打开顶部的工具栏
#   -N:显示笔记本的名字

jt -r       #恢复默认主题

FAQ

RuntimeError while importing, path must be None or a list
Solution: install or update spyder.

posted @ 2022-07-06 23:29  二球悬铃木  阅读(123)  评论(0编辑  收藏  举报