Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reset the IPython input prompt numbering? (console, not notebook or qt)

Tags:

ipython

The following is my current IPython command prompt (regular terminal IPython, not the notebook, or Qt):

In [45]: 

I'm done with my workflow, and am moving on to a new task. I'd like a way to reset the IPython input prompt number as follows:

In [1]:

I understand this may be possible from non-duplicate question to which I cannot reply nor answer for my case: How do I reset the IPython input prompt numbering?

For further clarity, an example of the command I'm after:

In [99]: %reset_command_number
In [1]: 2+2
Out[1]: 4
In [2]: %reset_command_number
In [1]:

The 3 Magic Questions:

  1. Does such magic (%reset_command_number) exist?
  2. If the answer to 1 is yes, what dark magic is it?
  3. If the answer to 1 is no, is this achievable from a lower level IPython api? (yes i've searched the api docs and no i did not find a way to do it, yet...)
like image 639
JoelBondurant Avatar asked Oct 01 '22 03:10

JoelBondurant


1 Answers

One possibility would be to modify the execution_count of the InteractiveShell:

import IPython; IPython.get_ipython().execution_count = 0
  • Using a one line command with ; makes it easier to call this repeatedly (if you use up-arrow to scroll the command history)
  • This has the side effect that after first command you will also see this error. I do not know if it has any side effects, though. (it already says that the history logging was moved to another session)
ERROR! Session/line number was not unique in database. History logging moved to new session 20741

Demo

In [1]: a = 1

In [2]: b = 88

In [3]: c = 99

In [4]: import IPython; IPython.get_ipython().execution_count = 0

In [1]: a
Out[1]: 1

ERROR! Session/line number was not unique in database. History logging moved to new session 20767
In [2]: b
Out[2]: 88

In [3]: c
Out[3]: 99

In [4]: import IPython; IPython.get_ipython().execution_count = 0

In [1]: a = 15

ERROR! Session/line number was not unique in database. History logging moved to new session 20768
In [2]: a
Out[2]: 15

In [3]: b
Out[3]: 88

Using an alias?

It is also possible to use an alias for the command. For example, create file i_reset.py and put it into your site-packages, ~/.ipython/extensions/, your PYTHONPATH or any other folder you can see in your sys.path

# i_reset.py

def i_reset(line):
    """
    Reset IPython shell numbering to [1]    
    """
    import IPython; IPython.get_ipython().execution_count = 0

def load_ipython_extension(ipython):
    """This function is called when the extension is
    loaded. It accepts an IPython InteractiveShell
    instance. We can register the magic with the
    `register_magic_function` method of the shell
    instance."""
    ipython.register_magic_function(i_reset, 'line')

Then, you may use the extension by first loading it and then calling it like so:

In [4]: %load_ext i_reset

In [5]: i_reset

In [1]: a
Out[1]: 1
like image 196
np8 Avatar answered Oct 11 '22 14:10

np8