Skip to content Skip to sidebar Skip to footer

Adding Color To New Style Ipython (v5) Prompt

Update to the newly release ipython5 today. Started up the interactive prompt and received: /usr/local/lib/python3.5/site-packages/IPython/core/interactiveshell.py:440: UserWarning

Solution 1:

from IPython.terminal.prompts import Prompts, Token
import os

classMyPrompt(Prompts):

    defin_prompt_tokens(self, cli=None):   # defaultreturn [
            (Token.Prompt, 'In ['),
            (Token.PromptNum, str(self.shell.execution_count)),
            (Token.Prompt, ']: '),
        ]

    defin_prompt_tokens(self, cli=None):  # samplereturn [(Token, os.getcwd()),
                 (Token.Prompt, ' >>>')]

    defin_prompt_tokens(self, cli=None):   # custom
        path = os.path.basename(os.getcwd())
        return [
            (Token.Prompt, '<'),
            (Token.PromptNum, '~/'+path),
            (Token.Prompt, '>'),
            (Token.Prompt, '['),
            (Token.PromptNum, str(self.shell.execution_count)),
            (Token.Prompt, ']: '),
        ]

    defin_prompt_tokens(self, cli=None):   # custom
        path = os.path.basename(os.getcwd())
        return [
            (Token.PromptNum, str(self.shell.execution_count)),
            (Token.Prompt, ':'),
            (Token.PromptNum, '~/'+path),
            (Token.Prompt, '$ '),
        ]

"""
use:
import myprompt as MP
ip=get_ipython()
ip.prompts=MP.MyPrompt(ip)
"""

I experimented with various prompts with this script. It includes the default in_prompt_tokens method, the example customization and a couple of alternatives. The last imitates my bash prompt

73:~/mypy$ 

In looks like the tuple (Token..., str) sets the color of the string according to the token_type. Token, Token.Prompt, Token.PromptNum are possible types. Look at Token.<tab> for more (such as OutPrompt(Num)).

IPython/terminal/prompts.py

I probably won't use any of these because I like the default matching In /Out pairs. Besides I can use --term-title to show the directory in the tab title.

Solution 2:

Simple example showing how to color the prompt in red:

from IPython.terminal.prompts importTokenipy_config= get_config()

ipy_config.TerminalInteractiveShell.highlighting_style_overrides = {
    Token.Prompt: '#ff0000',
}

More advanced example that changes prompt color according to environment variable (useful if you play alot with staging/live systems):

# Example showing how to change color of prompt and prompt string in specific environments.# put into ~/.ipython/profile_${YOURPROFILE}/ipython_config.py and start ipython with:# PROFILE_LIVE ipython --profile ${YOURPROFILE}import os

from IPython.terminal.prompts import Prompts, Token

ipy_config = get_config()


classMyPrompt(Prompts):
    environment = Nonedefin_prompt_tokens(self, cli=None):
        return [
            (Token.Prompt, '{} ['.format(MyPrompt.environment)),
            (Token.PromptNum, str(self.shell.execution_count)),
            (Token.Prompt, ']: ')
        ]


if'PROFILE_LIVE'in os.environ:
    ipy_config.TerminalInteractiveShell.highlighting_style_overrides = {
        Token.Prompt: '#ff0000',
    }
    MyPrompt.environment = 'LIVE'
    ipy_config.TerminalInteractiveShell.prompts_class = MyPrompt

Post a Comment for "Adding Color To New Style Ipython (v5) Prompt"