Bug introduced in V11.1 or earlier and persists through V11.3

[CASE:4061569], confirmed

(bug is described below)


My goal is the following:

  • Create a notebook programmatically, while Visible->False is set.
  • Evaluate some part of it to generate the output cells.
  • When done, show the resulting notebook.

The subject of this question is the second (and partly third point).


My first idea was to use SelectionEvaluate to evaluate the part in question. This has the issue that it does not wait for the evaluation to be done, showing the notebook too early. Reading this answer, it seems there's no way to do what I want using SelectionEvaluate.

The next attempt was to use the suggested NotebookEvaluate. Besides the obvious complication that there is no easy way to only evaluate part of the notebook, I have stumbled across a more severe issue (see below).


What is the best way to achieve my goal? urrently, I'm thinking about tagging all the cells I want evaluated and the use the tag based filtering of NotebookEvaluate. While being extremely inconvenient, this also suffers from the issue below. Ideally, I would prefer something more equivalent to SelectionEvaluate + "wait for it to be done" than this.

Issue with NotebookEvaluate

The following is an attempt to generate a notebook, evaluate it, and then show the result:

nb = CreateNotebook[Visible -> False]
(* NotebookObject[...] *)

NotebookWrite[nb, Cell[BoxData@ToBoxes@Unevaluated[1 + 2], "Input"]]

(* Notebook[{Cell[BoxData[RowBox[{"1", "+", "2"}]], "Input"]}, 
 WindowSize -> {775, 872}, 
 WindowMargins -> {{564, Automatic}, {Automatic, 74}}, 
 Visible -> False, 
 FrontEndVersion -> "11.3 for Microsoft Windows (64-bit) (March 6, 2018)", 
 StyleDefinitions -> "Default.nb"] *)

(* 3 *)

(* doesn't do anything *)
SetOptions[nb, Visible -> True]

(* Notebook[{Cell[BoxData[RowBox[{"1", "+", "2"}]], "Input", CellLabel -> "In[1]:="]},
 WindowSize -> {775, 872}, 
 WindowMargins -> {{564, Automatic}, {Automatic, 74}}, 
 Visible -> True, 
 FrontEndVersion -> "11.3 for Microsoft Windows (64-bit) (March 6, 2018)", 
 StyleDefinitions -> "Default.nb"] *)

(* displays the notebook successfully *)
(* NotebookObject[...] *)

Something about NotebookEvaluate causes the notebook to be permanently invisible (I assume this is a bug?).

  • Note that making the notebook visible before calling NotebookEvaluate works as expected
  • NotebookPut@*NotebookGet fixes the issue, but is of course ugly and breaks any state (selections, other references to the NotebookObject,...) that was present before.

1 Answer 1


Here is a workaround, the idea is to extract cells you need, evaluate them in separate notebooks and insert results back.

The limitation is that it does not handle output cells that already exist for input you want to evaluate. You may want to grab them too or delete generated cells via token or something.

nb = CreateDocument[{
    ExpressionCell[Defer[1 + 1], "Input"],
    ExpressionCell[Defer[2 + 1], "Input"],
       (*multiline cell*)
    Cell[BoxData[{MakeBoxes[1 + 1], MakeBoxes[1 + 2]}], "Input"]
}, Visible -> False];

And the procedure:

cells = Cells[nb, CellStyle -> {"Input"}][[{1, 3}]]
 Module[{cell, nb, result, sourceCell, sourceNb}
 , sourceCell = #;
   sourceNb = ParentNotebook @ sourceCell;
   cell = NotebookRead @ sourceCell;
   nb = NotebookPut @ Notebook[{cell}, Visible -> False];
   NotebookEvaluate[nb, InsertResults -> True];
   result = NotebookGet[nb][[1]];
   SelectionMove[sourceCell, All, Cell];
   NotebookWrite[sourceNb, result];
 ] &,

SetOptions[nb, Visible -> True];

The 3rd problem does not apply because it is not the original notebook which was evaluated. I'd report the visibility problem.

  • $\begingroup$ Thanks a lot for the suggestion! Using a separate notebook to do this should actually simplify several less-than-ideal workarounds for some of the other issues I've encountered - and none of the limitations you've pointed out should matter in my case (lucky me, for once at least ) $\endgroup$
    – Lukas Lang
    Commented May 2, 2018 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.