How do I customize text color in IPython?

Colorize and Syntax Style in IPython

First you have to create a ipython profile ~/.iphyton/ipython_config.py in your home directory. ipython_config.py. The easiest way to do so is to run the following command:

ipython profile create

In case you are using ipython3 start

ipython3 profile create

This will install a profile_default directory and some scripts in your ~/.ipython; otherwise find a file and copy it in your ~/.ipython/profile_default/ directory.

Make a backup of this file with:

cp ~/.ipython/profile_default/ipython_config.py \
   ~/.ipython/profile_default/ipython_config.py_backup

Open the ~/.ipython/profile_default/ipython_config.py with an text editor of your choice and search for following settig and comment it out if you like it (delete the '#'):

  • c.InteractiveShell.color_info = True

  • c.InteractiveShell.colors = 'Linux'

  • c.TerminalInteractiveShell.highlighting_style = 'monokai'

  • c.TerminalInteractiveShell.highlight_matching_brackets = True

and so on. There are many usefull settings which are disabled by default; you have only to comment them out (deleting the `#' ).

Style Files

On Ubuntu/Debian you have to install the pygments package

sudo apt install python3-pygments

or

sudo pip3 install pygments 

The style files can be found in following directory:

/path/to/your/python/site-packages/pygments/styles/, e.g. /usr/lib/python3/dist-packages/pygments/styles/monokai.py

Alternatively, you can also list your installed styles with pygmentize:

pygmentize -L styles

The prompt explicitly sets the color of input to colors.in_normal. If you want to change the input color, you have to change this color.

Unfortunately, customized color schemes are still on the todo list (should be pretty easy, just not a high priority).

A somewhat hackish example of what you seem to want, changing the color of input and/or output text in a config file:

from IPython.utils import coloransi
from IPython.core import prompts

termcolors = coloransi.TermColors() # the color table
# IPython's two color schemes:
dark = prompts.PColLinux.colors
light = prompts.PColLightBG.colors

# colors.in_normal affects input code
dark.in_normal = termcolors.Green
light.in_normal = termcolors.Blue
# colors.normal affects output
dark.normal = light.normal = termcolors.Red

This will set it so that the color of text matches the prompt, but you can of course choose whatever you want.


Warning: This Answer is only compatible with IPython 5.5.0 and it does not seem to translate well with IPython 6+. (thx @meowsqueak for the quick test !)

As of IPython 5.5.0, you can override any styling color thanks to the ipython_config.py in the targetted profile folder. (ie: ~/.ipython/profile_default for default profile on a linux typical install).

IPython code is quite a mess related to colorizing and several methods are used for parsers, debuggers, interactive shell. Some part use Pygments, some other provide ANSI escape code that are limited to a 16 colors palette.

The solution is not pretty, but works. If there are any other prettier other way, please tell me in comment !.

Please note that my solution also includes the ability to use 256 colors or more for every part of IPython coloring, thanks to an extending of the default palette. There's an example of how to extend to 256 colors in the following code.

So here's how to do, with a list of all tokens that might be used:

##
## Basic color scheme that will be modified
##

colorLabel = 'Linux'
c.InteractiveShell.colors = colorLabel


from pygments.token import Token, Keyword, Name, Comment, String, Error, \
     Number, Operator, Generic, Whitespace

c.TerminalInteractiveShell.highlighting_style_overrides = {

    ## Standard Pygments tokens (are all used by IPython ?)

    Whitespace:                "#bbbbbb",
    Comment:                   "italic #008800",
    Comment.Preproc:           "noitalic",
    Comment.Special:           "noitalic bold",

    Keyword:                   "bold #AA22FF",
    Keyword.Pseudo:            "nobold",
    Keyword.Type:              "bold #00BB00",

    Operator:                  "#666666",
    Operator.Word:             "bold #AA22FF",

    Name.Builtin:              "#fff", #""#AA22FF",
    Name.Function:             "#00A000",
    Name.Class:                "#0000FF",
    Name.Namespace:            "bold #0000FF",
    Name.Exception:            "bold #D2413A",
    Name.Variable:             "#B8860B",
    Name.Constant:             "#880000",
    Name.Label:                "#A0A000",
    Name.Entity:               "bold #999999",
    Name.Attribute:            "#BB4444",
    Name.Tag:                  "bold #008000",
    Name.Decorator:            "#AA22FF",

    String:                    "#BB4444",
    String.Doc:                "italic",
    String.Interpol:           "bold #BB6688",
    String.Escape:             "bold #BB6622",
    String.Regex:              "#BB6688",
    String.Symbol:             "#B8860B",
    String.Other:              "#008000",
    Number:                    "#666666",

    Generic.Heading:           "bold #000080",
    Generic.Subheading:        "bold #800080",
    Generic.Deleted:           "#A00000",
    Generic.Inserted:          "#00A000",
    Generic.Error:             "#FF0000",
    Generic.Emph:              "italic",
    Generic.Strong:            "bold",
    Generic.Prompt:            "bold #000080",
    Generic.Output:            "#888",
    Generic.Traceback:         "#04D",

    Error:                     "border:#ff0000",

    ## IPython

    Token.Number: '#ffffff',
    Token.Operator: 'noinherit',
    Token.String: '#8b8',
    Token.Name.Function: '#2080D0',
    Token.Name.Class: 'bold #2080D0',
    Token.Name.Namespace: 'bold #2080D0',
    Token.Prompt: '#ffffff bold',
    Token.PromptNum: '#888888 bold',
    Token.OutPrompt: '#008b8b bold',
    Token.OutPromptNum: '#006b6b bold',
}


from IPython.core import excolors, ultratb, debugger
from IPython.core.excolors import exception_colors as exception_colors_orig

##
## Add new color labels here before attributing them
##


from IPython.utils import coloransi

coloransi.color_templates = (
    # Dark colors

    ("Black"       , "0;30"),
    ("Red"         , "0;31"),
    ("Green"       , "0;32"),
    ("Brown"       , "0;33"),
    ("Blue"        , "0;34"),
    ("Purple"      , "0;35"),
    ("Cyan"        , "0;36"),
    ("LightGray"   , "0;37"),

    # Light colors
    ("DarkGray"    , "1;30"),
    ("LightRed"    , "1;31"),
    ("LightGreen"  , "1;32"),
    ("Yellow"      , "1;33"),
    ("LightBlue"   , "1;34"),
    ("LightPurple" , "1;35"),
    ("LightCyan"   , "1;36"),
    ("White"       , "1;37"),

    ## 256-colors

    ("Green108", "38;5;108"),
)

coloransi.make_color_table(coloransi.TermColors)
coloransi.make_color_table(coloransi.InputTermColors)

for name, value in coloransi.color_templates:
    setattr(coloransi.NoColors, name, '')


C = coloransi.TermColors
IC = coloransi.InputTermColors


def exception_colors():

    ex_colors = exception_colors_orig()

    ex_colors.add_scheme(coloransi.ColorScheme(
        colorLabel,

        # The color to be used for the top line
        topline=C.LightRed,

        # The colors to be used in the traceback
        filename=C.Green,
        lineno=C.DarkGray,
        name=C.Purple,
        vName=C.Cyan,
        val=C.White,
        em=C.LightCyan,

        # Emphasized colors for the last frame of the traceback
        normalEm=C.LightCyan,
        filenameEm=C.Green,
        linenoEm=C.Normal,
        nameEm=C.LightPurple,
        valEm=C.LightGreen,

        # Colors for printing the exception
        excName=C.Red,
        line=C.Yellow,
        caret=C.White,
        Normal=C.Normal
    ))
    return ex_colors

excolors.exception_colors = exception_colors
ultratb.exception_colors = exception_colors
debugger.exception_colors = exception_colors


##
## Parser color (source code colors)
##

from IPython.utils import PyColorize
import token
import tokenize

PyColorize.ANSICodeColors[colorLabel] = coloransi.ColorScheme(
    colorLabel, {
        'header'         : C.LightRed,
        token.NUMBER     : C.LightCyan,
        token.OP         : C.Normal,
        token.STRING     : C.Green108,
        tokenize.COMMENT : C.LightGray,
        token.NAME       : C.Normal,
        token.ERRORTOKEN : C.Red,

        PyColorize._KEYWORD         : C.White,
        PyColorize._TEXT            : C.Yellow,

        ## Keep IC here, you can use other colors

        'in_prompt'      : IC.Green,
        'in_number'      : IC.LightGreen,
        'in_prompt2'     : IC.Green,
        'in_normal'      : IC.Normal,  # color off (usu. Colors.Normal)

        'out_prompt'     : C.Red,
        'out_number'     : C.LightRed,

        'normal'         : C.Normal  # color off (usu. Colors.Normal)
})

The easy way to change colors in Ipython:

  1. Open your IPython config file (ipython_config.py) , normally at ~/.ipython/profile_default/ or in ~/.ipython/profile_<your_usrname>/ (if you are using IPython profiles).

  2. Find and uncomment (or change) setting c.InteractiveShell.colors

  3. Change it to one of the color schemas available you can see them all listed here in the comments - NoColor, Neutral, Linux, or LightBG

  4. Save config file and start IPython

If you do not see the config file - you can generate it quickly with the command:

ipython profile create [profilename]

It will generate config file with default settings and you can edit it then as needed.