Bug introduced in V11.1 or earlier and persists through V11.3
[CASE:4061569], confirmed
(bug is described below)
Goal
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).
Attempts
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).
Question
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"]]
NotebookGet@nb
(* 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"] *)
NotebookEvaluate[nb]
(* 3 *)
(* doesn't do anything *)
SetOptions[nb, Visible -> True]
NotebookGet@nb
(* 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 *)
NotebookPut@NotebookGet@nb
(* 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 theNotebookObject
,...) that was present before.