Help – I deleted my notebook/notebook cells

An undelete trick if you accidently delete ot lose a set of notebook files, that I need to try but remark on here, because it might help some students who accidentally delete things and only realise too late…

Via a tweet, the IPython %history magic, which will display the IPython history (essentially, your run code cell history) from the current session by default can also search back into previous sessions.

From the docs, you can alose go back in time to previous sessions:

  • line 4, current session
  • lines 4-6, current session: 4-6
  • lines 1-5, session 243: 243/1-5
  • Line 7, session 2 before current: ~2/7
  • From the first line of 8 sessions ago, to the fifth line of 6 sessions ago: ~8/1-~6/5

The -g flag will search through all session histories. This is perhaps best used in association with the -f flag which save to a file. The -u flag will only save unique entries.

So to capture something you lost from the mists of time, it will quite likely be found in %history -g -f phew.txt.

PS Thoughts:

  • how big is a my history cache? And how is it stored? In a sqlite db would be handy…
  • where is the history cache? If you’re working in a dockerised environment and using a fresh container each work session , then to go deep and search from previous work sessions would mean a mount from persistent storage needs to cover wherever the IPython history is kept.
  • could we fashion a simple emergency toolbely tool to recreate, in part at least, a notebook from an IPython history for a given session?! Maybe just opening a saved session history file via jupytext would be a start? The history includes comments (it captures the complete contents of each run cell), which could then be extracted as md cells, for example. But then, markdown cells presumably aren’t captured in history so would be lost to future history grabs. Which perhaps puts cell comments (in a notebook context) into a different light: code cell comments are recoverable from IPython history but md comments aren’t…
  • Security implications…. like: when you explicitly assign your password to a variable in a code cell, and then immediately delete the cell once it’s run “to be safe”… Oops…

Author: Tony Hirst

I'm a Senior Lecturer at The Open University, with an interest in #opendata policy and practice, as well as general web tinkering...

%d bloggers like this: