✅作者简介:人工智能专业本科在读,喜欢计算机与编程,写博客记录自己的学习历程。
🍎个人主页: 小嗷犬的博客
🍊个人信条:为天地立心,为生民立命,为往圣继绝学,为万世开太平。
🥭本文内容:jupyter notebook 主题自定义jupyter notebook 是我们常用的 Python 编辑环境,但是它的配色风格与字体风格却有些单调,使用久了难免有些厌倦,本文将教大家更改 jupyter notebook 的主题和字体。
1.jupyterthemes 的安装
jupyterthemes
是一个开源的 jupyter 插件,它为我们提供了多种配色风格和字体。开源地址:https://github.com/dunovank/jupyter-themes
我们这里用
pip
命令安装:
pip install jupyterthemes
2.jupyterthemes 内容与用法
摘自开源项目
README.md
文件:
2.1 JT Customizable Features(可自定义的部分)
2.1.1 plotting style
2.1.2 markdown/equations
2.1.3 pandas dataframes
2.1.4 command palette
2.2 Command Line Usage(命令行用法)
jt [-h] [-l] [-t THEME] [-f MONOFONT] [-fs MONOSIZE] [-nf NBFONT]
[-nfs NBFONTSIZE] [-tf TCFONT] [-tfs TCFONTSIZE] [-dfs DFFONTSIZE]
[-m MARGINS] [-cursw CURSORWIDTH] [-cursc CURSORCOLOR] [-vim]
[-cellw CELLWIDTH] [-lineh LINEHEIGHT] [-altp] [-altmd] [-altout]
[-P] [-T] [-N] [-r] [-dfonts]
2.2.1 Description of Command Line options(用法描述)
cl options | arg | default |
---|---|---|
Usage help | -h | -- |
List Themes | -l | -- |
Theme Name to Install | -t | -- |
Code Font | -f | -- |
Code Font-Size | -fs | 11 |
Notebook Font | -nf | -- |
Notebook Font Size | -nfs | 13 |
Text/MD Cell Font | -tf | -- |
Text/MD Cell Fontsize | -tfs | 13 |
Pandas DF Fontsize | -dfs | 9 |
Output Area Fontsize | -ofs | 8.5 |
Mathjax Fontsize (%) | -mathfs | 100 |
Intro Page Margins | -m | auto |
Cell Width | -cellw | 980 |
Line Height | -lineh | 170 |
Cursor Width | -cursw | 2 |
Cursor Color | -cursc | -- |
Alt Prompt Layout | -altp | -- |
Alt Markdown BG Color | -altmd | -- |
Alt Output BG Color | -altout | -- |
Style Vim NBExt* | -vim | -- |
Toolbar Visible | -T | -- |
Name & Logo Visible | -N | -- |
Kernel Logo Visible | -kl | -- |
Reset Default Theme | -r | -- |
Force Default Fonts | -dfonts | -- |
2.3 Command Line Examples(用例)
第2行为可选的主题类型:
# list available themes
# onedork | grade3 | oceans16 | chesterish | monokai | solarizedl | solarizedd
jt -l
# select theme...
jt -t chesterish
# restore default theme
# NOTE: Need to delete browser cache after running jt -r
# If this doesn't work, try starting a new notebook session.
jt -r
# toggle toolbar ON and notebook name ON
jt -t grade3 -T -N
# toggle kernel logo. kernel logo is in same container as name
# toggled with -N. That means that making the kernel logo visible is
# pointless without also making the name visible
jt -t grade3 -N -kl
# set code font to 'Roboto Mono' 12pt
# (see monospace font table below)
jt -t onedork -f roboto -fs 12
# set code font to Fira Mono, 11.5pt
# 3digit font-sizes get converted into float (115-->11.5)
# 2digit font-sizes > 25 get converted into float (85-->8.5)
jt -t solarizedd -f fira -fs 115
# set font/font-size of markdown (text cells) and notebook (interface)
# see sans-serif & serif font tables below
jt -t oceans16 -tf merriserif -tfs 10 -nf ptsans -nfs 13
# adjust cell width (% screen width) and line height
jt -t chesterish -cellw 90% -lineh 170
# or set the cell width in pixels by leaving off the '%' sign
jt -t solarizedl -cellw 860
# fix the container-margins on the intro page (defaults to 'auto')
jt -t monokai -m 200
# adjust cursor width (in px) and make cursor red
# options: b (blue), o (orange), r (red), p (purple), g (green), x (font color)
jt -t oceans16 -cursc r -cursw 5
# choose alternate prompt layout (narrower/no numbers)
jt -t grade3 -altp
# my two go-to styles
# dark
jt -t onedork -fs 95 -altp -tfs 11 -nfs 115 -cellw 88% -T
# light
jt -t grade3 -fs 95 -altp -tfs 11 -nfs 115 -cellw 88% -T
2.4 字体(代码单元格)
2.4.1 Monospace Fonts (code cells)
-f arg | Monospace Font |
---|---|
anka | Anka/Coder |
anonymous | Anonymous Pro |
aurulent | Aurulent Sans Mono |
bitstream | Bitstream Vera Sans Mono |
bpmono | BPmono |
code | Code New Roman |
consolamono | Consolamono |
cousine | Cousine |
dejavu | DejaVu Sans Mono |
droidmono | Droid Sans Mono |
fira | Fira Mono |
firacode | Fira Code |
generic | Generic Mono |
hack | Hack |
hasklig | Hasklig |
inconsolata | Inconsolata-g |
inputmono | Input Mono |
iosevka | Iosevka |
liberation | Liberation Mono |
meslo | Meslo |
office | Office Code Pro |
oxygen | Oxygen Mono |
roboto | Roboto Mono |
saxmono | saxMono |
source | Source Code Pro |
sourcemed | Source Code Pro Medium |
sudovar | Sudo Variable |
ptmono | PT Mono |
ubuntu | Ubuntu Mono |
2.4.2 Sans-Serif Fonts
-nf/-tf arg | Sans-Serif Font |
---|---|
opensans | Open Sans |
droidsans | Droid Sans |
exosans | Exo_2 |
latosans | Lato |
ptsans | PT Sans |
robotosans | Roboto |
sourcesans | Source Sans Pro |
2.4.3 Serif Fonts
-nf/-tf arg | Serif Font |
---|---|
loraserif | Lora |
ptserif | PT Serif |
georgiaserif | Georgia |
cardoserif | Cardo |
crimsonserif | Crimson Text |
ebserif | EB Garamond |
merriserif | Merriweather |
neutonserif | Neuton |
goudyserif | Sorts Mill Goudy |
3.如何设置导入的字体
笔者对插件提供的主题配色比较满意,但插件中没有包含笔者喜欢的Consola
字体,于是笔者便自己导入字体来设置,下面提供教程。首先让我们找到你的 Python 安装地址,然后打开以下路径的文件:
你的Python位置\Lib\site-packages\jupyterthemes\stylefx.py
大概在文件的524行,我们按格式:
fonts = {'mono':
{命令中使用的字体名:[字体名,字体所在的文件夹的名称]
...
}
加入自己的字体名称,红框为笔者添加的字体:
除此之外,我们还要将对应的字体文件放到对应的位置。进入以下路径:
你的Python位置\Lib\site-packages\jupyterthemes\fonts
选择你刚刚添加的字体类型:
进入对应分类子文件夹下,创建与你字体同名的文件夹:
将你要导入的字体改好对应的名字然后放入该文件夹:
然后就可以在命令行中设置自己的字体了。
4.自用主题字体分享
以下为笔者自己在用的主题和字体:
设置命令(需要
Consola
字体):
jt -t onedork -f consola -fs 14 -cellw 80% -ofs 14 -dfs 14 -T