Jupyter教程

官网:

https://jupyter.org/

https://jupyter.org/try 试试 Jupyter

https://docs.jupyter.org/en/latest/

https://docs.jupyter.org/en/latest/reference/ipython.html Jupyter 内核

Jupyter Notebook是一个基于Web的交互式计算环境,允许用户在浏览器中创建和共享包含实时代码、数学方程、可视化图表和解释性文本的文档。它通过将代码、输出和叙述(Markdown)整合到统一的“笔记本”文件(.ipynb)中,非常适合数据科学、机器学习、数据清理和统计建模等任务,支持40多种编程语言。

子项目

https://docs.jupyter.org/en/latest/projects/content-projects.html

用法

https://docs.jupyter.org/en/latest/use/using.html

jupyter命令

https://docs.jupyter.org/en/latest/use/jupyter-command.html

命令 jupyter notebook 会启动 Jupyter 应用程序。jupyter 命令主要是一个子命令命名空间。例如,如果 PATH 环境变量中包含 jupyter-foo,则该命令会以子命令 jupyter foo 的形式运行。

除了启动 Jupyter 应用程序之外,jupyter 命令还可以用于执行其他操作。

1
2
3
4
5
6
7
8
9
10
11
12
# 显示帮助信息,包括可用的子命令。
jupyter -h

options:
-h, --help
--version 显示 Jupyter 核心包的版本
--config-dir 显示 Jupyter 配置目录
--data-dir 显示 Jupyter 数据目录
--runtime-dir 显示 Jupyter 运行时目录
--paths 显示所有 Jupyter 路径。添加 --json 可转换为机器可读格式。
--json 以机器可读的 JSON 格式输出路径
--debug 输出路径的调试信息

常用目录和文件位置

https://docs.jupyter.org/en/latest/use/jupyter-directories.html

Jupyter 将不同类型的文件(例如配置文件、数据文件、运行时文件)存储在多个不同的位置。可以通过设置环境变量来自定义每种文件类型的存储位置。

Jupyter 将数据文件(nbextensions、kernelspecs)与运行时文件(日志、pid 文件、连接文件)和配置文件(config 文件、custom.js)分开。

安装

1
2
3
4
# 将几个核心的 Jupyter 项目作为依赖项一次性安装到您的环境中。包括 notebook、jupyterlab、ipython、jupyter-console 等
pip install jupyter
#pip install notebook
#pip install jupyterlab

启动 Jupyter Notebook

常用子命令

1
2
3
4
5
6
7
8
# 用于设置或重置访问 Jupyter Notebook 服务器的密码,它会提示您输入并确认一个新密码,然后将加密后的密码存储在一个配置文件中
jupyter notebook password
# 列出当前用户在系统中所有正在运行的 Jupyter Notebook 服务器
jupyter notebook list
# 尝试关闭所有正在运行的 Jupyter Notebook 服务器
jupyter notebook stop
# 关闭指定端口的服务器
jupyter notebook stop 8888

固定访问token

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 方式一:环境变量固定 Token(推荐)
export JUPYTER_TOKEN='f644a0ceb6e3fd3fe7312e83b86bd1ac3462976d8ba8ef3d'
# 方式二:设置密码。(推荐)
jupyter notebook password # 效果:设置完成后,Jupyter 会生成一个哈希密码存入配置文件。只需在网页端输入设置的密码即可。
Enter password:
Verify password:
[JupyterPasswordApp] Wrote hashed password to /root/.jupyter/jupyter_server_config.json # 新版位置
[NotebookPasswordApp] Wrote hashed password to /root/.jupyter/jupyter_notebook_config.json # 旧版位置
# 方式三:命令行固定 Token(不推荐)
jupyter notebook --ServerApp.token='f644a0ceb6e3fd3fe7312e83b86bd1ac3462976d8ba8ef3d' # 新版参数
jupyter notebook --NotebookApp.token='f644a0ceb6e3fd3fe7312e83b86bd1ac3462976d8ba8ef3d' # 旧版参数
# 方式四:配置文件永久固定(不推荐)
jupyter notebook --generate-config # 1、生成配置文件,默认路径为 ~/.jupyter/jupyter_notebook_config.py
Writing default config to: '/root/.jupyter/jupyter_notebook_config.py'
c.ServerApp.token = '固定Token' # 2、修改配置文件(新版)
c.NotebookApp.token = '固定Token' # 2、修改配置文件(旧版)

前台启动

1
2
3
4
5
6
# 执行命令的当前目录作为 Notebook 的根目录(服务器将在当前目录启动)
jupyter notebook
# 指定 Notebook 服务器的根目录(服务器将在指定目录启动)
jupyter notebook --notebook-dir=/data/local/workspaces/mars-python/
# 监听所有可用的网络接口,指定服务器运行的端口号,允许以 root 用户身份运行。
jupyter notebook --ip=0.0.0.0 --port=8888 --allow-root

后台启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 方法一:借助 tmux。
# tmux new -s <name> 新建会话,name 为会话名称
# tmux detach 分离会话(会话继续后台运行)。或在会话内使用快捷键:前缀键默认是 Ctrl+b d。
# tmux ls 列出会话
# tmux a -t <name> 接入会话 (a 为 attach 简写)。tmux a 会接入最近离开的会话。
# tmux kill-session -t <name> 杀死会话
# tmux switch -t <name> 切换会话
# exit 退出会话
tmux new -s jupyteNotebook
# 方法二:借助 screen
# screen -S <name> 新建会话,name 为会话名称
# Ctrl+a d 分离会话(会话继续后台运行)。在会话内使用快捷键:前缀键默认是 Ctrl+a d。
# screen -ls 列出会话
# screen -r <id/name> 接入会话。恢复一个已脱离(Detached)的会话
# screen -dr <id/name> 强制脱离(Detach)并在当前窗口恢复该会话(适用于异地卡死的情况)
# screen -x <id/name> 多窗口共享:允许多人同时连接并操作同一个会话
# screen -X -S <id/name> quit 关闭会话。在普通终端,关闭一个正在后台运行的会话。
# exit 退出会话
screen -S jupyteNotebook
# 方法三:使用 nohup
nohup jupyter notebook --ip=0.0.0.0 --port=8888 --allow-root >./jupyteNotebook.log 2>&1 &

启动 jupyterlab

1
jupyter lab

运行 Notebook 脚本

1
2
3
4
5
6
# 在命令行中自动运行 Jupyter Notebook 中的所有单元格。默认情况下,它不会覆盖原文件,而是生成一个名为 your_notebook.nbconvert.ipynb 的新文件,其中包含执行后的结果。
jupyter nbconvert --execute your_notebook.ipynb
# 直接更新当前笔记本并保存运行结果,使用 --inplace 标志。
jupyter nbconvert --execute --inplace your_notebook.ipynb
# 忽略错误继续运行:默认情况下,如果某个单元格报错,执行会立即停止。使用 --allow-errors 可以让它跑完整个笔记本。
jupyter nbconvert --execute --allow-errors your_notebook.ipynb