Jupyter Notebook字体样式、大小设置

前段时间学cs61a才开始使用vscode,发现自带jupyter,就弃用了忍受默认字体已久的jupyter notebook,但是操作上一直不太习惯,现在找到了Jupyter Notebook修改字体的方法,舒服多了。
直接用jupyter-theme安装主题比较方便,但是感觉界面还没有默认好看,又用css改回来了
改回来的时候需要注意:C:\Users\[username]\.jupyter下的custom需要删掉,不然会覆盖anaconda目录下的custom.css

通过css文件修改

  1. 找到anaconda子目录下的codemirror.css文件

    D:\anaconda3\Lib\site-packages\notebook\static\components\codemirror\lib

  2. 全局设置,font-family表示字体,修改为想要的字体,比如Consolas

/* BASICS */

.CodeMirror {
  /* Set height, width, borders, and global font properties here */
  font-family: Consolas;
  height: 300px;
  color: black;
  direction: ltr;
}
  1. 编辑器设置,font-size表示字体大小,line-height表示行高,其中em指的相对单位,1.5em指1.5*font-size。
.CodeMirror pre.CodeMirror-line-like {
  /* Reset some styles that the rest of the page might have set */
  -moz-border-radius: 0; -webkit-border-radius: 0; border-radius: 0;
  border-width: 0;
  background: transparent;
  font-family: inherit;
  font-size: 15px;
  margin: 0;
  white-space: pre;
  word-wrap: normal;
  line-height: 1.5em;
  color: inherit;
  z-index: 2;
  position: relative;
  overflow: visible;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-variant-ligatures: contextual;
  font-variant-ligatures: contextual;
}
  1. 找到anaconda子目录下的custom.css文件

    D:\anaconda3\Lib\site-packages\notebook\static\custom

  2. 打开为空文件,添加div.output_area pre等表示输出部分的字体设置。

div.output_area pre {font-family: Consolas; font-size: 10pt;}
div.input_prompt {font-family: Consolas; font-size: 10pt;}
div.out_prompt_overlay {font-family: Consolas; font-size: 10pt;}
div.prompt {font-family: Consolas; font-size: 10pt;}
span.cm-comment {font-family:  Consolas !important; font-style:normal !important; color:#FFAE3C !important;}

通过jupyter-themes包安装主题

官方链接:https://github.com/dunovank/jupyter-themes

  1. 安装jupyter-themes
# pip安装
pip install jupyterthemes

# upgrade to latest version
pip install --upgrade jupyterthemes


# conda安装
conda install -c conda-forge jupyterthemes

# update to latest version
conda update jupyterthemes
  1. 命令行用法
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]

命令行各选项:

cl options arg default cl options arg default
Usage help -h -- Cell Width -cellw 980
List Themes -l -- Line Height -lineh 170
Theme Name to Install -t -- Cursor Width -cursw 2
Code Font -f -- Cursor Color -cursc --
Code Font-Size -fs 11 Alt Prompt Layout -altp --
Notebook Font -nf -- Alt Markdown BG Color -altmd --
Notebook Font Size -nfs 13 Alt Output BG Color -altout --
Text/MD Cell Font -tf -- Style Vim NBExt* -vim --
Text/MD Cell Fontsize -tfs 13 Toolbar Visible -T --
Pandas DF Fontsize -dfs 9 Name & Logo Visible -N --
Output Area Fontsize -ofs 8.5 Kernel Logo Visible -kl --
Mathjax Fontsize (%) -mathfs 100 Reset Default Theme -r --
Intro Page Margins -m auto Force Default Fonts -dfonts --

examples(需要选择提供列表中的theme):

# 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
posted @ 2021-08-27 00:16  ikventure  阅读(10446)  评论(3编辑  收藏  举报