Do you ever wish you could save data directly into a notebook instead of having to export it? I sometimes do things like
mydata = {1,2,3, ...};
just to be able to use this data next time I open the notebook. This could be data I pasted from the web, or something that just took several minutes to generate.
But saving it like this is inconvenient, and often takes up a lot of space.
Today I published a blog post about a better method that I have been using recently. I thought people here may be interested:
The idea is to use first Compress the data, then use Interpretation to create a compact display for it. Here's a small function that packages all of this up:
ClearAll[SaveToCell]
SaveToCell::usage =
"SaveToCell[variable] creates an input cell that reassigns the current value of variable.\n" <>
"SaveToCell[variables, display] shows 'display' on the right-hand-side of the assignment.";
SetAttributes[SaveToCell, HoldFirst]
SaveToCell[var_, name : Except[_?OptionQ] : "data", opt : OptionsPattern[]] :=
With[{data = Compress[var],
panel = ToBoxes@Tooltip[Panel[name, FrameMargins -> Small], DateString[]]},
CellPrint@Cell[
BoxData@RowBox[{
MakeBoxes[var],
"=",
InterpretationBox[panel, Uncompress[data]],
";"
}],
"Input",
(* prevent deletion by Cell > Delete All Output: *)
GeneratedCell -> False,
(* CellLabel is special: last occrrence takes precedence, so it comes before opt: *)
CellLabel -> "(saved)",
opt,
CellLabelAutoDelete -> False
]
]
If you have your data in the variable var, simply run SaveToCell[var], which will create an input cell that re-assign the value of var. It looks like this:

We can also customize the display:
var = Range[1000];
SaveToCell[var, Short[var]]

Hovering the display will show the date when the data was saved.
SaveToCell also takes arbitrary Cell options, and passes them down to the generated cell. Something strange I observed while writing this function is that with some options, such as CellLabel, it is not the first but the last occurrence of the option that takes precedence. Does anyone know why?
We can use this functionality to change the cell style, add a different label, or to protect the cell against accidental deletion: SaveToCell[var, Deletable -> False].
I have been using this little function for a while, and I hope that others will find it useful too.
Do be careful though: notebooks are not designed for storing large data. I would avoid storing data as large as several tens of megabytes within notebooks.