[IPython-User] New IPython 5 prompt

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

[IPython-User] New IPython 5 prompt

Thorsten Kampe
Hello,

I'm at loss how to configure the new IPython 5 prompt. How can I
replace my old prompt (see below) using the new
`TerminalInteractiveShell.prompts_class`?

This is my old prompt:
"""
config = get_config()

PM  = config.PromptManager
PM.in_template  = '{color.LightCyan}>>> '
PM.in2_template = '{color.LightCyan}... '
PM.out_template = ''
"""

Thorsten

_______________________________________________
IPython-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|

Re: New IPython 5 prompt

Carl Smith
This was discussed here a little while ago. You can probably figure out what you need from that:


Best,

-- Carl Smith

On 21 August 2016 at 11:45, Thorsten Kampe <[hidden email]> wrote:
Hello,

I'm at loss how to configure the new IPython 5 prompt. How can I
replace my old prompt (see below) using the new
`TerminalInteractiveShell.prompts_class`?

This is my old prompt:
"""
config = get_config()

PM  = config.PromptManager
PM.in_template  = '{color.LightCyan}>>> '
PM.in2_template = '{color.LightCyan}... '
PM.out_template = ''
"""

Thorsten

_______________________________________________
IPython-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-user


_______________________________________________
IPython-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|

Re: New IPython 5 prompt

Thorsten Kampe
* Carl Smith (Sun, 21 Aug 2016 13:32:11 +0100)
> This was discussed here a little while ago. You can probably figure
> out what you need from that:
>
> http://python.6.x6.nabble.com/IPython-5-Docs-Custom-Terminal-Prompts-td5194562.html

Thanks, it looks like the documentation has been updated after the
attempt to work out the new way of customzing prompts:
<http://ipython.readthedocs.io/en/latest/config/details.html#MyPrompt
s>

So this is what I came up with:
"""
config = get_config()
TIS = config.TerminalInteractiveShell

from IPython.terminal.prompts import Prompts, Token

class CustomPrompts(Prompts):

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

    def out_prompt_tokens(self):
        return [(Token.OutPrompt, '')]

    def continuation_prompt_tokens(self, cli = None, width = None):
        return [(Token.Prompt, '...')]

TIS.prompts_class = CustomPrompts
"""

Unfortunately setting color (like `'{color.LightCyan}>>> '` seems not
to be supported anymore.

In all, a substantial loss of ease of configuration for IPython
users. "pretty low lever" as the developer said.

Thorsten

_______________________________________________
IPython-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|

Re: New IPython 5 prompt

Thomas Kluyver-2
On 21 August 2016 at 15:46, Thorsten Kampe <[hidden email]> wrote:
Unfortunately setting color (like `'{color.LightCyan}>>> '` seems not
to be supported anymore.

If you do what you have already shown, then you can set a colour for the prompt token:

TIS.highlighting_style_overrides = {Token.Prompt: '#0077FF'}

We appreciate this is a bit more fiddly for simple custom prompts, but the advantages of using prompt_toolkit outweigh it.

_______________________________________________
IPython-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|

Re: New IPython 5 prompt

Carl Smith
On 21 August 2016 at 16:05, Thomas Kluyver <[hidden email]> wrote:

We appreciate this is a bit more fiddly for simple custom prompts, but the advantages of using prompt_toolkit outweigh it.

​Agreed​. It is more complex, but once you understand the API, you can easily do everything you could do before, and there's some really nice new features and fixes. Prompt toolkit is awesome.

-- Carl Smith


_______________________________________________
IPython-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|

Re: New IPython 5 prompt

Carl Smith
I have this in my ipython_config file:

from pygments.token import Token

c.TerminalInteractiveShell.highlighting_style = 'lovelace'

c.TerminalInteractiveShell.highlighting_style_overrides = {
    Token.Prompt: '#00cccc',
    Token.PromptNum: '#00bbcc bold',
    Token.OutPrompt: '#cc00cc',
    Token.OutPromptNum: '#bb00cc bold',
    }

With a dark background, it looks really nice. It uses a teal/cyan colour for inputs and hotpink for outputs, so if you like cyan, you might like this. You can use it as a starting point at least.

Best,

-- Carl Smith

On 21 August 2016 at 16:27, Carl Smith <[hidden email]> wrote:
On 21 August 2016 at 16:05, Thomas Kluyver <[hidden email]> wrote:

We appreciate this is a bit more fiddly for simple custom prompts, but the advantages of using prompt_toolkit outweigh it.

​Agreed​. It is more complex, but once you understand the API, you can easily do everything you could do before, and there's some really nice new features and fixes. Prompt toolkit is awesome.

-- Carl Smith



_______________________________________________
IPython-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|

Re: New IPython 5 prompt

Carl Smith
Sorry for the noise. Just thought to share my prompt code too.

It changes the prompt based on whether you're entering a magic or command, implicitly or explicitly etc. My config runs %rehashx when IPython starts to turn all of the OS X commands into magics (except `from` as it conflicts with `%autocall` and `from x import y`). My prompts class has a method (`rehash`) for updating its list of line magics, and I have a magic named `%rehash` that wraps `%rehashx` and `CustomPrompts.rehash`, for when new commands are added to the system during an IPython session.

from IPython.core.magic import register_line_magic as line_magic
from IPython.terminal.prompts import Prompts, Token

class CustomPrompts(Prompts):

    def __init__(self, shell):

        self.cli = None
        self.shell = shell
        self.rehash()

    def rehash(self):

        self.commands = self.shell.magics_manager.magics["line"].keys()

    def input_prompt(self, cli):

        if not cli: return "▹ "
        text = cli.buffers["DEFAULT_BUFFER"].text
        if text.startswith("%") or text.startswith("!"): return ": "
        magic = "\n" not in text and text.split(" ")[0] in self.commands
        return "% " if magic else "▹ "

    def in_prompt_tokens(self, cli=None): return [
        (Token.Number, "("),
        (Token.Prompt, str(self.shell.execution_count)),
        (Token.Number, ")"),
        (Token.Prompt, self.input_prompt(cli))
        ]

    def out_prompt_tokens(self): return [
        (Token.Number, "("),
        (Token.OutPrompt, str(self.shell.execution_count)),
        (Token.Number, ")"),
        (Token.OutPrompt, "▹ ")
        ]

    def continuation_prompt_tokens(self, cli=None, width=None):
        if width is None: width = self._width()
        return [(Token.Prompt, " " * (width - 5) + "   ▹ ")]

    def rewrite_prompt_tokens(self): return [
        (Token.Number, ('▹' * (self._width() - 2))),
        (Token.Number, '▹ ')
        ]

@line_magic
def rehash(silent=False):

    % rehashx
    del ip.magics_manager.magics["line"]["from"]
    ip.prompts.rehash()
    if not silent: print("System magic updated.")

ip = get_ipython()
ip.prompts = CustomPrompts(ip)

% rehash quiet
del rehash

I know the code's a bit messy. It's just what I had, and should offer enough clues to figure out how to do most things.

Hope it helps,

-- Carl Smith

On 21 August 2016 at 16:36, Carl Smith <[hidden email]> wrote:
I have this in my ipython_config file:

from pygments.token import Token

c.TerminalInteractiveShell.highlighting_style = 'lovelace'

c.TerminalInteractiveShell.highlighting_style_overrides = {
    Token.Prompt: '#00cccc',
    Token.PromptNum: '#00bbcc bold',
    Token.OutPrompt: '#cc00cc',
    Token.OutPromptNum: '#bb00cc bold',
    }

With a dark background, it looks really nice. It uses a teal/cyan colour for inputs and hotpink for outputs, so if you like cyan, you might like this. You can use it as a starting point at least.

Best,

-- Carl Smith

On 21 August 2016 at 16:27, Carl Smith <[hidden email]> wrote:
On 21 August 2016 at 16:05, Thomas Kluyver <[hidden email]> wrote:

We appreciate this is a bit more fiddly for simple custom prompts, but the advantages of using prompt_toolkit outweigh it.

​Agreed​. It is more complex, but once you understand the API, you can easily do everything you could do before, and there's some really nice new features and fixes. Prompt toolkit is awesome.

-- Carl Smith




_______________________________________________
IPython-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|

Re: New IPython 5 prompt

Thomas Kluyver-2
Thanks Carl! FWIW, here's my startup file for custom prompts, which produces a more compact prompt than the default, and (IMO) a nicer look for multiline input:
https://gist.github.com/takluyver/85b33db0836cdcc4baf252fd81937fa7

On 21 August 2016 at 17:01, Carl Smith <[hidden email]> wrote:
Sorry for the noise. Just thought to share my prompt code too.

It changes the prompt based on whether you're entering a magic or command, implicitly or explicitly etc. My config runs %rehashx when IPython starts to turn all of the OS X commands into magics (except `from` as it conflicts with `%autocall` and `from x import y`). My prompts class has a method (`rehash`) for updating its list of line magics, and I have a magic named `%rehash` that wraps `%rehashx` and `CustomPrompts.rehash`, for when new commands are added to the system during an IPython session.

from IPython.core.magic import register_line_magic as line_magic
from IPython.terminal.prompts import Prompts, Token

class CustomPrompts(Prompts):

    def __init__(self, shell):

        self.cli = None
        self.shell = shell
        self.rehash()

    def rehash(self):

        self.commands = self.shell.magics_manager.magics["line"].keys()

    def input_prompt(self, cli):

        if not cli: return "▹ "
        text = cli.buffers["DEFAULT_BUFFER"].text
        if text.startswith("%") or text.startswith("!"): return ": "
        magic = "\n" not in text and text.split(" ")[0] in self.commands
        return "% " if magic else "▹ "

    def in_prompt_tokens(self, cli=None): return [
        (Token.Number, "("),
        (Token.Prompt, str(self.shell.execution_count)),
        (Token.Number, ")"),
        (Token.Prompt, self.input_prompt(cli))
        ]

    def out_prompt_tokens(self): return [
        (Token.Number, "("),
        (Token.OutPrompt, str(self.shell.execution_count)),
        (Token.Number, ")"),
        (Token.OutPrompt, "▹ ")
        ]

    def continuation_prompt_tokens(self, cli=None, width=None):
        if width is None: width = self._width()
        return [(Token.Prompt, " " * (width - 5) + "   ▹ ")]

    def rewrite_prompt_tokens(self): return [
        (Token.Number, ('▹' * (self._width() - 2))),
        (Token.Number, '▹ ')
        ]

@line_magic
def rehash(silent=False):

    % rehashx
    del ip.magics_manager.magics["line"]["from"]
    ip.prompts.rehash()
    if not silent: print("System magic updated.")

ip = get_ipython()
ip.prompts = CustomPrompts(ip)

% rehash quiet
del rehash

I know the code's a bit messy. It's just what I had, and should offer enough clues to figure out how to do most things.

Hope it helps,

-- Carl Smith

On 21 August 2016 at 16:36, Carl Smith <[hidden email]> wrote:
I have this in my ipython_config file:

from pygments.token import Token

c.TerminalInteractiveShell.highlighting_style = 'lovelace'

c.TerminalInteractiveShell.highlighting_style_overrides = {
    Token.Prompt: '#00cccc',
    Token.PromptNum: '#00bbcc bold',
    Token.OutPrompt: '#cc00cc',
    Token.OutPromptNum: '#bb00cc bold',
    }

With a dark background, it looks really nice. It uses a teal/cyan colour for inputs and hotpink for outputs, so if you like cyan, you might like this. You can use it as a starting point at least.

Best,

-- Carl Smith

On 21 August 2016 at 16:27, Carl Smith <[hidden email]> wrote:
On 21 August 2016 at 16:05, Thomas Kluyver <[hidden email]> wrote:

We appreciate this is a bit more fiddly for simple custom prompts, but the advantages of using prompt_toolkit outweigh it.

​Agreed​. It is more complex, but once you understand the API, you can easily do everything you could do before, and there's some really nice new features and fixes. Prompt toolkit is awesome.

-- Carl Smith




_______________________________________________
IPython-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-user



_______________________________________________
IPython-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|

Re: New IPython 5 prompt

Carl Smith

Cool. That's a much cleaner example, and a nice, simple prompt. Perhaps it's worth adding a couple of simple, self contained alternative propmts like that to IPython. People can pick one of maybe three or four, or use them as example code to define their own.


On Sun, 21 Aug 2016 17:12 Thomas Kluyver, <[hidden email]> wrote:
Thanks Carl! FWIW, here's my startup file for custom prompts, which produces a more compact prompt than the default, and (IMO) a nicer look for multiline input:
https://gist.github.com/takluyver/85b33db0836cdcc4baf252fd81937fa7

On 21 August 2016 at 17:01, Carl Smith <[hidden email]> wrote:
Sorry for the noise. Just thought to share my prompt code too.

It changes the prompt based on whether you're entering a magic or command, implicitly or explicitly etc. My config runs %rehashx when IPython starts to turn all of the OS X commands into magics (except `from` as it conflicts with `%autocall` and `from x import y`). My prompts class has a method (`rehash`) for updating its list of line magics, and I have a magic named `%rehash` that wraps `%rehashx` and `CustomPrompts.rehash`, for when new commands are added to the system during an IPython session.

from IPython.core.magic import register_line_magic as line_magic
from IPython.terminal.prompts import Prompts, Token

class CustomPrompts(Prompts):

    def __init__(self, shell):

        self.cli = None
        self.shell = shell
        self.rehash()

    def rehash(self):

        self.commands = self.shell.magics_manager.magics["line"].keys()

    def input_prompt(self, cli):

        if not cli: return "▹ "
        text = cli.buffers["DEFAULT_BUFFER"].text
        if text.startswith("%") or text.startswith("!"): return ": "
        magic = "\n" not in text and text.split(" ")[0] in self.commands
        return "% " if magic else "▹ "

    def in_prompt_tokens(self, cli=None): return [
        (Token.Number, "("),
        (Token.Prompt, str(self.shell.execution_count)),
        (Token.Number, ")"),
        (Token.Prompt, self.input_prompt(cli))
        ]

    def out_prompt_tokens(self): return [
        (Token.Number, "("),
        (Token.OutPrompt, str(self.shell.execution_count)),
        (Token.Number, ")"),
        (Token.OutPrompt, "▹ ")
        ]

    def continuation_prompt_tokens(self, cli=None, width=None):
        if width is None: width = self._width()
        return [(Token.Prompt, " " * (width - 5) + "   ▹ ")]

    def rewrite_prompt_tokens(self): return [
        (Token.Number, ('▹' * (self._width() - 2))),
        (Token.Number, '▹ ')
        ]

@line_magic
def rehash(silent=False):

    % rehashx
    del ip.magics_manager.magics["line"]["from"]
    ip.prompts.rehash()
    if not silent: print("System magic updated.")

ip = get_ipython()
ip.prompts = CustomPrompts(ip)

% rehash quiet
del rehash

I know the code's a bit messy. It's just what I had, and should offer enough clues to figure out how to do most things.

Hope it helps,

-- Carl Smith

On 21 August 2016 at 16:36, Carl Smith <[hidden email]> wrote:
I have this in my ipython_config file:

from pygments.token import Token

c.TerminalInteractiveShell.highlighting_style = 'lovelace'

c.TerminalInteractiveShell.highlighting_style_overrides = {
    Token.Prompt: '#00cccc',
    Token.PromptNum: '#00bbcc bold',
    Token.OutPrompt: '#cc00cc',
    Token.OutPromptNum: '#bb00cc bold',
    }

With a dark background, it looks really nice. It uses a teal/cyan colour for inputs and hotpink for outputs, so if you like cyan, you might like this. You can use it as a starting point at least.

Best,

-- Carl Smith

On 21 August 2016 at 16:27, Carl Smith <[hidden email]> wrote:
On 21 August 2016 at 16:05, Thomas Kluyver <[hidden email]> wrote:

We appreciate this is a bit more fiddly for simple custom prompts, but the advantages of using prompt_toolkit outweigh it.

​Agreed​. It is more complex, but once you understand the API, you can easily do everything you could do before, and there's some really nice new features and fixes. Prompt toolkit is awesome.

-- Carl Smith




_______________________________________________
IPython-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-user

_______________________________________________
IPython-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-user

_______________________________________________
IPython-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-user