Skip to main content
edited tags
Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751
added 54 characters in body
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751

I have a useful snippet of code that I use for anything in my Notebooknotebook that takes a while to execute and doesn't need to be executed for subsequent cells:

buttonEvaluate[expr_] := 
  Module[{cell}, 
   Button["Evaluate", cell 
 = Cell[BoxData[ToBoxes[
   expr
cell = Cell[BoxData[ToBoxes[ expr ]], "Output"];
     SelectionMove[ButtonNotebook[], All, ButtonCell]; 
     NotebookWrite[ButtonNotebook[], cell]
     ,
     Method -> "Queued"
]]   ]
];

SetAttributes[buttonEvaluate, HoldFirst]; 

buttonEvaluate::usage = "Returns a button, that when pressed, replaces itself with the evaluation of the passed in expression. Useful for things you don't necessarily care to recalculate when the notebook is evaluated.";

I usually use it like this:

some[expression,that,takes] + a * long / time // buttonEvaluate

...and when that cell is executed, the output is a button labelled 'Evaluate'Evaluate, which, when pressed, replaces the output cell with the result. This means I can use Evaluation->Evaluate NotebookEvaluation->Evaluate Notebook, and it effectively skips over some cells until I explicitly choose to evaluate them.

However, I've run into an issue when I try to use it inside an expression, because the perfectionist in me wants to be able to do something like:

Row[{slowThing[] // buttonEvaluate, fastThing[]}]

However, the Button'sButton's action is to replace the ButtonCellButtonCell with the results, which inadvertently replaces everything, not just the Button'sButton's Box. So, two questions:

  1. Is there any way to select the BoxBox of the ButtonButton?
  2. Will replacing that selection using NotebookWriteNotebookWrite work how I want it to? I can imagine it might play havoc with Mathematica'sMathematica's formatting.

I have a useful snippet of code that I use for anything in my Notebook that takes a while to execute and doesn't need to be executed for subsequent cells:

buttonEvaluate[expr_] := 
  Module[{cell}, Button["Evaluate", cell = Cell[BoxData[ToBoxes[
   expr
   ]], "Output"];
  SelectionMove[ButtonNotebook[], All, ButtonCell]; 
  NotebookWrite[ButtonNotebook[], cell],
  Method -> "Queued"
]]
SetAttributes[buttonEvaluate, HoldFirst];
buttonEvaluate::usage = "Returns a button, that when pressed, replaces itself with the evaluation of the passed in expression. Useful for things you don't necessarily care to recalculate when the notebook is evaluated.";

I usually use it like this:

some[expression,that,takes] + a * long / time // buttonEvaluate

...and when that cell is executed, the output is a button labelled 'Evaluate', which, when pressed, replaces the output cell with the result. This means I can use Evaluation->Evaluate Notebook, and it effectively skips over some cells until I explicitly choose to evaluate them.

However, I've run into an issue when I try to use it inside an expression, because the perfectionist in me wants to be able to do something like:

Row[{slowThing[] // buttonEvaluate, fastThing[]}]

However, the Button's action is to replace the ButtonCell with the results, which inadvertently replaces everything, not just the Button's Box. So, two questions:

  1. Is there any way to select the Box of the Button?
  2. Will replacing that selection using NotebookWrite work how I want it to? I can imagine it might play havoc with Mathematica's formatting.

I have a useful snippet of code that I use for anything in my notebook that takes a while to execute and doesn't need to be executed for subsequent cells:

buttonEvaluate[expr_] := Module[{cell}, 
   Button["Evaluate",  
     cell = Cell[BoxData[ToBoxes[ expr ]], "Output"];
     SelectionMove[ButtonNotebook[], All, ButtonCell]; 
     NotebookWrite[ButtonNotebook[], cell]
     ,
     Method -> "Queued"
   ]
];

SetAttributes[buttonEvaluate, HoldFirst]; 

buttonEvaluate::usage = "Returns a button, that when pressed, replaces itself with the evaluation of the passed in expression. Useful for things you don't necessarily care to recalculate when the notebook is evaluated.";

I usually use it like this:

some[expression,that,takes] + a * long / time // buttonEvaluate

...and when that cell is executed, the output is a button labelled Evaluate, which, when pressed, replaces the output cell with the result. This means I can use Evaluation->Evaluate Notebook, and it effectively skips over some cells until I explicitly choose to evaluate them.

However, I've run into an issue when I try to use it inside an expression, because the perfectionist in me wants to be able to do something like:

Row[{slowThing[] // buttonEvaluate, fastThing[]}]

However, the Button's action is to replace the ButtonCell with the results, which inadvertently replaces everything, not just the Button's Box. So, two questions:

  1. Is there any way to select the Box of the Button?
  2. Will replacing that selection using NotebookWrite work how I want it to? I can imagine it might play havoc with Mathematica's formatting.
Tweeted twitter.com/#!/StackMma/status/552393713423749122
Source Link
Widjet
  • 199
  • 4

Getting the Box of a Button

I have a useful snippet of code that I use for anything in my Notebook that takes a while to execute and doesn't need to be executed for subsequent cells:

buttonEvaluate[expr_] := 
  Module[{cell}, Button["Evaluate", cell = Cell[BoxData[ToBoxes[
   expr
   ]], "Output"];
  SelectionMove[ButtonNotebook[], All, ButtonCell]; 
  NotebookWrite[ButtonNotebook[], cell],
  Method -> "Queued"
]]
SetAttributes[buttonEvaluate, HoldFirst];
buttonEvaluate::usage = "Returns a button, that when pressed, replaces itself with the evaluation of the passed in expression. Useful for things you don't necessarily care to recalculate when the notebook is evaluated.";

I usually use it like this:

some[expression,that,takes] + a * long / time // buttonEvaluate

...and when that cell is executed, the output is a button labelled 'Evaluate', which, when pressed, replaces the output cell with the result. This means I can use Evaluation->Evaluate Notebook, and it effectively skips over some cells until I explicitly choose to evaluate them.

However, I've run into an issue when I try to use it inside an expression, because the perfectionist in me wants to be able to do something like:

Row[{slowThing[] // buttonEvaluate, fastThing[]}]

However, the Button's action is to replace the ButtonCell with the results, which inadvertently replaces everything, not just the Button's Box. So, two questions:

  1. Is there any way to select the Box of the Button?
  2. Will replacing that selection using NotebookWrite work how I want it to? I can imagine it might play havoc with Mathematica's formatting.