One possibility is to have a function create a toggler that resets to the invisible state when the notebook is closed. Here is a function that does this:
hideOnClose /: MakeBoxes[hideOnClose[expr_], fmt:StandardForm] := Module[{i=1},
TemplateBox[
{MakeBoxes[expr,fmt]},
"Hide",
DisplayFunction -> (TogglerBox[
Dynamic[i],
{1 -> PaneBox[#, ImageSize->Scaled[1]], 2->""},
DynamicBox[ToBoxes[i], Deinitialization:>(i=2)],
ImageSize->All, Alignment->Center]
&),
InterpretationFunction->(#&)
]
]
The simplest way to implement this is to use:
$PrePrint = hideOnClose
Using $PrePrint
means that you can refer to Out
and the hideOnClose
wrapper won't cause trouble. As an example, suppose you evaluate:
Graphics[{Red,Circle[]}]
$PrePrint =.
2+2

After closing the notebook, and reopening, you will see:
Graphics[{Red,Circle[]}]
$PrePrint =.
2+2

The red circle has disappeared, but the 2+2 output has not because I unset the $PrePrint
first. One might consider using a custom style in a style sheet instead, eg., "HideInput" with a CellProlog
and CellEpilog
that sets and unsets $PrePrint
. Then, use "HideInput" for cells you want to hide.