在某种程度上,IPython笔记本中某些输出是否可能以不同的颜色显示? 例如,类似于:
print("Hello Red World", color='red')
答案 0 :(得分:48)
当然,笔记本有自己的语法高亮显示。因此,在其他地方使用颜色时要小心,只是为了避免让自己或其他人更难阅读(例如,输出应该只是黑色,但如果有异常,你会得到红色的部分)。
但是(令我惊讶的是),您似乎可以使用ANSI转义码(即使在浏览器中)。至少,我可以:
在默认的Python提示符下:
>>> print("\x1b[31m\"red\"\x1b[0m")
"red"
在笔记本中:
In [28]: print("\x1b[31m\"red\"\x1b[0m")
"red"
(显然,我在这里欺骗了SO的语法高亮,以便在两个例子中以红色打印“红色”。我不认为SO允许用户为文本设置颜色。)
我真的不知道另一种获取颜色的方法。
有关ANSI转义码的更多信息,我建议使用Wikipedia article。如果你发现上面的详细信息,你当然可以编写一个包装函数。
答案 1 :(得分:20)
您可以使用此库termcolor,您可以在PyPi中获取python的所有其他官方库
请参阅pypi.python.org中的文档或按照以下步骤进行操作
- pip install termcolor
- 然后转到ipython
醇>
<强>代码强>
from termcolor import colored
print colored('hello', 'red'), colored('world', 'green')
print colored("hello red world", 'red')
<强>输出:强>
hello world
hello red world
第一个参数是您想要在控制台上打印的内容,第二个参数使用该颜色
答案 2 :(得分:14)
这是一个快速黑客:
public HttpResponseMessage GetSample()
{
string result = "data1,data2,data3";
var response = new HttpResponseMessage(HttpStatusCode.OK);
response .Content = new StringContent(result, System.Text.Encoding.UTF8, "text/plain");
return response ;
}
[OUT]:
如果您只想要一种颜色:from IPython.display import HTML as html_print
def cstr(s, color='black'):
return "<text style=color:{}>{}</text>".format(color, s)
left, word, right = 'foo' , 'abc' , 'bar'
html_print(cstr(' '.join([left, cstr(word, color='red'), right]), color='black') )
答案 3 :(得分:13)
不使用原始Python打印。您必须在对象上定义_repr_html_
并将其返回或致电IPython.lib.display(object_with_repr_html)
。
我想你可以覆盖内置的打印件来自动执行...
你可以从http://nbviewer.ipython.org/5098827,代码in a gist on github,ML讨论here中获得灵感。
答案 4 :(得分:9)
类似于@alvas提到的内容,但更简单
from IPython.display import Markdown
display (Markdown('this is in <span style="color: #ff0000">red</span> color.'))
答案 5 :(得分:6)
有colored library(var drives = Utils.GetAllAvailableVolumes();
),您可以使用它来修改字符串以获取颜色代码以修改其打印方式。使用示例:
pip install colored
答案 6 :(得分:2)
colorama
使事情变得简单:
from colorama import Fore
print(Fore.RED + "this is red")
答案 7 :(得分:1)
使用@Evert回答,这是一个将标记列表换成红色并返回突出显示的字符串的方法
def color_in_tokens(tokens, color_token_contains="_"):
"""
Highlights the tokens which contain 'color_token_contains'
:param tokens: list of strings
:param color_token_contains: str (the string for marking a token red)
:return: str
"""
return " ".join(["\x1b[31m%s\x1b[0m" % i if color_token_contains in i else i for i in tokens])
答案 8 :(得分:1)
红色文字:
print("\x1b[31mText in red")
粗体文本:
print("\x1B[1mText in bold")
带下划线的文本
print("\x1B[4mText in underline")
请参阅“样式和颜色”一章下的here,并查看适合您的方法。 RGB颜色对我不起作用。
答案 9 :(得分:1)
在Jupiter笔记本中,我们可以使用颜色标记以不同颜色打印输出 我们可以使用三种方式使用颜色标记。
让我们看看如何在笔记本上打印黄色。
print('\033[93m output')
print('\033[93m' 'output')
print('\033[93m' + 'output')
其中一些颜色是:
您可以更改最后一个数字并获得其他颜色。
答案 10 :(得分:0)
感谢@alvas函数并添加另一个函数,我们获得了一种非常简单的打印方式
from IPython.display import HTML as html_print
from IPython.display import display
def cstr(s, color='black'):
return "<text style=color:{}>{}</text>".format(color, s)
def print_color(t):
display(html_print(' '.join([cstr(ti, color=ci) for ti,ci in t])))
print_color((('hello my name is', 'black'),('jhjfd','red')))
print_color((('hello my name is', 'green'),))
答案 11 :(得分:0)
来自:Standard for ANSI Colors in Terminals
此代码段可以在一行中显示所有颜色:
RESET = "\x1b[0m"
print("To reset attributes: \\x1b[0m\n")
for i in range(0, 8):
print("\x1b[1;3{0}m\\x1b[1;3{0}m{1} \x1b[0;3{0}m\\x1b[0;3{0}m{1} "
"\x1b[1;4{0};3{0}m\\x1b[1;3{0};4{0}m{1}".format(i, RESET))