0

As we know, if we stop the kernel, when we re-open the file, all outputs are lost.

Is there any way we can save them and reload (resume) it later especially on another device?

Jaya A
  • 145
  • 1
  • 8

1 Answers1

0

If you have in-memory objects you are trying to store you can look into Pickle, it's part of the standard library.

import pickle

# Create a large object
oldObj = [n for n in range(10**3)]
print(len(oldObj), type(oldObj))
# <class 'list'> 1000

# Save to file
with open('myFile.pkl', 'wb') as file:
    pickle.dump(oldObj, file)

# --- you can send the file to someone else, or stop the kernel

# Load from file
with open('myFile.pkl', 'rb') as file:
    newObj = pickle.load(file)

print(len(newObj), type(newObj))
# <class 'list'> 1000

This works with nearly any native Python type, with a few exceptions. There is another library called dill that extends this functionality even further (it uses the same syntax)

$ pip install dill

EDIT: Upon re-reading your post, I am unsure what you mean by "output". If you simply mean the literal printed output of cells, that should persist as long as you are saving the notebook after execution. You also can save the notebook as a static document in order to preserve its visual state in a more robust manner. Some of the common export types are HTML, PDF, and Markdown.

$ jupyter nbconvert --help
vulpxn
  • 731
  • 1
  • 6
  • 14
  • Sorry, I'm not sure the right term for that. I mean the values stored in a variables. Let me know the correct term, so I can edit my question. – Jaya A Apr 13 '20 at 07:50
  • By the way, I have used that dill library. Unfortunately, I cannot reload on another device. – Jaya A Apr 13 '20 at 07:54
  • Can you elaborate on what you mean with "I cannot reload on another device"? In your question you mention that you want to "reload... on another device". – vulpxn Apr 13 '20 at 18:04
  • I have used the 'dill' and saved the file to the current device. After that, I transferred the file to another device. And, finally, I reload to this new device. – Jaya A Apr 14 '20 at 04:47
  • You'll have to explain the specific problem you're trying to solve here, else there's not else anyone can help with. Are you just trying to save your global state? If so just use either pickle or dill to dump `globals()` – vulpxn Apr 14 '20 at 04:50
  • Sorry. I'm still new. I cannot explain correctly. What is `globals()'? With the same file I save using 'dill', it cannot be opened with another computer. – Jaya A Apr 14 '20 at 06:47
  • `globals` and `locals` are top-level namespaces for a Python process. You can read more about them [here](https://stackoverflow.com/questions/7969949/whats-the-difference-between-globals-locals-and-vars), but just think of them as a big dictionary that holds everything in your workspace. – vulpxn Apr 14 '20 at 18:01
  • Referring to my question, how do I solve the issue? I used 'dill' and reload in another computer, it cannot be reloaded. – Jaya A Apr 15 '20 at 06:38