Jupyter编辑器

安装jupyter notebook

  1. 先安装好python3
  2. 安装jupyter notebook ,命令行窗口中 pip3 install jupyter
  3. 启动jupyter notebookr
    1. 打开CMD命令窗口
    2. 输入: jupyter notebook

修改主题

  1. 安装主题
    1. pip install jupyterthemes
    2. pip install jupyterlab_legos_ui
  2. 查看已经安装的主题
    1. !jt -l # 列出已经安装的主题
    2. 结果:[chesterish,grade3,gruvboxd,gruvboxl,monokai,oceans16,onedork,solarizedd,solarizedl]
  3. 切换主题并设置特性
    1. jt -t gruvboxd # -t 后面的是要切换的新主题名称
    2. jt的参数表
  4. 重置主题至默认主题
    1. jt -r
参数1 功能 参数2 功能
-f 代码字体 -fs 代码字号
-nf notebook字体 -nfs notebook字号
-tf Text/MD Cell字体 -tfs Text/MD Cell字号
-dfs pandas DF字号 -ofs Output Area 字号
-cellw cell的宽度 -dfonts 强制默认字体
-lineh 行高 -l 列出已经安装的主题
-r 恢复默认主题 -t *** 切换主题***:新主题名称
-T 显示工具栏 -N 显示名称和徽标

jt函数的使用

1
2
%%cmd
jt -h
Task exception was never retrieved
future: <Task finished name='Task-12' coro=<ScriptMagics.shebang.`<locals>`._handle_stream() done, defined at C:\Python310\lib\site-packages\IPython\core\magics\script.py:211> exception=UnicodeDecodeError('utf-8', b'Microsoft Windows [\xb0\xe6\xb1\xbe 10.0.22000.556]\r\n', 19, 20, 'invalid start byte')>
Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\IPython\core\magics\script.py", line 213, in _handle_stream
    line = (await stream.readline()).decode("utf8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 19: invalid start byte

查看已经安装的主题

1
!jt - l
Available Themes:
   chesterish
   grade3
   gruvboxd
   gruvboxl
   monokai
   oceans16
   onedork
   solarizedd
   solarizedl

切换新主题

1
!jt - t grade3 - cellw 91 % -lineh 130 - f source - fs 115 - tf ptserif - tfs 13 - nf ptsans - nfs 9 - ofs 9 - TN
jt -t 参数 功能描述
grade3 切换新主题grade3
-cellw 100% 调整单元格的宽度,占屏幕的比例
-lineh 120 单元格的高度
-f fira -fs 115 设置代码字体为Fira字体大小为11.5pt
-tf merriserif -tfs 10 设置makedown字体和大小
-nf ptsans -nfs 13 设置notebook(界面)字体、字体大小,实际就是菜单栏字体
-m 200 页面上的容器边距
-cursc r -cursw 5 设置光标的颜色为红色,宽度为5
-of *** -ofs 14 输出区的字形和大小(14)

代码的字体(等宽字体):-f 参数

  • 参数列表

anka
anonymous
aurulent
bitstream
bpmono
code
consolamono
cousine
dejavu
droidmono
fira
firacode
generic
hack
hasklig
inconsolata
inputmono
iosevka
liberation
meslo
office
oxygen
roboto
saxmono
source
sourcemed
ptmono
ubuntu

Notebook 、Text/MD 单元格的字体(无衬线字体):-nf/-tf 参数

  • 参数列表

opensans
droidsans
exosans
latosans
ptsans
robotosans
sourcesans

Notebook 、Text/MD 单元格的字体(衬线字体):-nf/-tf 参数

  • 参数列表

loraserif
ptserif
georgiaserif
cardoserif
crimsonserif
ebserif
merriserif
neutonserif
goudyserif

安装插件

  1. 安装nbextensions
  2. 了解插件包的功能;
  3. 大部分插件只需要在nbextensions 界面勾选即可,部分需要pip 安装
1
2
3
4
# !pip install jupyter_contrib_nbextensions
# !jupyter contrib nbextension install - -user
# !pip install jupyter_nbextensions_configurator
# !jupyter nbextension enable

<b>limit_output extension: Maximum message size of 10000 exceeded with 10114 characters</b>

1
2
3
4
5
6
# 安装autopep8
# !pip3 install autopep8

# # 安装魔法工具包
# !pip install ipywidgets
# !jupyter nbextension enable--py widgetsnbextension
插件 功能
0 Autopep8 美化代码
1 Hinterland 代码提示和补全
2 ExecuteTime 运行时间和耗时
3 variable inspector 变量监视
4 zenmode 不显示菜单栏
5 code folding 代码折叠
6 Collapsible headings 标题折叠
7 Skip-traceback 跳过报错的过程
8 Snippets 插入常用代码片
9 Slideshow 幻灯片放映
10 Jupyter Widgets 魔法工具包
11 Table of Contents 生成目录

输出其他类型代码

  1. 输入 %lsmagic 可以获得Magic操作符的列表
  2. 输入 %cmdname? 可以查看命令信息
  3. 输出HTML: %%html
  4. 输出cmd:%%cmd
  5. 输出LATEX:%%latex
  6. 嵌入链接:
    1. from IPython.display import IFrame
    2. IFrame(‘url’, width=800, height=450)
  7. 嵌入PDF:
    1. from IPython.display import IFrame
    2. IFrame(‘.\DR153test.pdf’, width=800, height=800)
  8. 运行python文件 .py
    1. %run xxx.py 按Ctrl+Enter后执行
1
%lsmagic

Output:

Available line magics:
%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cd  %clear  %cls  %colors  %conda  %config  %connect_info  %copy  %ddir  %debug  %dhist  %dirs  %doctest_mode  %echo  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %macro  %magic  %matplotlib  %mkdir  %more  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %ren  %rep  %rerun  %reset  %reset_selective  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%cmd  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.
1
2
# 查看用法
%cd?
1
2
3
%%cmd
cd d:\
dir
Task exception was never retrieved
future: <Task finished name='Task-7' coro=<ScriptMagics.shebang.`<locals>`._handle_stream() done, defined at C:\Python310\lib\site-packages\IPython\core\magics\script.py:211> exception=UnicodeDecodeError('utf-8', b'Microsoft Windows [\xb0\xe6\xb1\xbe 10.0.22000.556]\r\n', 19, 20, 'invalid start byte')>
Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\IPython\core\magics\script.py", line 213, in _handle_stream
    line = (await stream.readline()).decode("utf8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 19: invalid start byte
1
2
# 等同于上面的简化代码
!dir d: \

嵌入链接

1
2
3
4
5
from IPython.display import IFrame

IFrame('https://pandas.pydata.org/pandas-docs/stable/reference/groupby.html',
width=800,
height=800)

Output:

显示图片

1
2
3
4
import os
from IPython.display import Image, IFrame

Image('./1.webp', width='25%')

Output:

1
2
3
4
import os
from IPython.display import Image, IFrame

Image('./1.webp', retina=True)

Output:

显示pdf

1
2
3
4
5
import os
from IPython.display import Image, IFrame


IFrame('./2.pdf', width='80%', height='800px')

Output:

更新软件包

1
!pip3 install --upgrade pip