【问题标题】:Adding Color to new style ipython (v5) prompt为新样式 ipython (v5) 提示添加颜色
【发布时间】:2016-11-11 13:11:31
【问题描述】:

今天更新到新发布的 ipython5。启动交互提示并收到:

/usr/local/lib/python3.5/site-packages/IPython/core/interactiveshell.py:440: UserWarning: As of IPython 5.0 `PromptManager` config will have no effect and has been replaced by TerminalInteractiveShell.prompts_class
warn('As of IPython 5.0 `PromptManager` config will have no effect'

抽出我的旧配置设置来自定义和着色提示并寻找自定义提示的新方法并发现它,非常酷。使用了example code中的新类样式:

class MyPrompt(Prompts):
    def in_prompt_tokens(self, cli=None):
        return [(Token, os.getcwd()),
                (Token.Prompt, ' >>>')]

把它放到一个启动脚本中,它工作得很好,除了默认情况下它不会给 Token 行着色,Token.Prompt 是浅绿色的。

尝试使用旧的配置方法颜色 (r'{color.Green}') 但这在这里不起作用。任何指向正确方向的指针都会很棒。

谢谢!

【问题讨论】:

  • 我喜欢这个新的 Ipython 的多行编辑。可能需要一些时间来适应不同的选项卡完成行为。

标签: python python-3.x ipython


【解决方案1】:
from IPython.terminal.prompts import Prompts, Token
import os

class MyPrompt(Prompts):

    def in_prompt_tokens(self, cli=None):   # default
        return [
            (Token.Prompt, 'In ['),
            (Token.PromptNum, str(self.shell.execution_count)),
            (Token.Prompt, ']: '),
        ]

    def in_prompt_tokens(self, cli=None):  # sample
        return [(Token, os.getcwd()),
                 (Token.Prompt, ' >>>')]

    def in_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, ']: '),
        ]

    def in_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)
"""

我用这个脚本尝试了各种提示。它包括默认的in_prompt_tokens 方法、示例自定义和一些替代方法。最后模仿我的bash提示

73:~/mypy$ 

In 看起来像元组(Token..., str) 根据token_type 设置字符串的颜色。 TokenToken.PromptToken.PromptNum 是可能的类型。查看Token.&lt;tab&gt; 了解更多信息(例如OutPrompt(Num))。

IPython/terminal/prompts.py

我可能不会使用这些,因为我喜欢默认匹配的In /Out 对。此外,我可以使用--term-title 在标签标题中显示目录。

【讨论】:

  • 我的 VIM 自动完成功能没有为 Token 做任何事情。所以我继续前进。从 ipython 终端本身尝试过,并且可以正常工作。这有帮助,非常感谢。
  • 在上面的后面加上c.InteractiveShell.prompts_class = MyPrompt ~/.ipython/profile_default/ipython_config.py(因为get_ipython()那里返回None)。感谢newton.cx/~peter/2015/11/extensible-prompts-in-jupyter-console
【解决方案2】:

显示如何将提示设置为红色的简单示例:

from IPython.terminal.prompts import Token

ipy_config = get_config()

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

根据环境变量更改提示颜色的更高级示例(如果您经常使用 staging/live 系统,则很有用):

# 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()


class MyPrompt(Prompts):
    environment = None

    def in_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

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 2014-06-27
    • 2021-09-08
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    相关资源
    最近更新 更多