jupyter notebook 主题自定义

✅作者简介:人工智能专业本科在读,喜欢计算机与编程,写博客记录自己的学习历程。
🍎个人主页:小嗷犬的博客
🍊个人信条:为天地立心,为生民立命,为往圣继绝学,为万世开太平。
🥭本文内容:jupyter notebook 主题自定义



jupyter notebook 是我们常用的 Python 编辑环境,但是它的配色风格与字体风格却有些单调,使用久了难免有些厌倦,本文将教大家更改 jupyter notebook 的主题和字体。

jupyterthemes 的安装

jupyterthemes 是一个开源的 jupyter 插件,它为我们提供了多种配色风格和字体。

开源地址:https://github.com/dunovank/jupyter-themes

我们这里用pip命令安装:

pip install jupyterthemes

jupyterthemes 内容与用法

摘自开源项目README.md文件:

JT Customizable Features(可自定义的部分)

plotting style

plotting style

markdown/equations

markdown/equations

pandas dataframes

pandas dataframes

command palette

command palette

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]
Description of Command Line options(用法描述)
cl optionsargdefault
Usage help-h
List Themes-l
Theme Name to Install-t
Code Font-f
Code Font-Size-fs11
Notebook Font-nf
Notebook Font Size-nfs13
Text/MD Cell Font-tf
Text/MD Cell Fontsize-tfs13
Pandas DF Fontsize-dfs9
Output Area Fontsize-ofs8.5
Mathjax Fontsize (%)-mathfs100
Intro Page Margins-mauto
Cell Width-cellw980
Line Height-lineh170
Cursor Width-cursw2
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

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

字体(代码单元格)

Monospace Fonts (code cells)
-f argMonospace Font
ankaAnka/Coder
anonymousAnonymous Pro
aurulentAurulent Sans Mono
bitstreamBitstream Vera Sans Mono
bpmonoBPmono
codeCode New Roman
consolamonoConsolamono
cousineCousine
dejavuDejaVu Sans Mono
droidmonoDroid Sans Mono
firaFira Mono
firacodeFira Code
genericGeneric Mono
hackHack
haskligHasklig
inconsolataInconsolata-g
inputmonoInput Mono
iosevkaIosevka
liberationLiberation Mono
mesloMeslo
officeOffice Code Pro
oxygenOxygen Mono
robotoRoboto Mono
saxmonosaxMono
sourceSource Code Pro
sourcemedSource Code Pro Medium
sudovarSudo Variable
ptmonoPT Mono
ubuntuUbuntu Mono
Sans-Serif Fonts
-nf/-tf argSans-Serif Font
opensansOpen Sans
droidsansDroid Sans
exosansExo_2
latosansLato
ptsansPT Sans
robotosansRoboto
sourcesansSource Sans Pro
Serif Fonts
-nf/-tf argSerif Font
loraserifLora
ptserifPT Serif
georgiaserifGeorgia
cardoserifCardo
crimsonserifCrimson Text
ebserifEB Garamond
merriserifMerriweather
neutonserifNeuton
goudyserifSorts Mill Goudy

如何设置导入的字体

笔者对插件提供的主题配色比较满意,但插件中没有包含笔者喜欢的Consola字体,于是笔者便自己导入字体来设置,下面提供教程。

首先让我们找到你的 Python 安装地址,然后打开以下路径的文件:

你的Python位置\Lib\site-packages\jupyterthemes\stylefx.py

大概在文件的524行,我们按格式:

    fonts = {'mono':
             {命令中使用的字体名:[字体名,字体所在的文件夹的名称]
             ...
             }

加入自己的字体名称,红框为笔者添加的字体:
文件1

除此之外,我们还要将对应的字体文件放到对应的位置。

进入以下路径:

你的Python位置\Lib\site-packages\jupyterthemes\fonts

选择你刚刚添加的字体类型:
文件夹1

进入对应分类子文件夹下,创建与你字体同名的文件夹:
文件夹2

将你要导入的字体改好对应的名字然后放入该文件夹:
文件夹3

然后就可以在命令行中设置自己的字体了。

自用主题字体分享

以下为笔者自己在用的主题和字体:

jupyter notebook1

jupyter notebook2

设置命令(需要Consola字体):

jt -t onedork -f consola -fs 14 -cellw 80% -ofs 14 -dfs 14 -T
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小嗷犬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值