自定义IPython提示符

首先创建IPython的自定义配置文件

$ ipython profile create

可以看到在HOME目录下: 多了两个配置文件

我们修改~/.ipython/profile_default/ipython_config.py文件, 在文件的最底部,
加入如下代码:

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

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

    def out_prompt_tokens(self):
       return []

c.TerminalInteractiveShell.prompts_class = MyPrompt

重新打开IPython, 实现效果如下:

$ ipython
Python 3.6.1 (default, Apr  6 2017, 11:37:13)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

>>> import numpy as np

>>> a = np.arange(15).reshape(3, 5)

>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

在看了ipython的代码IPython/terminal/prompts.py时,发现它已经定义好了经典提示符的风格ClassicPrompts。不需要我们再自定义实现了,直接在配置文件~/.ipython/profile_default/ipython_config.py里配置使用它即可

c.TerminalInteractiveShell.prompts_class = 'IPython.terminal.prompts.ClassicPrompts'

参考https://ipython.readthedocs.io/en/stable/config/details.html#custom-prompts

原文地址:https://www.cnblogs.com/ZeroTensor/p/10452470.html