Reputation: 1277
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:
Cell 1:
variable overhead #this will be used in the entire notebook
Cell 2:
import or generate data & manipulate data
clear all variables introduced in cell without the need of addressing every single one of them by hand <-- this is what i am looking for.
Thank you very much!
Upvotes: 7
Views: 8870
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
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