Skip to content

Jupyter 配置

生成配置文件

Jupyter 的配置文件 jupyter_notebook_config.py 一开始并不存在,需要手动生成。在命令行输入以下命令:

jupyter notebook --generate-config

生成的配置文件通常位于 C:\Users\<Username>\.jupyter\ 中。

更改默认工作目录

Jupyter 的默认工作目录通常是用户主目录,可以通过以下步骤更改:

  1. 打开配置文件 jupyter_notebook_config.py
  2. 找到 c.NotebookApp.notebook_dir,去掉注释 #
  3. 修改路径为你的目标目录,例如:
c.NotebookApp.notebook_dir = 'E:\\jupyter_notebook'
  1. 如果通过快捷方式启动 Jupyter,还需修改快捷方式的 目标 属性,删除或修改 %USERPROFILE% 部分。

更改默认浏览器

可以指定 Jupyter 使用特定的浏览器:

  1. 找到目标浏览器的可执行文件路径,例如 Chrome 的路径可能是:
C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe
  1. 打开配置文件 jupyter_notebook_config.py
  2. 添加以下代码:
import webbrowser
webbrowser.register('chrome', None, webbrowser.GenericBrowser('C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'))
c.NotebookApp.browser = 'chrome'

设置登录密码

为 Jupyter 设置密码以增强安全性:

  1. 打开配置文件 jupyter_notebook_config.py
  2. 找到 c.NotebookApp.allow_password_change,修改为:
c.NotebookApp.allow_password_change = False
  1. 在命令行生成密码哈希值:
jupyter notebook password
  1. 将生成的哈希值添加到配置文件中:
c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'

安装扩展插件

Jupyter 提供了丰富的插件来增强功能:

  1. 安装 jupyter_contrib_nbextensions
pip install jupyter_contrib_nbextensions
  1. 安装 JavaScript 和 CSS 文件:
jupyter contrib nbextension install --user
  1. 安装 jupyter_nbextensions_configurator
pip install jupyter_nbextensions_configurator
  1. 重启 Jupyter Notebook,即可在界面中看到 nbextensions 标签。

修改 notebook 样式

可以使用 jupyterthemes 来更改 Jupyter 的外观:

  1. 安装主题样式工具包:
pip install jupyterthemes
  1. 查看可用主题:
jt -l
  1. 应用主题(例如切换为 chesterish 主题):
jt -t chesterish
  1. 恢复默认主题:
jt -r

开启远程访问(可选)

如果需要在远程计算机上访问 Jupyter Notebook:

  1. 打开配置文件 jupyter_notebook_config.py
  2. 设置允许远程连接的 IP 地址:
c.NotebookApp.ip = '*'
  1. 可选:修改端口号:
c.NotebookApp.port = 9000

验证配置

配置完成后,重启 Jupyter Notebook。在本地或远程计算机的浏览器中输入 Jupyter 的地址和端口进行访问。

Ref

https://www.zhihu.com/tardis/zm/art/166165379 https://www.jianshu.com/p/4dd75f03180b