Persisting data in Google Colaboratory

后端 未结 7 688
眼角桃花
眼角桃花 2020-12-28 14:11

Has anyone figured out a way to keep files persisted across sessions in Google\'s newly open sourced Colaboratory?

Using the sample notebooks, I\'m successfully authe

7条回答
  •  悲哀的现实
    2020-12-28 14:42

    If anyone's interested in saving and restoring the whole session, here's a snippet I'm using that you might find useful:

    import os
    import dill
    from google.colab import drive
    
    backup_dir = 'drive/My Drive/colab_sessions'
    backup_file = 'notebook_env.db'
    backup_path = backup_dir + '/' + backup_file
    
    def init_drive():
      # create directory if not exist
      drive.mount('drive')
      if not os.path.exists(backup_dir):
        !mkdir backup_dir
    
    def restart_kernel():
      os._exit(00)
    
    def save_session():
      init_drive()
      dill.dump_session(backup_path)
    
    def load_session():
      init_drive()
      dill.load_session(backup_path)
    

    Edit: Works fine until your session size is not too big. You need to check if it works for you..

提交回复
热议问题