![](/img/trans.png)
[英]How do I make IPython reload a file passed to `ipython -i …`
[英]How do I customize text color in IPython?
我想在 IPython 中自定義文本的顏色,但不知道該怎么做。
我知道在 Python 中,我可以通過使用 ANSI 顏色代碼結束sys.ps1
和sys.ps2
來做到這一點,例如
sys.ps1=">>> \001\033[0m\033[34m\002"
但是使用PromptManager.in_template
的相應方法不適用於 IPython。 例如
c = get_config()
c.PromptManager.in_template = 'In [{count}] : {color.Blue}'
對提示后文本的顏色沒有影響。
有沒有辦法改變 IPython 中文本的顏色?
首先,您必須在您的主目錄中創建一個 ipython 配置文件~/.iphyton/ipython_config.py
。 ipython_config.py 。 最簡單的方法是運行以下命令:
ipython profile create
如果您使用的是ipython3
start
ipython3 profile create
這將在你的~/.ipython
安裝一個profile_default
目錄和一些腳本; 否則找到一個文件並將其復制到您的~/.ipython/profile_default/目錄中。
使用以下命令備份此文件:
cp ~/.ipython/profile_default/ipython_config.py \
~/.ipython/profile_default/ipython_config.py_backup
使用您選擇的文本編輯器打開~/.ipython/profile_default/ipython_config.py
並搜索以下 settig 並根據需要將其注釋掉(刪除“#”):
c.InteractiveShell.color_info = True
c.InteractiveShell.colors = 'Linux'
c.TerminalInteractiveShell.highlighting_style = 'monokai'
c.TerminalInteractiveShell.highlight_matching_brackets = True
等等。 有許多有用的設置默認是禁用的; 您只需將它們注釋掉(刪除 `#' )。
樣式文件
在 Ubuntu/Debian 上你必須安裝pygments包
sudo apt install python3-pygments
要么
sudo pip3 install pygments
樣式文件可以在以下目錄中找到:
/path/to/your/python/ site-packages/pygments/styles/ ,例如/usr/lib/python3/dist-packages/pygments/styles/monokai.py
或者,您還可以使用pygmentize
列出已安裝的樣式:
pygmentize -L styles
提示將輸入的顏色顯式設置為colors.in_normal
。 如果要更改輸入顏色,則必須更改此顏色。
不幸的是,定制的配色方案仍然在待辦事項列表中(應該很容易,只是不是一個高優先級)。
你似乎想要的一個有點hackish的例子,改變配置文件中輸入和/或輸出文本的顏色:
from IPython.utils import coloransi
from IPython.core import prompts
termcolors = coloransi.TermColors() # the color table
# IPython's two color schemes:
dark = prompts.PColLinux.colors
light = prompts.PColLightBG.colors
# colors.in_normal affects input code
dark.in_normal = termcolors.Green
light.in_normal = termcolors.Blue
# colors.normal affects output
dark.normal = light.normal = termcolors.Red
這將設置它以使文本顏色與提示匹配,但您當然可以選擇任何您想要的顏色。
警告:此答案僅與IPython
5.5.0
兼容,並且似乎與IPython
6+
無法很好地轉換。 (感謝@meowsqueak 進行快速測試!)
從IPython 5.5.0
,由於ipython_config.py
配置文件文件夾中的ipython_config.py
,您可以覆蓋任何樣式顏色。 (即:Linux 典型安裝上的default
配置文件的~/.ipython/profile_default
)。
IPython 代碼與着色有關,並且有幾種方法用於解析器、調試器、交互式 shell。 某些部分使用Pygments
,其他一些提供僅限於 16 色調色板的 ANSI 轉義碼。
解決方案並不漂亮,但有效。 如果還有其他更漂亮的其他方式,請在評論中告訴我!
請注意,由於默認調色板的擴展,我的解決方案還包括為 IPython 着色的每個部分使用 256 種或更多顏色的能力。 下面的代碼中有一個如何擴展到 256 色的示例。
所以這里是怎么做,以及可能使用的所有令牌的列表:
##
## Basic color scheme that will be modified
##
colorLabel = 'Linux'
c.InteractiveShell.colors = colorLabel
from pygments.token import Token, Keyword, Name, Comment, String, Error, \
Number, Operator, Generic, Whitespace
c.TerminalInteractiveShell.highlighting_style_overrides = {
## Standard Pygments tokens (are all used by IPython ?)
Whitespace: "#bbbbbb",
Comment: "italic #008800",
Comment.Preproc: "noitalic",
Comment.Special: "noitalic bold",
Keyword: "bold #AA22FF",
Keyword.Pseudo: "nobold",
Keyword.Type: "bold #00BB00",
Operator: "#666666",
Operator.Word: "bold #AA22FF",
Name.Builtin: "#fff", #""#AA22FF",
Name.Function: "#00A000",
Name.Class: "#0000FF",
Name.Namespace: "bold #0000FF",
Name.Exception: "bold #D2413A",
Name.Variable: "#B8860B",
Name.Constant: "#880000",
Name.Label: "#A0A000",
Name.Entity: "bold #999999",
Name.Attribute: "#BB4444",
Name.Tag: "bold #008000",
Name.Decorator: "#AA22FF",
String: "#BB4444",
String.Doc: "italic",
String.Interpol: "bold #BB6688",
String.Escape: "bold #BB6622",
String.Regex: "#BB6688",
String.Symbol: "#B8860B",
String.Other: "#008000",
Number: "#666666",
Generic.Heading: "bold #000080",
Generic.Subheading: "bold #800080",
Generic.Deleted: "#A00000",
Generic.Inserted: "#00A000",
Generic.Error: "#FF0000",
Generic.Emph: "italic",
Generic.Strong: "bold",
Generic.Prompt: "bold #000080",
Generic.Output: "#888",
Generic.Traceback: "#04D",
Error: "border:#ff0000",
## IPython
Token.Number: '#ffffff',
Token.Operator: 'noinherit',
Token.String: '#8b8',
Token.Name.Function: '#2080D0',
Token.Name.Class: 'bold #2080D0',
Token.Name.Namespace: 'bold #2080D0',
Token.Prompt: '#ffffff bold',
Token.PromptNum: '#888888 bold',
Token.OutPrompt: '#008b8b bold',
Token.OutPromptNum: '#006b6b bold',
}
from IPython.core import excolors, ultratb, debugger
from IPython.core.excolors import exception_colors as exception_colors_orig
##
## Add new color labels here before attributing them
##
from IPython.utils import coloransi
coloransi.color_templates = (
# Dark colors
("Black" , "0;30"),
("Red" , "0;31"),
("Green" , "0;32"),
("Brown" , "0;33"),
("Blue" , "0;34"),
("Purple" , "0;35"),
("Cyan" , "0;36"),
("LightGray" , "0;37"),
# Light colors
("DarkGray" , "1;30"),
("LightRed" , "1;31"),
("LightGreen" , "1;32"),
("Yellow" , "1;33"),
("LightBlue" , "1;34"),
("LightPurple" , "1;35"),
("LightCyan" , "1;36"),
("White" , "1;37"),
## 256-colors
("Green108", "38;5;108"),
)
coloransi.make_color_table(coloransi.TermColors)
coloransi.make_color_table(coloransi.InputTermColors)
for name, value in coloransi.color_templates:
setattr(coloransi.NoColors, name, '')
C = coloransi.TermColors
IC = coloransi.InputTermColors
def exception_colors():
ex_colors = exception_colors_orig()
ex_colors.add_scheme(coloransi.ColorScheme(
colorLabel,
# The color to be used for the top line
topline=C.LightRed,
# The colors to be used in the traceback
filename=C.Green,
lineno=C.DarkGray,
name=C.Purple,
vName=C.Cyan,
val=C.White,
em=C.LightCyan,
# Emphasized colors for the last frame of the traceback
normalEm=C.LightCyan,
filenameEm=C.Green,
linenoEm=C.Normal,
nameEm=C.LightPurple,
valEm=C.LightGreen,
# Colors for printing the exception
excName=C.Red,
line=C.Yellow,
caret=C.White,
Normal=C.Normal
))
return ex_colors
excolors.exception_colors = exception_colors
ultratb.exception_colors = exception_colors
debugger.exception_colors = exception_colors
##
## Parser color (source code colors)
##
from IPython.utils import PyColorize
import token
import tokenize
PyColorize.ANSICodeColors[colorLabel] = coloransi.ColorScheme(
colorLabel, {
'header' : C.LightRed,
token.NUMBER : C.LightCyan,
token.OP : C.Normal,
token.STRING : C.Green108,
tokenize.COMMENT : C.LightGray,
token.NAME : C.Normal,
token.ERRORTOKEN : C.Red,
PyColorize._KEYWORD : C.White,
PyColorize._TEXT : C.Yellow,
## Keep IC here, you can use other colors
'in_prompt' : IC.Green,
'in_number' : IC.LightGreen,
'in_prompt2' : IC.Green,
'in_normal' : IC.Normal, # color off (usu. Colors.Normal)
'out_prompt' : C.Red,
'out_number' : C.LightRed,
'normal' : C.Normal # color off (usu. Colors.Normal)
})
在 Ipython 中更改顏色的簡單方法:
打開你的 IPython 配置文件( ipython_config.py
),通常在~/.ipython/profile_default/
或~/.ipython/profile_<your_usrname>/
(如果你使用 IPython 配置文件)。
查找並取消注釋(或更改)設置c.InteractiveShell.colors
將其更改為可用的顏色模式之一,您可以在評論中看到它們都在此處列出 - NoColor
、 Neutral
、 Linux
或LightBG
保存配置文件並啟動 IPython
如果您沒有看到配置文件- 您可以使用以下命令快速生成它:
ipython profile create [profilename]
它將使用默認設置生成配置文件,然后您可以根據需要對其進行編輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.