jupyter notebook简易入门

hzem0kll.l4w.png

安装

安装notebook和插件

1
2
3
4
5
6
7
8
9
10
11
12
# 安装notebook,7.*版本对插件支持有点问题
$ conda install notebook=6.4

# 安装插件管理器jupyter_contrib_nbextensions
$ pip install jupyter_contrib_nbextensions
$ jupyter contrib nbextension install --user

# 安装可视化功能
$pip install jupyter_nbextensions_configurator
## can be skipped for notebook >=5.3
$jupyter nbextensions_configurator enable --user

在conda中,jupyter为notebook的别名,notebook才是包的官方名

常用插件

  • Toggle all line numbers
  • Collapsible headings
  • Variable Inspector
  • Execute time
  • Hinterland
  • Table of Contents(2)
  • Autopep8

配置

1
2
3
4
5
6
7
8
9
10
# 添加配置文件
$ jupyter notebook --generate-config

# 设置密码,生成密钥
$ ipython
In [1]: from notebook.auth import passwd
In [2]: passwd()

# 修改配置文件
$ vi ~/.jupyter/jupyter_notebook_config.py

在配置文件中添加如下内容

  • 7.*版本请参照此博客:博客链接
    1
    2
    3
    4
    5
    6
    c.NotebookApp.ip = '*' # 设置所有ip皆可访问 
    c.NotebookApp.password = '' # 刚才生成的密钥
    c.NotebookApp.open_browser = False # 禁止自动打开浏览器
    c.NotebookApp.port = 8888 # 随便指定一个端口
    c.NotebookApp.allow_root = True # 允许root身份运行jupyter notebook
    c.NotebookApp.notebook_dir = '/home/ubuntu/notebook' # 设置默认启动路径

配置jupyter,让其一直挂在服务器后台

1
$ nohup jupyter notebook --no-browser --allow-root &

关闭jupyter

1
2
$ ps -aux | grep jupyter
$ kill -9 PID

matplotlib缺少中文字体

下载对应字体

拷贝 simsun.ttc 到 matplotlib 的 font 文件夹

找到对应字体后在自己对应的 python 程序内执行下述语句找到 matplotlib 存放 font 的位置:

1
2
import matplotlib
print(matplotlib.matplotlib_fname())

结果如下:

1
.../lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc

以这个路径为基础截取至 mpl-data,然后执行下述命令拷贝 simsun.ttc 到 font 文件夹:

1
2
cd ../lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf
cp simhei字体路径 .

jupyter使用指南

配置内核

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 安装内核管理器
$ conda install ipykernel

# 添加内核
$ python -m ipykernel install --user --name 环境名 --display-name 别名

# 删除 jupyter 内核
$ jupyter kernelspec remove kernelname

# 查看 Jupyter notebook kernel
$ jupyter kernelspec list


# 方法二
# 在base环境中安装nb
$ conda install nb_conda_kernels

常用魔术命令

行魔术命令(Line Magics):

  • %run:运行指定的 Python 脚本。
  • %load:将外部脚本加载到单元格中。
  • %whos:列出当前命名空间中的变量和它们的信息。
  • %timeit:测量代码执行的时间。
  • %debug:进入交互式调试器。
  • %reset:重置命名空间中的所有变量和名称定义。

单元格魔术命令(Cell Magics):

  • %%time:测量整个单元格的执行时间。
  • %%html:渲染单元格中的内容作为 HTML。
  • %%writefile:将单元格中的内容写入文件。
  • %%bash:在单元格中执行 Bash 命令。
  • %%capture:捕获输出并将其赋值给一个变量。

帮助与文档:

  • %lsmagic:列出所有可用的魔术命令。
  • %magic:显示魔术命令的详细文档。
  • ?:获取对象的帮助信息。
  • ??:获取对象的详细帮助信息。

变量和状态:

  • %who:列出当前命名空间中定义的变量。
  • %whos:以更详细的格式列出当前命名空间中的变量。
  • %reset:重置命名空间中的所有变量和名称定义。
  • %store:将变量保存到文件中,以便在不同的 Notebook 中共享

魔术命令配置:

  • %config:查看和修改 Jupyter Notebook 的配置选项。

修改 Matplotlib 配置

1
2
import matplotlib
print(matplotlib.matplotlib_fname())

继续通过该命令获得 matplotlib 的配置文件:

1
vim .../lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc

执行下述命令找到如图所示相关位置:

1
/font.family

bvdwmrlv.n10.png

  • 去掉 font.family 前的 # 号

  • 去掉 font.sans-serif 的 # 号,并加入 simhei

  • axes.unicode_minus 修改为 False

  • :wq 保存

4、清空原有配置

通过下述命令获取 matplotlib 缓存位置,还是在自己对应 python 项目中执行:

1
2
import matplotlib
print(matplotlib.get_cachedir())

结果如下:

1
/home/ubuntu/.cache/matplotlib

删除文件夹

1
rm -r /home/ubuntu/.cache/matplotlib
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2023-2025 John Doe
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信