Robin's Blog

How to: rescue lost code from a Jupyter/IPython notebook

Jupyter (formerly known as IPython) notebooks are great – but have you ever accidentally deleted a cell that contained a really important function that you want to keep? Well, this post might help you get it back.

So, imagine you have a notebook with the following code:

Screen Shot 2016-04-15 at 12.52.40

and then you accidentally delete the top cell, with the definition of your function…oops! Furthermore, you can’t find it in any of your ‘Checkpoints’ (look under the File menu). Luckily, your function is still defined…so you can still run it:

Screen Shot 2016-04-15 at 12.53.01

This is essential for what follows…because as the function is still defined, the Python interpreter still knows internally what the code is, and it gives us a way to get this out!

So, if you’re stuck and just want the way to fix it, then here it is:

def rescue_code(function):
    import inspect
    get_ipython().set_next_input("".join(inspect.getsourcelines(function)[0]))

Just call this as rescue_code(f), or whatever your function is, and a new cell should be created with the code of you function: problem solved! If you want to learn how it works then read on…

Screen Shot 2016-04-15 at 12.54.19

The code is actually very simple, inspect.getsourcelines(function) returns a tuple containing a list of lines of code for the function and the line of the source file that the code starts on (as we’re operating in a notebook this is always 1). We extract the 0th element of this tuple, then join the lines of code into one big string (the lines already have \n at the end of them, so we don’t have to deal with that. The only other bit is a bit of IPython magic to create a new cell below the current cell and set its contents….and that’s it!

I hope this is helpful to someone – I’m definitely going to keep this function in my toolkit.


If you found this post useful, please consider buying me a coffee.
This post originally appeared on Robin's Blog.


Categorised as: How To, Programming, Python


30 Comments

  1. Jaime Lopez says:

    Hi Robin,

    Great tool, I will keep in my tool’s box. It is really important.
    I am wondering why Jupyter creators (Perez and company) do not create an UNDO function inside EDIT menu?
    Thanks, Jaime

  2. Robin Wilson says:

    There is an undo function in the notebook, but it doesn’t cover everything. When you’ve deleted a cell there is an ‘undo delete cell’ item in the Edit menu, but you can also undo changes to the text in cells by pressing Ctrl-Z (or Cmd-Z). I think (though I am not 100% sure) that there is a per-cell undo buffer: so you can undo changes in separate cells independently. That does, of course, mean that you have to click on the cell that you want to undo things in before you can actually undo anything.

    Hope that helps!

  3. Jaime Lopez says:

    Thanks Robin,

    Yeah, that help me.

    Have a good day, Jaime

  4. Alex says:

    god bless you

  5. Dzung Nguyen says:

    You’ve just saved my life 😀

  6. Hallu Liao says:

    Thanks, that’s really helpful.

  7. mxhf says:

    You’ve just saved my life as well!!!

  8. Luke says:

    Thank you- just saved me 2 days work!

  9. Robert Schnabel says:

    Thank you. Again! Thank you for saving me from myself..

  10. Harry Morris says:

    Fantastic, this helped me a lot!

  11. Selim says:

    Saved me a lot of time ! thanks 🙂

  12. sidharth mallick says:

    Thanks dude for all the help

    %hist -gu

    %hist -gu *search parameter*

    %history

    These are helpful for getting all the data also

  13. Kristian says:

    This looks really good – however I need to rescue a deleted function in a Julia notebook.
    Can you point me towards how to translate that into Julia code?

  14. Robin Wilson says:

    Unfortunately this uses Python’s introspection capabilities, so won’t work with Julia. I’m not familiar enough with Julia to know if there’s a way to do it in Julia – but you could try asking on a Julia forum somewhere whether there is a way to get the source code of a function once it has been written and executed, as that’s basically all I’m doing here.

  15. thank you says:

    Amazing!
    saved me couple hours of rewriting my lost code

  16. Daniel Soutar says:

    You are a LIFESAVER. THANK YOU

  17. Sam says:

    Thanks so much!

  18. Sal R says:

    THANK YOUUUUUU

  19. Abdullahi Mohammad says:

    Hi,
    That’s a great post. Suppose you have accidentally deleted some parts of the codes in the same cell and forgot to undo before closing the notebook, is there a way to recover the codes later when the notebook is reopened? Thank you.

  20. Anna K says:

    So useful, thank you!!!

  21. Anton Hassan says:

    Thank you .. thank you .. You really saved me!

  22. La Cancion says:

    Hi Robin,
    Great tool, I will keep in my tool’s box. It is really important.
    I am wondering why Jupyter creators (Perez and company) do not create an UNDO function inside EDIT menu?
    Thanks, Jaime

  23. Enguichou says:

    THANK YOU! Simple, efficient and several hours of work have been saved in no time thanks to you! 🙂

  24. Edoardo says:

    Really, you saved my life…

  25. Inkling says:

    HUGE LIFE SAVER!!!!!!

  26. Saghi_Kh says:

    Thank you for your great advice. You are a lifesaver 🙂

  27. Joan Villa says:

    thankssssssssssss!!!!!!

  28. Kyle Zhu says:

    You saved my life. Thank you.

  29. Montel Moore says:

    Just like the people prior to me, my life has been saved by this web page

  30. Thomas says:

    So amazing, this just saved me!

Leave a Reply

Your email address will not be published. Required fields are marked *