465b
465b

Reputation: 1277

Clear all variables which where defined in Jupiter cell after execution finished

I need to import and manipulate memory heavy data in jupyter. Because I tend to have rather long notebooks were several data sets will be importer I need to clear them continuously by hand. This is tideous.

If possible, i would like to have a tool which clears all variables introduced in a cell and only those without the need of addressing them by hand after they fullfilled there purpose.

I could of course overwrite variables, however as they all serve rather different purposes this will drastically reduce the readabiliy of the code.

To summarize:

Thank you very much!

Upvotes: 7

Views: 8870

Answers (2)

465b
465b

Reputation: 1277

It's not a clean solution but the cells which data I need to keep are not computationally expensive. Therefore I found it most convenient to simply do:

%reset -f
exec In[n:m]

Upvotes: 1

AleksMat
AleksMat

Reputation: 914

You can reset variables in the Jupyter Notebook by putting the following magic command in the code:

%reset_selective -f [var1, var2, var3]

If you add such lines in your code it should remain readable.

To answer your question completely - At the moment I don't think there exists a command that would automatically find all variables created in a specific cell and reset only them. (Someone please correct me if I am wrong.)

But you can use the following code that deletes exactly those namespace objects which were newly created in a cell. It is probably what you wanted:

from IPython import get_ipython

my_variables = set(dir())  # Write this line at the beginning of cell

# Here is the content of the cell

my_variables = list(set(dir()) - my_variables)  # Write these 2 lines at the end of cell
get_ipython().magic('%reset_selective -f [{}]'.format(','.join(my_variables)))

Upvotes: 4

Related Questions