在以前版本的IPython中,通过在配置中包含以下内容,很容易在提示中包含您的主机名:
c.PromptManager.in_template = '(\H) In [\\#]: '
(\H
将替换为您的主机名。)
但是,IPython 5中已删除PromptManager
配置。当我尝试使用它时,我看到以下警告:
/env/lib/python2.7/site-packages/IPython/core/interactiveshell.py:448: 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'
那么如何在IPython 5中实现类似的效果呢?
答案 0 :(得分:2)
如警告所示,您应该使用新的TerminalInteractiveShell.prompts_class
。因此,要在提示中包含主机名,可以在配置中删除以下内容:
from IPython.terminal.prompts import Prompts, Token
import socket
class MyPrompt(Prompts):
def __init__(self, *args, **kwargs):
hn = socket.gethostname()
self._in_txt = '({}) In ['.format(hn)
self._out_txt = '({}) Out['.format(hn)
super(MyPrompt, self).__init__(*args, **kwargs)
def in_prompt_tokens(self, cli=None):
return [
(Token.Prompt, self._in_txt),
(Token.PromptNum, str(self.shell.execution_count)),
(Token.Prompt, ']: '),
]
def out_prompt_tokens(self):
return [
(Token.OutPrompt, self._out_txt),
(Token.OutPromptNum, str(self.shell.execution_count)),
(Token.OutPrompt, ']: '),
]
以下提示中的结果:
(marv) In [1]: 'hello, world'
(marv) Out[1]: 'hello, world'