Sharing variables between different jupyter laptops

I have two different Jupyter laptops running on the same server. I would like to make access to some (only some of them) from the variables of one laptop through another laptop (I need to compare if two different versions of the algorithm give the same results, basically). Is there any way to do this?

thanks

+15
jupyter-notebook jupyter
source share
3 answers

If you only need something fast, you can use the pickle module to make the data permanent (save it to a file), and then pick it up with another laptop. For example:

import pickle a = ['test value','test value 2','test value 3'] # Choose a file name file_name = "sharedfile" # Open the file for writing with open(file_name,'wb') as my_file_obj: pickle.dump(a,my_file_obj) # The file you have just saved can be opened in a different session # (or iPython notebook) and the contents will be preserved. # Now select the (same) file to open (eg in another notebook) file_name = "sharedfile" # Open the file for reading file_object = open(file_Name,'r') # load the object from the file into var b b = pickle.load(file_object) print(b) >>> ['test value','test value 2','test value 3'] 
+7
source share

Between 2 jupyter laptops you can use the% store command.

In the first Jupyter notebook:

 data = 'string or data-table to pass' %store data del data 

In the second Jupyter notebook:

 %store -r data data 

You can find more information here .

+22
source share

You can use the same magic commands . Cell magic : %%cache in an IPython laptop can be used to cache the results and results of lengthy calculations in a persistent pickle file. Useful when some calculations on a laptop are long and you want to easily save the results in a file.

To use it on your laptop, you first need to install the ipycache module, since this magic Cell command is not a built-in magic command.

then load the module into your notepad:

 %load_ext ipycache 

Then create a cell with:

 %%cache mycache.pkl var1 var2 var1 = 1 # you can put any code you want at there, var2 = 2 # just make sure this cell is not empty. 

When you run this cell for the first time, the code is executed, and the var1 and var2 variables are stored in mycache.pkl in the current directory along with the outputs. Rich output is only displayed if you are using the IPython version for development. When you run this cell again, the code is skipped, the variables are loaded from the file and entered into the namespace, and the outputs are restored to the laptop.

Alternatively, use $file_name instead of mycache.pkl , where file_name is a variable containing the path to the file used for caching.

Use the --force or -f option to force cell execution and overwrite the file.

Use the --read or -r option to prevent the cell from executing and always load variables from the cache. An exception occurs if the file does not exist.

ref: github ipycache storage and laptop example

+2
source share

All Articles