[IPython-User] configuring prompts for custom substitutions

Skipper Seabold jsseabold@gmail....
Thu Aug 23 17:15:45 CDT 2012


On Thu, Aug 23, 2012 at 5:58 PM, Thomas Kluyver <takowl@gmail.com> wrote:
> Hi Skipper,
>
> On 23 August 2012 19:13, Skipper Seabold <jsseabold@gmail.com> wrote:
>> I'm having some trouble working through the code. Is there anyway to
inject
>> user-defined (callable) substitutions into the prompts. E.g., something
like
>
> I don't have time to test at the moment, but I think you need to add
> it using the IPython.core.prompts.LazyEvaluate wrapper, something like
> this:
>
> @LazyEvaluate
> def am_at_home():
>     import os
>     if os.path.expanduser('~') == os.path.abspath(os.path.curdir):
>         return "(home)"
>     return ""
>
> get_ipython().prompt_manager.lazy_evaluate_fields['amathome'] = am_at_home
> get_ipython().prompt_manager.in_template = '[\#]: {amathome}'
>
> Note the new-style formatting syntax, rather than \amathome.
>
> This example is written for a startup script -
> PromptManager.lazy_evaluate_fields is not currently configurable (and
> adding values to lists or dicts in configuration is awkward anyway.
>
> Hope that helps,
> Thomas

Thanks Thomas,

Cool. Works like a charm, my ipython config now to give my git branch in
the ipython terminal

c.TerminalIPythonApp.exec_lines = [
        """
from IPython.core.prompts import LazyEvaluate

@LazyEvaluate
def parse_git_branch():
    import subprocess
    cmd = 'ref=$(git symbolic-ref HEAD 2> /dev/null); echo
\"(\"${ref#refs/heads/}\")\"'
    branch = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE).communicate()[0]
    if not branch.strip().strip("()"):
        return ""
    else:
        return branch""",

"""get_ipython().prompt_manager.lazy_evaluate_fields["parse_git_branch"] =
parse_git_branch""",
        r"get_ipython().prompt_manager.in_template = '[\Y0/]
{parse_git_branch}\n[\#]: '"
        ]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-user/attachments/20120823/bac1aa15/attachment.html 


More information about the IPython-User mailing list