Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printing bold, colored, etc., text in ipython qtconsole

I'm trying to get text to display as bold, or in colors, or possibly in italics, in ipython's 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.

I try:

print '\033[1m' + 'Hello World!' + '\033[0m'

And get:

Hello World!

(No boldface). The colors don't work either. But:

print '\033[4m' + 'Hello World!' + '\033[0m'

And get:

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.

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.

Does anyone know what's going on? Is this simply a limitation of the qtconsole?

like image 393
David Schueler Avatar asked Apr 24 '14 14:04

David Schueler


People also ask

How do I print text in bold and color in Python?

\033[1m is the escape code for bold in the terminal. \033[0m is the escape code for end the edited text and back default text format. If you do not use \033[0m then all upcoming text of the terminal will become bold.

How do you print a string in bold in Python?

The ANSI escape sequence to print bold text in Python is: '\033[1m'.

How do you make bold letters in Jupyter notebook?

Use the following code to emphasize text: Bold text: __string__ or **string** Italic text: _string_ or *string*

What is Ipython QT console?

Jupyter QtConsole The Qtconsole is a very lightweight application that largely feels like a terminal, but provides a number of enhancements only possible in a GUI, such as inline figures, proper multiline editing with syntax highlighting, graphical calltips, and more.


1 Answers

In Jupyter Notebooks, one clean way of solving this problem is using 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:

printmd("<span style='color:red'>Red text</span>") 

You could also wrap this in the printmd function :

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)

To clarify, although we use markdown, this is a code cell: you can do things like:

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.

like image 127
Charles Avatar answered Oct 02 '22 11:10

Charles