pythonipythoncustomization

How do I customize text color in IPython?


I'd like to customize the color of text in IPython, but am not sure how to do it.

I know that in Python, I can do this by ending sys.ps1 and sys.ps2 with an ANSI color code such as

sys.ps1=">>> \001\033[0m\033[34m\002"

But the corresponding approach, using PromptManager.in_template, does not work for IPython. For example

c = get_config()
c.PromptManager.in_template = 'In [{count}] : {color.Blue}'

has no effect on the color of text after the prompt.

Is there a way to change the color of text in IPython?


Solution

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