[IPython-User] change prompt dynamically

Kuznetsov Valentin vkuznet@gmail....
Wed Sep 14 13:40:02 CDT 2011

In ipython version < 0.11 I was able to dynamically change prompt into ipython session as simple as

IP = IPython.ipapi.get()

def set_prompt(in1):
    """Define shell prompt"""
    ip = IP.user_ns['__IP']
    prompt = getattr(ip.outputcache, 'prompt1') 
    prompt.p_template = '\C_LightBlue[\C_LightCyan%s\C_LightBlue]|\#> ' % in1

The set_prompt function I kept in my ipy_profile configuration file. Then in my workflow I was able to change prompt accordingly upon certain actions, e.g. new connection to DB, host, etc. It was VERY handy. With new release of ipython, 0.11, I don't see obvious way to do that. The prompts are set via configuration, but there is no way to modify them dynamically during existing ipython session. 

Yes I can get instance of ip = get_ipython(), and yes I can access its data, e.g. ip.prompt_in1, but I CAN'T change them. Is it possible?

Here is scenario:

1. I load ipython
2. I connect to DB/hosts/etc. (and therefore gain info about connection)
3. I invoke set_prompt(new_prompt) to change existing prompt.

As you can imaging having set_prompt can be encapsulated into step #2 and prompt will change magically every time I do invoke my connection functions. Here is a pseudo-code:

def connect(host, port):
    # do connection
    set_prompt('%s:%s' % (host, port))

Then in my ipython session I have the following behavior:

In [1] connect('a.b.com', 80)

a.b.com:80 [2]

Can someone give me hints how this can be accomplished in new version of ipython.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1895 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/ipython-user/attachments/20110914/60a648bd/attachment.bin 

More information about the IPython-User mailing list