Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

jupyter

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!

like image 508
465b Avatar asked May 03 '18 13:05

465b


People also ask

How do you clear all variables in Jupyter notebook?

“jupyter notebook clear all variables” Code Answer'sPress Esc to enter command mode. Press d twice to delete all selected cells.

How do I clear all variables in Google Colab?

You can clear variables by using %reset-f function. If you want to delete any specific variable then use %reset-f.

How do I clear a cell output Jupyter notebook?

When you have Jupyter notebook opened, you can do this by selecting the Cell -> All Output -> Clear menu item.

How do you clear all variables?

To clear all variables from the current workspace, use clear or clearvars . To clear all global variables, use clear global or clearvars –global . To clear a particular class, use clear myClass . To clear a particular function or script, use clear functionName .


2 Answers

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)))
like image 64
AleksMat Avatar answered Sep 30 '22 19:09

AleksMat


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]
like image 45
465b Avatar answered Sep 30 '22 20:09

465b