I am writing a notebook programmatically. I would like the FrontEnd to wait until an evaluation is finished, before continuing. This is best explained with examples:
The following opens a notebook, writes a text cell, writes an input cell, evaluates it, and continues to write more text cells. Notice that the evaluation doesn't finish until the the the the FrontEnd finishes.
NotebookWrite[exampleNB,
Cell["example of evaluating input programmatically",
"Text"]]; NotebookWrite[exampleNB,
Cell["D[Sin[ RandomReal[] x],x]" , "Input", CellTags -> 1]];
SelectionMove[exampleNB, Previous, Cell];
SelectionEvaluate[exampleNB];
NotebookWrite[exampleNB, Cell[RandomWord[], "Text"]];
Pause[5];
NotebookWrite[exampleNB, Cell[RandomWord[], "Text"]];
This is related to a similar post, the answer to which almost gets me to a solution. How to wait a Cell in a Notebook to be executed until another Notebook's Cell expressions executed?
That post's solution doesn't work for my case, because this will evaluate all input cells:
NotebookWrite[exampleNB,
Cell["example of evaluating input programattically",
"Text"]]; NotebookWrite[exampleNB,
Cell["D[Sin[ RandomReal[] x],x]" , "Input", CellTags -> 1]];
SelectionMove[exampleNB, Previous, Cell];
NotebookEvaluate[exampleNB, InsertResults -> True];
NotebookWrite[exampleNB, Cell[RandomWord[], "Text"]];
Pause[1];
NotebookWrite[exampleNB,
Cell["D[Sin[ RandomReal[] x],x]" , "Input", CellTags -> 1]];
SelectionMove[exampleNB, Previous, Cell];
NotebookEvaluate[exampleNB, InsertResults -> True];
Pause[1];
NotebookWrite[exampleNB, Cell[RandomWord[], "Text"]];
I've tried a solution with CellTags. This works, but there is a problem: there is no output from the evaluation.
NotebookWrite[exampleNB,
Cell["example of evaluating input programattically",
"Text"]]; NotebookWrite[exampleNB,
Cell["D[Sin[ RandomReal[] x],x]" , "Input", CellTags -> 1]];
SelectionMove[exampleNB, Previous, Cell];
NotebookEvaluate[exampleNB,
EvaluationElements -> {"CellTags" -> 1}];
NotebookWrite[exampleNB, Cell[RandomWord[], "Text"]];
Pause[2];
NotebookWrite[exampleNB,
Cell["D[Sin[ RandomReal[] x],x]" , "Input", CellTags -> 2]];
SelectionMove[exampleNB, Previous, Cell];
NotebookEvaluate[exampleNB, EvaluationElements -> {"CellTags" -> 2}];
Pause[2];
NotebookWrite[exampleNB, Cell[RandomWord[], "Text"]];
One might think that adding the InsertResults->True option would insert results, but it doesn't:
NotebookWrite[exampleNB,
Cell["example of evaluating input programattically",
"Text"]]; NotebookWrite[exampleNB,
Cell["D[Sin[ RandomReal[] x],x]" , "Input", CellTags -> 1]];
SelectionMove[exampleNB, Previous, Cell];
NotebookEvaluate[exampleNB, InsertResults -> True,
EvaluationElements -> {"CellTags" -> 1}];
NotebookWrite[exampleNB, Cell[RandomWord[], "Text"]];
Pause[2];
NotebookWrite[exampleNB,
Cell["D[Sin[ RandomReal[] x],x]" , "Input", CellTags -> 2]];
SelectionMove[exampleNB, Previous, Cell];
NotebookEvaluate[exampleNB, InsertResults -> True,
EvaluationElements -> {"CellTags" -> 2}];
Pause[2];
NotebookWrite[exampleNB, Cell[RandomWord[], "Text"]];
I think that this may be a buglet.
Is there a work-around? My goal here is simple. Get the input's evaluation to display before the rest of the cells are written.
It may seem like a strange thing to want to do, but if is anyone is interested a out why, I'll add this as a comment.
TaskWait
may help $\endgroup$