# Are persistent variable values after the kernel is stopped possible?

Is there a way to make values of variables persistent even after the kernel is stopped?

Example, if I were to set up a list for which I append new list items (such as a check ledger, as a basic example). If I stop the kernel, the value of the variable is lost. I can import/export to .csv, .txt, or .xlsx but that kind of defeats the point.

Thanks!

-
Just a quick suggestion: this sounds like a perfect application for a database, see the documentation – Jens Jun 17 '12 at 21:31
@Jens I use databases at work and they work pretty great. But I'm not sure there are good solutions for Mac (what I use at home) in this regard. And that would be almost the same as the import/export of tabular data. – kale Jun 17 '12 at 21:49

There's a related question with interesting answers, where I suggested storing in the notebook's tagging rules. Just use something like this as a variable CurrentValue[EvaluationNotebook[], {TaggingRules, "myvars", "x"}] and it will be stored in the notebook, so it survives kernel crashes. If it's something big however, I would probably use DumpSave before quitting the kernel, and load it in an initialization cell. It's not nice to have 800MB notebooks around

-
I like the tagging method with or without the use of Compress[], but I think with the large amount of data, DumpSave/Get-ing the variable is probably the best solution for me. Thanks! – kale Jun 17 '12 at 22:00

A very fine solution is given by this answer by WReach on Stackoverflow/Mathematica. He provides an approach with simple file-backed storage during operations. An alternative using SQL is given as well.

-

Some fancy ways have already been posted, but I'd like to mention the simple ones.

### Per Notebook

Use an Initialization Cell (Cell > Cell Properties > Initialization Cell) for code that should be evaluated any time you first evaluate code in the Notebook in each session.

### Globally

Use the Kernel\init.m file (FileNameJoin[{\$UserBaseDirectory, "Kernel", "init.m"}]) for code that is run every time the kernel is started.

-

You could wrap (the relevant part of) your code in DynamicModule. The documentation says "Symbols specified in a DynamicModule will by default have their values maintained even across Mathematica sessions."

-
Welcome :). The difficulty with this solution is that you can only wrap a single cell in DynamicModule so unless you use it in a creative dynamic way, there's not much you can do with this, I think. – Rojo Jun 18 '12 at 0:21
You could use this sort of construct: <code>DynamicModule[{a=0},Row[{Button["+",a+=1],Button["-",a-=1],Dynamic[a]}]]</‌​code> – Stephen Luttrell Jun 18 '12 at 12:01
It would be nice to add to your answer a use case of your idea to save and load with a couple of buttons some saved variable. For example, SetAttributes[dataSaver, HoldFirst]; dataSaver[sym_] := DynamicModule[{bck = 0}, Row[{Button["Save " <> ToString@Unevaluated@sym <> " ownvalues", bck = sym], Button["Load " <> ToString@Unevaluated@sym <> " ownvalues", sym = bck]}], Initialization :> (sym = bck)] – Rojo Jun 18 '12 at 13:48
I agree with your suggestion about giving more comprehensive answers. As you can see in my comment above, I fleshed things out a bit, but it didn't display nicely (I thought I had deleted the comment!). Any ideas why the code.../code section didn't work as I had intended? – Stephen Luttrell Jun 18 '12 at 19:43
I use backticks to write code. If you want to know more, when you're writing a comment there's a help link that leads you to some info, and then a Learn more link that leads you to loads more – Rojo Jun 18 '12 at 20:28