一、什么是Jupyter Notebook?
Jupyter Notebook是基于网页的用于交互计算的应用程序。其可被应用于全过程计算:开发、文档编写、运行代码和展示结果。
——Jupyter Notebook官方介绍
https://jupyter-notebook.readthedocs.io/en/stable/notebook.html
二、安装Jupyter Notebook
使用命令安装
conda install ipykernel
或者
pip install jupyter notebook
三、运行Jupyter Notebook
3.1 帮助
jupyter notebook --help
# 同上
jupyter notebook -h
3.2 启动
# 默认启动端口8888
jupyter notebook
# 指定端口启动
jupyter notebook --port 8889
# 指定工作目录启动
jupyter notebook --notebook-dir .
比如我在E盘下创建一个目录study,进入base环境,启动jupyter,端口是8889,指定目录命令行接入的目录
cd E:\study
conda activate base
jupyter notebook --port 8889 --notebook-dir .
在命令行终端显示内容
[I 12:21:04.177 NotebookApp] [jupyter_nbextensions_configurator] enabled 0.6.3
[W 12:21:04.903 NotebookApp] Loading JupyterLab as a classic notebook (v6) extension.
[W 2023-11-20 12:21:04.911 LabApp] 'port' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[W 2023-11-20 12:21:04.911 LabApp] 'notebook_dir' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[W 2023-11-20 12:21:04.911 LabApp] 'notebook_dir' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[I 2023-11-20 12:21:04.919 LabApp] JupyterLab extension loaded from D:\tool\anaconda\Lib\site-packages\jupyterlab
[I 2023-11-20 12:21:04.919 LabApp] JupyterLab application directory is D:\tool\anaconda\share\jupyter\lab
[I 12:21:12.314 NotebookApp] Serving notebooks from local directory: E:\study
[I 12:21:14.111 NotebookApp] Jupyter Notebook 6.5.4 is running at:
[I 12:21:14.126 NotebookApp] http://localhost:8889/?token=7c619de75e5ed07516912c78784f01af3637f4cd023ba5d4
[I 12:21:14.126 NotebookApp] or http://127.0.0.1:8889/?token=7c619de75e5ed07516912c78784f01af3637f4cd023ba5d4
[I 12:21:14.126 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 12:21:14.236 NotebookApp]
注意:之后在Jupyter Notebook的所有操作,都请保持终端不要关闭,因为一旦关闭终端,就会断开与本地服务器的链接,你将无法在Jupyter Notebook中进行其他操作啦。
如果你同时启动了多个Jupyter Notebook,有的端口被占用,可以通过指定不同的端口进行启动。
四、配置Jupyter Notebook
常规的情况下,Windows和Linux/macOS的配置文件所在路径和配置文件名如下所述:
-
Windows系统的配置文件路径:C:\Users<user_name>.jupyter\
Linux/macOS系统的配置文件路径:/Users/<user_name>/.jupyter/ 或 ~/.jupyter/
配置文件名:jupyter_notebook_config.*
可以是.json
文件或者.py
文件
如果不存在配置文件,可通过下面命令生成
jupyter notebook --generate-config
找到该jupyter_notebook_config.py文件,并打开
# 指定默认工作目录
c.NotebookApp.notebook_dir = r'D:\python学习'
#允许所有IP都可访问
c.NotebookApp.ip = '*'
#将开启jupyter后在服务器上打开浏览器的选项设置为False
c.NotebookApp.open_browser = False
#指定访问端口
c.NotebookApp.port = 8888
#允许远程访问
c.NotebookApp.allow_remote_access = True
五、Jupyter Notebook更改环境
jupyter notebook访问的时候,默认使用了anaconda的base环境。
如果需要更换环境,执行下面命令:
python -m ipykernel install --user --name 要添加的环境 --display-name "jupyter中显示的kernel名字"
python -m ipykernel install --user --name Pytorch --display-name "Pytorch"
查看Kernel
jupyter kernelspec list
删除指定Kernel
jupyter kernelspec remove pyhorch
六、Jupyter Notebook 主题更换
经测试,大部分自定义主题界面不支持自适应,因此如果需要在平板上使用jupyter,建议使用默认主题。
# 通过下面命令安装主题
pip install jupyterthemes
查看所有主题
jt -l
# 更换为onedork主题
jt -t onedork
# 下面命令回复默认主题
jt -r