0

I am trying to use colorama in Jupyter notebook and it doesn't do anything. However when try it in console it works fine. Here my sample code:

from sys import stdout
from colorama import Fore

# Option 1
stdout.write(Fore.RED + "Test")

# Option 2
print(Fore.GREEN + "Test")

My goal is to print different elements on the same line, with different colors.

I'm on Linux (Ubuntu 20) and using Python 2.7. The same issue occurs when I try it in python3

Adam Boinet
  • 521
  • 8
  • 22
Ivan
  • 27
  • 6

1 Answers1

1

You could use some markdown here, using Markdown and display from the IPython.display module.

I believe this answer may be what you're looking for.


EDIT

Based on the answer to the question you referenced to in your comment, here is some code that prints different elements on the same line, with the same color:

In [1]:     class ListOfColoredStrings(object):
                def __init__(self, *args):
                    """
                    Expected input:
                    args = ["word_1", "color_1"], ["word_2", "color_2"]

                    :param args: pairs of [word, color], both given as strings
                    """
                    self.strings = [a[0] for a in args]
                    self.colors = [a[1] for a in args]

                def _repr_html_(self):
                    return ''.join( [
                       "<span class='listofstr' style='color:{}'>{}</span>"
                            .format(self.colors[i], self.strings[i])
                       for i in range(len(self.strings))
                       ])

In [2]:     %%html
            <style type='text/css'>
            span.listofstr {
              margin-left: 5px
            }
            </style>

In [3]:     ListOfColoredStrings(["hi", "red"], ["hello", "green"])

Output:

output

Adam Boinet
  • 521
  • 8
  • 22
  • Yes, that works great until I need each print being on a new line But I need different colored characters be side by side and [this](https://stackoverflow.com/questions/17439176/ipython-notebook-how-to-display-multiple-objects-without-newline) shows it's too complicated – Ivan Jun 18 '20 at 09:28
  • @Ivan I just updated my answer, I hope this is what you were looking for – Adam Boinet Jun 18 '20 at 15:55
  • 1
    Yeah, this way might be a bit long, but it perfectly solves the problem so thank you very much ! – Ivan Jun 19 '20 at 16:07