How do I reset the Jupyter/IPython input prompt nu

2019-03-14 06:12发布

问题:

I just wrote my first extensive Python tutorial using IPython notebooks. All went well, except I did a lot of testing and moving blocks around. How do I reset the In [ ]: numbering? I have tried quitting and reloading, but that doesn't seem to work.

回答1:

I think, the only way to to what you want is: - 'Kernel > Restart' (restart the kernel) and then 'Cell > Run All' (run the script).



回答2:

You can reset the kernel (shortcut: C-m .) and re-run the whole notebook.

Quitting and reloading doesn't work because the code is not re-evaluated.



回答3:

'Kernel' -> 'Restart & Run All'

Just make sure you saved your Notebook. You can also bind/assign keyboard key for running this command.

'Help' -> 'Edit Keyboard Shortcuts'


回答4:

Every .ipynb file can be opened in an editor. Everything written there is in plain text (JSON). For each cell which has the "cell_type": "code" there'd be another key-value pair as "execution_count": <number>. As you might have guessed, that is the prompt numbering. Hence, if the notebook contains code which will take time to execute (as was, in my case) this method would be time efficient.

Now, either you can manually change each execution_count or write a simple script to get the numbering right. To check the results just refresh the notebook in the browser without stopping the kernel. And, everything will be as per your needs, even all the variables/loaded data will remain in the environment.



回答5:

If what you want is to remove the numbers themselves, so that each cell shows In [ ] (instead of something like In [247] which is leftover from some previous incarnation of the kernel), use "Cell" > "All Output" > "Clear" (in Jupyter Notebook 5.4.0) or "Edit" > "Clear All Outputs" (In Jupyter Lab 0.32.1).

This will remove all the numbers, even if you're in the middle of running a notebook. It will not reset the numbering back to 1; e.g. if the last cell you executed was 18, the next will be 19.

If you're using this because you want clarity about which cells you've executed during this run of the kernel and which cells you haven't executed yet, use "Cell" > "All Output" > "Clear" (or "Edit" > "Clear All Outputs") immediately after you start (or restart) the kernel. This can be useful when restarting a kernel, or when opening a saved or duplicated notebook.

This will also remove all outputs from the notebook.

Thanks to user2651084 in a previous comment for this.