Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving Variable state in Colaboratory

Tags:

When I am running a Python Script in Colaboratory, it's running all previous code cell.

Is there any way by which previous cell state/output can be saved and I can directly run next cell after returning to the notebook.

like image 735
saurabh rathor Avatar asked Nov 23 '17 05:11

saurabh rathor


1 Answers

The outputs of Colab cells shown in your browser are stored in notebook JSON saved to Drive. Those will persist.

If you want to save your Python variable state, you'll need to use something like pickle to save to a file and then save that file somewhere outside of the VM.

Of course, that's a bit a trouble. One way to make things easier is to use a FUSE filesystem to mount some persistant storage where you can easily save regular files but have them persist beyond the lifetime of the VM.

An example of using a Drive FUSE wrapper to do this is in this example notebook: https://colab.research.google.com/notebook#fileId=1mhRDqCiFBL_Zy_LAcc9bM0Hqzd8BFQS3

This notebook shows the following:

  1. Installing a Google Drive FUSE wrapper.
  2. Authenticating and mounting a Google Drive backed filesystem.
  3. Saving local Python variables using pickle as a file on Drive.
  4. Loading the saved variables.
like image 144
Bob Smith Avatar answered Sep 20 '22 12:09

Bob Smith