Suppose I have a long notebook with three parts:
Remove["Global`*"];
(*global settings*)
$globalvariable[]=1;
(*main functions*)
foo[]:=Print@$globalvariable[];
(*collecting the results*)
foo[]
Each time using this notebook, I just change the global settings and then Evaluate Notebook
. The problem is that the Output[]
in the third part will be overwritten, and to prevent this behaviour I need to add a new line by hand between foo[]
and Output[]
.
Is there a method of preventing overwriting the output only for foo[]
without modifying any behaviour of foo[]
?
I tried wrapping foo[]
with Button
Button["test",foo[]]
as in this question but failed:
when
foo[]
is complicated containingPrint
,Message
,Timing
, etc,Button["test",foo[]]
's result is different fromfoo[]
;when
Button["test",foo[]]
is re-evaluated, the results generated by the button will be cleared.
I think there should be more direct approach but am not familiar with the front-end issues.
update 1
The situation is that when I am developing some package, this notebook serves as a pre-package before integrating the codes into *.wl
. I need to modify the global settings and main functions according to the outputs.
I think the current comments and answers are suitable for the final stable version of the codes. For this situation there are certainly indirect approaches like:
- packing all the relavent codes into a single function;
- integrating the relavent codes into a package or a notebook
- etc...
update 2: more background
The number of global variables and main functions of the notebook is ~100. The simplest function (three-point in 1-dim) is like the followings. As you can see, tracking all symbols across functions is inconvenient.
The current method I used is saving all the codes into one notebook init1.nb
, and working in another notebook with
Module[{path,notebook},path=NotebookDirectory[]<>"init1.nb";
notebook=NotebookOpen[path,CellContext->"Global`"];
FrontEndExecute[FrontEndToken[notebook,"SelectAll"]];
FrontEndExecute[FrontEndToken[notebook,"Evaluate"]]
]
Example codes:
threePoint[x1_, x2_,
x3_, \[CapitalDelta]1_, \[CapitalDelta]2_, \[CapitalDelta]3_] := \
(x1 - x2)^(-\[CapitalDelta]1 - \[CapitalDelta]2 + \[CapitalDelta]3) \
(x2 - x3)^(\[CapitalDelta]1 - \[CapitalDelta]2 - \[CapitalDelta]3) \
(-x1 + x3)^(-\[CapitalDelta]1 + \[CapitalDelta]2 - \[CapitalDelta]3) \
;
threePointCoefficient[rank1_, rank2_, rank3_, i1_, i2_, i3_] :=
0 /; (i1 <= 0 || i1 > rank1) || (i2 <= 0 ||
i2 > rank2) || (i3 <= 0 || i3 > rank3);
threePointCoefficient[rank1_, rank2_, rank3_, i1_, i2_, i3_] :=
sub[symbolThreePointCoefficient, i1, i2, i3];
threePointFactor[{rank1_, rank2_, rank3_, i1_, i2_, i3_}, k1_, k2_,
k3_] := \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(n1 = 0\), \(rank1 - i1\)]\(
\*UnderoverscriptBox[\(\[Sum]\), \(n2 = 0\), \(rank2 - i2\)]\(
\*UnderoverscriptBox[\(\[Sum]\), \(n3 = 0\), \(rank3 - i3\)]
\*FractionBox[\(
\*SuperscriptBox[\(k1\), \(n1\)]\
\*SuperscriptBox[\(k2\), \(n2\)]\
\*SuperscriptBox[\(k3\), \(n3\)]\), \(\(n1!\)\ \(n2!\)\ \(n3!\)\)]\ \
threePointCoefficient[rank1, rank2, rank3, i1 + n1, i2 + n2,
i3 + n3]\)\)\);
threePointFactor2[{rank1_, rank2_, rank3_, i1_, i2_, i3_}, x1_, x2_,
x3_] :=
threePointFactor[{rank1, rank2, rank3, i1, i2,
i3}, -Log@ratioX[x2, x3, x1], -Log@ratioX[x3, x1, x2], -Log@
ratioX[x1, x2, x3]];
threePoint[{rank1_, rank2_, rank3_, i1_, i2_, i3_}, x1_, x2_,
x3_, \[CapitalDelta]1_, \[CapitalDelta]2_, \[CapitalDelta]3_] :=
threePoint[x1, x2,
x3, \[CapitalDelta]1, \[CapitalDelta]2, \[CapitalDelta]3] \
threePointFactor[{rank1, rank2, rank3, i1, i2,
i3}, -Log@ratioX[x2, x3, x1], -Log@ratioX[x3, x1, x2], -Log@
ratioX[x1, x2, x3]];
foo[]
will generate a new cell storing the messages, prints and outputs, instead of overwritting the latest ones. $\endgroup$$globalvariable[]
conveniently. $\endgroup$