使用jupyterthemes插件定制jupyter notebook界面

本文介绍如何使用jupyter-themes工具定制Jupyter Notebook的主题和字体,包括安装、基本用法和深度定制方法。通过简单的命令行操作,可以调整界面样式、字体大小、颜色等,以满足个人喜好和需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jupyter notebook界面是可以定制的,定制位置在:C:\anaconda\Lib\site-packages\notebook\static\custom
启动jupyter notebook之后,查看网页源代码,会发现网页中会自动加载custom.css和custom.js。

于是有人推出了jupyter-themes这款主题定制工具,这个工具使用上是存在一些界面bug的。

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

一、安装

# install jupyterthemes
pip install jupyterthemes

# upgrade to latest version
pip install --upgrade jupyterthemes

二、基本用法jt -h

usage: jt [-h] [-l] [-t THEME] [-f MONOFONT] [-fs MONOSIZE] [-nf NBFONT]
          [-nfs NBFONTSIZE] [-tf TCFONT] [-tfs TCFONTSIZE] [-dfs DFFONTSIZE]
          [-ofs OUTFONTSIZE] [-mathfs MATHFONTSIZE] [-m MARGINS]
          [-cursw CURSORWIDTH] [-cursc CURSORCOLOR] [-cellw CELLWIDTH]
          [-lineh LINEHEIGHT] [-altp] [-altmd] [-altout] [-P] [-T] [-N] [-kl]
          [-vim] [-r] [-dfonts]

optional arguments:
  -h, --help            show this help message and exit
  -l, --list            list available themes
  -t THEME, --theme THEME
                        theme name to install
  -f MONOFONT, --monofont MONOFONT
                        monospace code font
  -fs MONOSIZE, --monosize MONOSIZE
                        code font-size
  -nf NBFONT, --nbfont NBFONT
                        notebook font
  -nfs NBFONTSIZE, --nbfontsize NBFONTSIZE
                        notebook fontsize
  -tf TCFONT, --tcfont TCFONT
                        txtcell font
  -tfs TCFONTSIZE, --tcfontsize TCFONTSIZE
                        txtcell fontsize
  -dfs DFFONTSIZE, --dffontsize DFFONTSIZE
                        pandas dataframe fontsize
  -ofs OUTFONTSIZE, --outfontsize OUTFONTSIZE
                        output area fontsize
  -mathfs MATHFONTSIZE, --mathfontsize MATHFONTSIZE
                        mathjax fontsize (in %)
  -m MARGINS, --margins MARGINS
                        fix margins of main intro page
  -cursw CURSORWIDTH, --cursorwidth CURSORWIDTH
                        set cursorwidth (px)
  -cursc CURSORCOLOR, --cursorcolor CURSORCOLOR
                        cursor color (r, b, g, p)
  -cellw CELLWIDTH, --cellwidth CELLWIDTH
                        set cell width (px or %)
  -lineh LINEHEIGHT, --lineheight LINEHEIGHT
                        code/text line-height (%)
  -altp, --altprompt    alt input prompt style
  -altmd, --altmarkdown
                        alt markdown cell style
  -altout, --altoutput  set output bg color to notebook bg
  -P, --hideprompt      hide cell input prompt
  -T, --toolbar         make toolbar visible
  -N, --nbname          nb name/logo visible
  -kl, --kernellogo     kernel logo visible
  -vim, --vimext        toggle styles for vim
  -r, --reset           reset to default theme
  -dfonts, --defaultfonts
                        force fonts to browser default

三、github上的README教程

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

# 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

Set Plotting Style (from within notebook)

jtplot.style() makes changes to matplotlib's rcParams dictionary so that figure aesthetics match those of a chosen jupyterthemes style. In addition to setting the color scheme, jtplot.style() allows you to control various figure properties (spines, grid, font scale, etc.) as well as the plotting "context" (borrowed from seaborn).

Note, these commands do not need to be re-run every time you generate a new plot, just once at the beginning of your notebook or whenever style changes are desired after that.

Pro-tip: Include the following two lines in ~/.ipython/profile_default/startup/startup.ipy file to set plotting style automatically whenever you start a notebook:

# import jtplot submodule from jupyterthemes
from jupyterthemes import jtplot

# currently installed theme will be used to
# set plot style if no arguments provided
jtplot.style()

jtplot.style() Examples

# import jtplot module in notebook
from jupyterthemes import jtplot

# choose which theme to inherit plotting style from
# onedork | grade3 | oceans16 | chesterish | monokai | solarizedl | solarizedd
jtplot.style(theme='onedork')

# set "context" (paper, notebook, talk, poster)
# scale font-size of ticklabels, legend, etc.
# remove spines from x and y axes and make grid dashed
jtplot.style(context='talk', fscale=1.4, spines=False, gridlines='--')

# turn on X- and Y-axis tick marks (default=False)
# turn off the axis grid lines (default=True)
# and set the default figure size
jtplot.style(ticks=True, grid=False, figsize=(6, 4.5))

# reset default matplotlib rcParams
jtplot.reset()
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
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

四、 深度定制

一般设置一下主题和字体就足够普通用户使用了,例如:
jt -t monokai -f consolamono -fs 15

如果还嫌瑕疵,那就需要自己去改前端了。
~/.jupyter\custom目录下有主题、CSS、字体等。

转载于:https://www.cnblogs.com/weiyinfu/p/9697364.html

### Jupyter Notebook 界面使用指南 #### 一、基本布局介绍 Jupyter Notebook界面主要包括两个区域:菜单栏和工作区。菜单栏提供了多种工具来管理和操作笔记本文件,而工作区则用于编写和运行代码。 - **菜单栏**:位于页面顶部,包含了多个选项卡,允许用户执行各种命令[^2]。 - **工作区**:占据大部分屏幕空间,由一系列可交互的单元格组成。每个单元格可以包含代码或文本描述。 #### 二、自定义设置与布局调整 为了更好地适应个人需求,可以通过以下方式来自定义 Jupyter Notebook: ##### 更改主题样式 通过安装扩展插件如 `jupyterthemes` 可以为 Jupyter Notebook 应用不同的颜色方案和字体大小等视觉效果改进措施。 ```bash pip install jupyterthemes jt -t oceans16 ``` 上述命令会将默认的主题更改为名为 "oceans16" 的深色风格,并应用相应的配色方案[^3]。 ##### 调整侧边栏宽度 对于某些情况下希望增加编辑器可用面积的情况来说,可以在浏览器开发者控制台输入 JavaScript 来动态改变左侧目录树所占用的空间比例: ```javascript // 将左边栏宽度设为20% $(".tree-container").css("width", "20%"); ``` 请注意这只是一个临时性的修改方法,在刷新页面之后将会恢复原状;如果想要永久生效,则需考虑其他配置手段或者寻找合适的第三方包支持此类定制化功能[^4]。 ##### 修改快捷键映射 利用官方提供的 API 或者借助社区开发的小工具实现对常用操作对应的键盘组合重新分配,从而提高工作效率并减少鼠标依赖度。例如,要创建一个新的 Python 文件可以用 Ctrl+N (Windows/Linux) 或 Command+N (Mac),这些都可以按照自己的习惯来进行个性化设定[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值