如何在IPython中自定义文本颜色?
我想在IPython中自定义文本的颜色,但不确定如何实现 我知道在Python中,我可以通过在sys.ps1和sys.ps2末尾加上ANSI颜色代码来实现这一点,例如如何在IPython中自定义文本颜色?,python,ipython,customization,Python,Ipython,Customization,我想在IPython中自定义文本的颜色,但不确定如何实现 我知道在Python中,我可以通过在sys.ps1和sys.ps2末尾加上ANSI颜色代码来实现这一点,例如 sys.ps1=">>> \001\033[0m\033[34m\002" 但是使用PromptManager.in_template的相应方法不适用于IPython。比如说 c = get_config() c.PromptManager.in_template = 'In [{count}] : {colo
sys.ps1=">>> \001\033[0m\033[34m\002"
但是使用PromptManager.in_template
的相应方法不适用于IPython。比如说
c = get_config()
c.PromptManager.in_template = 'In [{count}] : {color.Blue}'
对提示后文本的颜色没有影响
有没有办法更改IPython中文本的颜色?提示将输入的颜色设置为colors.in_normal
。如果要更改输入颜色,则必须更改此颜色
不幸的是,定制的配色方案仍然在待办事项列表中(应该很容易,只是优先级不高)
您似乎想要更改配置文件中输入和/或输出文本的颜色,这是一个有点粗俗的示例:
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+
很好地转换。(thx@meowsqueak用于快速测试!)
从IPython 5.5.0
开始,由于目标配置文件文件夹中的IPython_config.py
,您可以覆盖任何样式颜色。(即:~/.ipython/profile_默认值
用于linux典型安装上的默认值
配置文件)
IPython代码与着色相关,有几种方法用于解析器、调试器和交互式shell。一些部件使用Pygments
,另一些部件提供ANSI转义代码,仅限于16色调色板
解决方案并不漂亮,但很有效。如果还有其他更漂亮的方式,请在评论中告诉我
请注意,由于扩展了默认调色板,我的解决方案还包括为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配置文件~/.iphyton/ipython\u config.py
ipython_config.py。最简单的方法是运行以下命令:
ipython profile create
如果您使用的是ipython3
start
ipython3 profile create
这将在您的~/.ipython
中安装一个默认的配置文件目录和一些脚本;否则,请找到一个文件并将其复制到~/.ipython/profile\u default/目录中
使用以下命令对此文件进行备份:
cp ~/.ipython/profile_default/ipython_config.py \
~/.ipython/profile_default/ipython_config.py_backup
使用您选择的文本编辑器打开~/.ipython/profile\u default/ipython\u config.py
,搜索以下设置并注释掉(删除“#”):
c.InteractiveShell.color\u info=True
c.InteractiveShell.colors='Linux'
c.TerminalInteractiveShell.highlighting\u style='monokai'
c.TerminalInteractiveShell.highlight\u matching\u方括号=True
等等。默认情况下,有许多有用的设置被禁用;您只需将它们注释掉(删除“#”)
样式文件
在Ubuntu/Debian上,您必须安装该软件包
或
样式文件可在以下目录中找到:
/path/to/your/python/站点包/pygments/styles/,例如。
/usr/lib/python3/dist-packages/pygments/styles/monokai.py
或者,您也可以使用pygmentize
列出已安装的样式:
pygmentize -L styles
在Ipython中更改颜色的简单方法:
打开IPython配置文件(IPython\u config.py
),通常位于~/.IPython/profile\u default/
或~/.IPython/profile.
(如果您使用的是IPython配置文件)
查找并取消注释(或更改)设置c.InteractiveShell.colors
将其更改为可用的颜色模式之一,您可以在注释中看到它们全部列出-NoColor
、Neutral
、Linux
、或LightBG
保存配置文件并启动IPython
如果看不到配置文件-可以使用以下命令快速生成:
ipython配置文件创建[profilename]
它将生成带有默认设置的配置文件,然后您可以根据需要对其进行编辑。在Windows中如何执行此操作?我可能遗漏了一些东西,但对于我特定的iPython安装,没有对象提示。PColLinuxI使用的是iPython 6.1.0,也没有提示。PColLinux
或提示。PColLinuxBG
。从外观上看,这个子系统是在IPython 5(或接近IPython 5)中重写的,我在最新版本中找不到任何等价物。@minrk很抱歉这个愚蠢的问题,但是我们应该将config.py保存在哪里?@meowsqueak你尝试过我的解决方案吗(我只测试了5.5.0
)?@vaab我刚刚用IPython 6.2.1尝试过你的解决方案(使用python 3.6.2)-请参阅我对您的答案的评论。我刚刚在rxvt-unicode-256color中使用IPython 6.2.1(使用python 3.6.2)尝试了您的解决方案,它似乎可以部分工作-我使用默认的“Linux”获得不同的颜色方案,但它们是不寻常的。例如,提示是一个非常非常浅的绿色,带有一个浅蓝色的灰色数字部分,但是像import
这样的关键字是粗体洋红,def
是粗体蓝色。你有它应该是什么样子的屏幕截图吗?@meowsqueak感谢测试。它实际上改变了颜色,这确实很奇怪ut没有得到预期的颜色。我没有在这里分享一组精心制作的颜色,但只分享了Linux方案中的一些更改
sudo pip3 install pygments
pygmentize -L styles