'custom ipython prompt messing with prev-history `up-arrow`-key

I customized a script I found a bit for my own custom iPython prompt. However when I press arrow up I get the history from the previous session or something. I've been using recall with the previous line number a lot :D

Here is the code, I hope someone can tell me what could be causing this ...

edit I found the solution, it was quite simple, but I thought I'd share and eventually help someone with this nice feature

from IPython.terminal.prompts import Prompts, Token
from pathlib import Path
import os
from platform import python_version
import subprocess

def get_branch():
    try:
        return (
            subprocess.check_output(
                "git branch --show-current", shell=True, stderr=subprocess.DEVNULL
            )
            .decode("utf-8")
            .replace("\n", "")
        )
    except BaseException:
        return ""


def get_env(key):
    if key in os.environ:
       return os.environ[key]
    else:
       return key
 

def explore_tokens(i):
    tokens = ('Aborted', 'AutoSuggestion', 'ColorColumn', 'Comment',
              'CursorColumn', 'CursorLine', 'Digraph', 'Error',
              'Escape', 'Generic', 'Keyword', 'LeadingWhiteSpace',
              'LineNumber', 'Literal', 'MatchingBracket', 'Menu',
              'MultipleCursors', 'Name', 'Number', 'Operator',
              'Other', 'OutPrompt', 'OutPromptNum', 'Prompt',
              'PromptNum', 'Punctuation', 'Scrollbar', 'SearchMatch',
              'SelectedText', 'SetCursorPosition', 'String', 'Tab',
              'Text', 'Tilde', 'Token', 'Toolbar',
              'TrailingWhiteSpace', 'Transparent', 'WindowTooSmall',
              'ZeroWidthEscape')


class MyPrompt(Prompts):
    
    def get_line_number(self):
#         return str(list(_oh.keys())[-1])
        line_no = self.shell.execution_count
        return str(line_no)
   
    def in_prompt_tokens(self, cli=None):
        return [
            (Token, ""),
#            (Token.OutPromptNum, 0),
            (Token.OutPrompt, Path().absolute().name), #.stem
            (Token, " "),
            (Token.Generic.Subheading, get_branch()),
            (Token, " "),
            (Token.Prompt, "@"),
            (Token.Prompt, get_env("NAME")),
            (Token, " "),
            (Token.Name.Class, "v" + python_version()),
            (Token, " "),
            (Token.Name.Entity, "ipython"),
            (Token, "\n"),
            (Token.LineNumber, "["),
            (Token.LineNumber, self.get_line_number()),
            (Token.LineNumber, "]"),
            (
                Token.Prompt
                if self.shell.last_execution_succeeded
                else Token.Generic.Error,
                "|: ",
            ),
#            (Token.Generic.Subheading, "Ħ"),
        ]

    def out_prompt_tokens(self, cli=None):
        return [
#            (Token.PromptNum,self.get_line_number()), # disabled for easier copy pasting of blocks

        ]


ipy = get_ipython()
ipy.prompts = MyPrompt(ipy)


Solution 1:[1]

As happens a number of times, before even posting this I found the solution. The out_prompt_tokens should not return an empty array, adding

(Prompt, "> "),

after the commented out line, fixed it So now it looks like

    def out_prompt_tokens(self, cli=None):
        return [
#            (Token.PromptNum,self.get_line_number()), # disabled for easier copy pasting of blocks
                (Token.Prompt, "> "),

        ]

(Or I could just have enabled the PromptNum line again)

I hope it might help anyone else

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 oneindelijk