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:
One possibility would be to modify the execution_count
of the InteractiveShell:
import IPython; IPython.get_ipython().execution_count = 0
;
makes it easier to call this repeatedly (if you use up-arrow to scroll the command history)ERROR! Session/line number was not unique in database. History logging moved to new session 20741
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With