Ask Your Question

Revision history [back]

These are really two distinct questions. The IPython history can be printed with line numbers. This is the option -n

sage: %hist -n

1: %hist -n

To see all available option, do in the sage console %history?.

Secondly, to customize the prompt you should write your own Prompts class. This is documented at https://ipython.readthedocs.io/en/stable/config/details.html (I also found the code of IPython useful https://github.com/ipython/ipython/blob/master/IPython/terminal/prompts.py)

sage: ip = get_ipython()
sage: from IPython.terminal.prompts import Prompts, Token
sage: import os
sage: class MyPrompts(Prompts):
....:      def in_prompt_tokens(self):
....:            return [(Token.InPrompt, 'sage['),
....:                    (Token.PromptNum, str(self.shell.execution_count)),
....:                    (Token.InPrompt, ']: ')]
....:      def continuation_prompt_tokens(self, width=None):
....:          if width is None:
....:              width = self._width()
....:          return [(Token.Prompt, '.' * (width-2) + ': ')]
....:      def out_prompt_tokens(self):
....:           return [(Token.OutPrompt, '_'),
....:                   (Token.OutPromptNum, str(self.shell.execution_count)),
....:                   (Token.OutPrompt, ' = ')]
sage: ip.prompts = MyPrompts(ip)
sage[6]: 1+3
_6 = 4
sage[7]: 5+5
_7 = 10

These are really two distinct questions. The IPython history can be printed with line numbers. This is the option -n

sage: %hist -n

1: %hist -n

-n

To see all available option, do in the sage console %history?.

Secondly, to customize the prompt you should write your own Prompts class. This is documented at https://ipython.readthedocs.io/en/stable/config/details.html (I also found the code of IPython useful https://github.com/ipython/ipython/blob/master/IPython/terminal/prompts.py)

sage: ip = get_ipython()
sage: from IPython.terminal.prompts import Prompts, Token
sage: import os
sage: class MyPrompts(Prompts):
....:      def in_prompt_tokens(self):
....:            return [(Token.InPrompt, 'sage['),
....:                    (Token.PromptNum, str(self.shell.execution_count)),
....:                    (Token.InPrompt, ']: ')]
....:      def continuation_prompt_tokens(self, width=None):
....:          if width is None:
....:              width = self._width()
....:          return [(Token.Prompt, '.' * (width-2) + ': ')]
....:      def out_prompt_tokens(self):
....:           return [(Token.OutPrompt, '_'),
....:                   (Token.OutPromptNum, str(self.shell.execution_count)),
....:                   (Token.OutPrompt, ' = ')]
sage: ip.prompts = MyPrompts(ip)
sage[6]: 1+3
_6 = 4
sage[7]: 5+5
_7 = 10