I want to execute code in a jupyter notebook that appends a cell to the notebook that is executing the code and writes code in it.
e.g. in ‘test.ipynb’ execute code like this
name='test.ipynb'
code_str='{some_code}'
nb = nbf.read(f'{name}', as_version=4)
cells=[nbf.v4.new_code_cell(code_str)]
nb['cells'].extend(cells)
nbf.write(nb, f'{name}')
Ideally, a new cell should be created in ‘test.ipynb’ containing the new code. However, seemingly due to the notebook changing state while executing the code, it doesn’t work.
VS Code issues an error (not in the notebook itself, at the VS Code application level) of
“Failed to save ‘test.ipynb’: The content of the file is newer. Do you want to overwrite the file with your changes?”
Is there a way to circumvent this behaviour?
Thanks