printing bold, colored, etc., text in ipython qtconsole
Asked Answered
D

5

45

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?

Damondamour answered 24/4, 2014 at 14:18 Comment(0)
O
105

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.

Oman answered 25/10, 2017 at 13:48 Comment(3)
+1 One of the answers that helps in Jupyter / IPython 5.7.8 with Python 3.7 (used under firefox and Ubunutu)!Bernat
Great for tag results in Jupyter notebook, when there are multiple similar ones in a funciton.Fribourg
This helped greatly. Also, I combined it with f-strings. I called display(Markdown(f"text and {variable}")) and it works like a charm. Not only bold either, all markdown features seem at reach. For example, I used the "#" character that creates markdown titles in Jupyter Notebooks to generate titles that updated my index.Rhombus
A
27

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:

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.

The Qt console can also display simple HTML, like this:

from IPython.display import HTML
HTML("<i>Italic text</i>")

But of course, HTML doesn't work in regular terminals.

Aliform answered 24/4, 2014 at 16:50 Comment(1)
I found this link with more colours and highlightingSobriety
D
26

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.:

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

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

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

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)

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

Note that 1;42 etc. also works similarly

Tested on windows 7, python 3.6, IPython console, in spyder 3.2.3 this works for me

Decagon answered 13/11, 2017 at 17:57 Comment(0)
N
-3

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:

__BOLD TEXT__ => BOLD TEXT

if you put a backslash before that, it will be counteracted:

\__BOLD TEXT__ => __BOLD TEXT__

Nakamura answered 23/11, 2015 at 11:4 Comment(1)
Requires markdown cell. Question was about using code cells.Oman
F
-3

Few more ways you can tweak around (I tried in iPython Notebook, not sure about other)..

**BOLD TEXT**

Above will produce bold text: BOLD TEXT

*__BOLD TEXT__*

will produce bold and italic text: BOLD TEXT

Fettle answered 2/4, 2016 at 13:54 Comment(1)
It only works in markdown cell. You can't get it work by print('Text')Cislunar

© 2022 - 2024 — McMap. All rights reserved.