在 ipython qtconsole 中打印粗体、彩色等文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23271575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
printing bold, colored, etc., text in ipython qtconsole
提问by David Schueler
I'm trying to get text to display as bold, or in colors, or possibly in italics, in ipython's qtconsole.
我试图让文本在 ipython 的 qtconsole 中显示为粗体、颜色或斜体。
I found this link: How do I print bold text in Python?, and used the first and second answers, but in qtconsole, only the underlining option works.
我找到了这个链接:如何在 Python 中打印粗体文本?,并使用了第一个和第二个答案,但在 qtconsole 中,只有下划线选项有效。
I try:
我尝试:
print '\033[1m' + 'Hello World!' + '\033[0m'
print '\033[1m' + 'Hello World!' + '\033[0m'
And get:
并得到:
Hello World!
Hello World!
(No boldface). The colors don't work either. But:
(没有粗体字)。颜色也不行。但:
print '\033[4m' + 'Hello World!' + '\033[0m'
print '\033[4m' + 'Hello World!' + '\033[0m'
And get:
并得到:
Hello World!
Hello World!
With underlining.
带下划线。
This is only in the qtconsole. Running ipython just in the terminal, it works to do boldface and color in this way.
这仅在 qtconsole 中。仅在终端中运行 ipython,它可以以这种方式进行粗体和颜色处理。
There were other options suggested in that link and another, Print in terminal with colors using Python?, linked from it, but they all seem more complex, and to use more elaborate packages, than seems necessary for what I want to do, which is simply to get qtconsole to display like the ordinary terminal does.
该链接中还建议了其他选项,另一个使用 Python 在终端中使用颜色打印?,从中链接,但它们似乎更复杂,并且使用更精细的包,而不是我想做的事情似乎是必要的,这只是让 qtconsole 像普通终端一样显示。
Does anyone know what's going on? Is this simply a limitation of the qtconsole?
有谁知道发生了什么?这仅仅是 qtconsole 的限制吗?
回答by Thomas K
Those are ANSI escapes, special sequences of characters which terminals process to switch font styles. The Qt console interprets some of them, but not all of the ones that serious terminals do. This sequence works to print in red, for instance:
这些是 ANSI 转义符,即终端处理以切换字体样式的特殊字符序列。Qt 控制台会解释其中的一些,但不是严肃终端所做的全部。此序列适用于以红色打印,例如:
print('\x1b[1;31m'+'Hello world'+'\x1b[0m')
However, if you're trying to write a cross platform application, be aware that the Windows command prompt doesn't handle these codes. Some of the more complex packages can process them to produce similar effects on Windows.
但是,如果您正在尝试编写跨平台应用程序,请注意 Windows 命令提示符不处理这些代码。一些更复杂的包可以处理它们以在 Windows 上产生类似的效果。
The Qt console can also display simple HTML, like this:
Qt 控制台还可以显示简单的 HTML,如下所示:
from IPython.display import HTML
HTML("<i>Italic text</i>")
But of course, HTML doesn't work in regular terminals.
但是当然,HTML 在常规终端中不起作用。
回答by Ali Sharifi B.
If you mean the body text of the iPython notebook (Markdowns), you can put 2 underline characters directly before and after your text to make it BOLD:
如果您指的是 iPython 笔记本 (Markdowns) 的正文文本,则可以在文本前后直接放置 2 个下划线字符以使其成为BOLD:
__BOLD TEXT__
=> BOLD TEXT
__BOLD TEXT__
=> 粗体文字
if you put a backslash before that, it will be counteracted:
如果在此之前加上反斜杠,它将被抵消:
\__BOLD TEXT__
=> __BOLD TEXT__
\__BOLD TEXT__
=> __粗体文字__
回答by Aadhya Manu Anand
Few more ways you can tweak around (I tried in iPython Notebook, not sure about other)..
还有更多方法可以调整(我在 iPython Notebook 中尝试过,不确定其他方法)。
**BOLD TEXT**
Above will produce bold text: BOLD TEXT
以上将产生粗体文本: BOLD TEXT
*__BOLD TEXT__*
will produce bold and italic text: BOLD TEXT
将产生粗体和斜体文本: BOLD TEXT
回答by Charles
In Jupyter Notebooks, one clean way of solving this problem is using markdown:
在 Jupyter Notebooks 中,解决此问题的一种简洁方法是使用 Markdown:
from IPython.display import Markdown, display
def printmd(string):
display(Markdown(string))
And then do something like:
然后执行以下操作:
printmd("**bold text**")
Of course, this is great for bold, italics, etc., but markdown itself does not implement color. However, you can place html in your markdown, and get something like this:
当然,这对于粗体、斜体等很有用,但 Markdown 本身并没有实现颜色。但是,您可以将 html 放在 Markdown 中,并得到如下内容:
printmd("<span style='color:red'>Red text</span>")
You could also wrap this in the printmd
function :
您也可以将其包装在printmd
函数中:
def printmd(string, color=None):
colorstr = "<span style='color:{}'>{}</span>".format(color, string)
display(Markdown(colorstr))
And then do cool things like
然后做一些很酷的事情,比如
printmd("**bold and blue**", color="blue")
For the colors, you can use the hexadecimal notation too (eg. color = "#00FF00"
for green)
对于颜色,您也可以使用十六进制表示法(例如color = "#00FF00"
绿色)
To clarify, although we use markdown, this is a codecell: you can do things like:
澄清一下,虽然我们使用 Markdown,但这是一个代码单元:您可以执行以下操作:
for c in ('green', 'blue', 'red', 'yellow'):
printmd("Writing in {}".format(c), color=c)
Of course, a drawback of this method is the reliance on being within a Jupyter notebook.
当然,这种方法的一个缺点是依赖于 Jupyter notebook。
回答by Jonasz
I would like to complete the previous incomplete answer. Way more complex and fun things can be done without importing additional packages. e.g.
我想完成之前不完整的答案。无需导入额外的包就可以完成更复杂和有趣的事情。例如
print('\x1b[1;03;31;46m'+'Hello'+ '\x1b[0;4;30;42m' + ' world' '\x1b[0m')
i.e.:
IE:
Open with:
打开用:
'\x1b[XX;YY;ZZm'
Close with:
关闭:
'\x1b[0m'
Where XX, YY and ZZ are numbers from: https://en.wikipedia.org/wiki/ANSI_escape_code
其中 XX、YY 和 ZZ 是来自:https://en.wikipedia.org/wiki/ANSI_escape_code 的数字
It should be noted that it is very much dependant on what you use as a console to see what works.
应该注意的是,它在很大程度上取决于您用作控制台的内容,以查看哪些内容有效。
Working for me are combinations of the following:
为我工作是以下的组合:
Text styling
文字样式
- 1 Increased intensity (it operates on highlight and text simultaneously in my case)
- 3 Itallic
- 4 Underline
- 1 增加强度(在我的情况下它同时对突出显示和文本进行操作)
- 3 斜体
- 4 下划线
Text colors
文字颜色
- 30 Black text
- 31 Dark Red text
- 32 Dark Green text
- 33 Red text
- 34 Dark blue text
- 35 Purple text
- 36 Blue text
- 37 Gray text
- 30 黑色文字
- 31 深红色文字
- 32 深绿色文字
- 33 红色文字
- 34 深蓝色文字
- 35紫色文字
- 36 蓝色文字
- 37 灰色文本
Bright text colors
明亮的文字颜色
- 1;30 Gray text (Bright black)
- 1;31 Orange text (Bright red)
- 1;32 Bright Green text
- 1;33 Bright Yellow text
- 1;34 Bright Blue text
- 1;35 Bright Purple text
- 1;36 Bright Cyan text
- 1;37 White text (Bright gray)
- 1;30 灰色文本(亮黑色)
- 1;31 橙色文本(亮红色)
- 1;32 亮绿色文本
- 1;33 亮黄色文本
- 1;34 亮蓝色文本
- 1;35 亮紫色文本
- 1;36 亮青色文本
- 1;37 白色文本(亮灰色)
Background colors (i.e. highlights)
背景颜色(即高光)
- 40 Black highlight
- 41 Dark Red highlight
- 42 Dark Green highlight
- 43 Red highlight
- 44 Dark blue highlight
- 45 Purple highlight
- 46 Blue highlight
- 47 Gray highlight
- 40 黑色高光
- 41暗红色高光
- 42深绿色高光
- 43 红色高光
- 44 深蓝色高光
- 45紫色高光
- 46蓝色高光
- 47 灰色高光
Note that 1;42 etc. also works similarly
请注意, 1;42 等也类似
Tested on windows 7, python 3.6, IPython console, in spyder 3.2.3 this works for me
在 windows 7、python 3.6、IPython 控制台上测试,在 spyder 3.2.3 这对我有用