Reputation: 257
I am using Jupyter Notebook to write some tutorials. However, I met a problem here. As shown in following picture, when I made changes to my colleague's notebook program, the index is not right. how to change those in 1, in [2] to in [34], in [35]?
Upvotes: 8
Views: 15360
Reputation: 89
You can easily solve this problem as follows:
Jupyter Notebook Menubar > Kernel
> Restart & Run All
Upvotes: 0
Reputation: 2425
This simple Python snippet would do it
import json
with open(NOTEBOOK_FILE, 'rt') as f_in:
doc = json.load(f_in)
cnt = 1
for cell in doc['cells']:
if 'execution_count' not in cell:
continue
cell['execution_count'] = cnt
for o in cell.get('outputs', []):
if 'execution_count' in o:
o['execution_count'] = cnt
cnt = cnt + 1
with open(NOTEBOOK_FILE, 'wt') as f_out:
json.dump(doc, f_out, indent=1)
(Make sure the notebook isn't running in Jupyter)
Upvotes: 6
Reputation: 85512
Click the menu Cell
--> Run all
. This will execute all cells and you will have ordered cell index numbers. If it does not start from cell index 1
, first click Kernel
--> Restart
and confirm the re-start.
Upvotes: 9