# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.9.7 ('base')
#     language: python
#     name: python3
# ---

# # storemagic - Don't lose your variables in Jupyter Notebook
# > A post on IPython extension 'storemagic' to keep your important variables in persistent storage. Use this trick to keep them safe and stop pulling your hair (if there are any left) 
#
# - toc: true 
# - badges: true
# - comments: true
# - categories: [jupyter, python]
# - keyword: [jupyter, notebook, python, tip, IPython, store, storemagic]
# - image: images/copied_from_nb/images/2022-05-30-storemagic-jupyter-notebook.jpeg

# ![](images/2022-05-30-storemagic-jupyter-notebook.jpeg)

# # Enviornment

# !jupyter --version

# # About
#
# This post is about a very important (and highly underrated) Jupyter notebook extension "storemagic". We will see how we can use it to store our important data, and recover safely even when the unforgivable happens.
#
# # Introduction
#
# All Jupyter Notebook lovers remember their petrified moments when they are struck by a [dark terminal] force, their heart skips a beat, and they stare blanky at the screen. When they know that the unforgivable has happened but they still timidly move the cursor to the last cell and press Enter with the faintest hope that it might be still alive. But fate has chosen something else for us, and we poor people are left cursing ourselves as to why we did not save our precious work!
#
# I have been to those despair valleys (many times) till I stumbled upon 'storemagic'. It seems like archaic magic as so few people know or use it, but I can guarantee once you get used to it you will barely write more than five lines in your notebooks without using it. So let's see how it works.
#
# ## Store a variable
# Use `%store` magic for lightweight persistence. It stores variables, aliases and macros in IPython’s database. Let's create a variable and then store it using this magic.

# +
##
# create a variable
var_hello = "hello world!"

# store this variable
# %store var_hello
# -

# Now I am going to intentionally restart the kernel. We can check that our created variable is now gone from the memory.

var_hello

# But no worries. We have it stored safely using our magic. So let's get it back.

## 
# get the variable back from store
# %store -r var_hello

#  Okay, we have our variable back and (with a sigh of relief) we can use it again.

var_hello

# Let's create a few more variables and do some more magic with them.

# +
##
# create variables
var_foo = [1,2,3,4]
var_bar = {'a':var_hello}

# store multiple variables
# %store var_foo var_bar
# -

# ## Check all stored varaibles
#
# We can check all the varaibles stored using the following magic command.

# %store

# ## Remove a variable from store
#
# To remove a variable from our storage is also straight forward. Put its name after `%store -d` flag

##
# remove 'var_hello'
# %store -d var_hello

# 'var_hello' is now gone. Forever ...

##
# check the remaining variables in store
# %store

# ## Remove all variables from store
#
# If you want to remove all the varaibles from store and start clean then use `-z` flag

##
# remove all variables
# %store -z

# Kaboom! all variables are gone.

##
# check store
# %store

# ## Reference
#
# Check the official IPython `storemagic` documentation here: https://ipython.readthedocs.io/en/stable/config/extensions/storemagic.html

StarcoderdataJupyterScriptsDedupFiltered