VSCode Jupyter Notebook - Restore Cache Version

为君一笑 提交于 2021-01-29 17:50:33

问题


I was creating a Jupyter Notebook using VSCode (v 1.48.0) running on Ubuntu 19.10. VSCode crashed and unfortunately I had not saved the notebook, and when I restarted it was empty.

I have been able to find what looks like a cached version of the notebook in ~/.config/Code/User/globalStorage/ms-python.python , in a file called 527ed533.ipynb. Small sample of what the file looks like

Reviewing the contents of the file, I can tell that this is the notebook I was working on, but the format looks a bit garbled,

When I try and open the file back up in VSCode it just recognises this as text. I have tried to open it on Jupyter server too and it also does not recognise this as a valid notebook.

I tried to do a bit of manual editing of the data, mainly to remove everything other than the "cells" but this was not successful.

Is there any way that I can get the notebook back from this file?


回答1:


Manual way:

  • Copy contents from your temp file.
  • Run this script by placing the content here (in Chrome inspector's console).
var fileContent = << paste it just like that >>;   // It's an object
console.log(unescape(fileContent.contents));
  • Copy the contents that are logged in console.
  • Create a new file with .ipynb, open and paste it here.

Output:

I did notice such files but in a different location, I use Mac.

~/Library/Application Support/Code/User/globalStorage/ms-python.python/4ae407c9.ipynb



回答2:


I also bumped into this problem. I recovered the source code by using this script

import json
with open('b1d10574.ipynb', 'r') as f1, open('example.py', 'w') as f2:
    data = json.loads(json.load(f1)['contents'])
    for cell in data['cells']:
         f2.write("# %%\n")
         for line in cell['source']:
             f2.write(line) 
         f2.write("\n")



来源:https://stackoverflow.com/questions/63511764/vscode-jupyter-notebook-restore-cache-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!