1

When coding in notebook, I'm trying to show result(s) in a Single Notbook Dialog by this,

fPreview[content_,opts : OptionsPattern[]] :=
    CreateDialog[
    Pane[content, 
        ImageSize -> {500-10, 600-20},
        Scrollbars -> True, 
        AppearanceElements -> None, 
        Alignment -> Center],
        WindowTitle -> "Preview" <> " " <> ToString@Head@content,
        WindowSize -> {500, 600},opts]

i.e.

 fPreview@Plot[Sin[x],{x,-4,4}]
 fPreview@Plot3D[Sin[x y],{x,-4,4},{x,-4,4}]

but once fPreview applied, there be a new Notebook Dialog, so I wonder how to modify the upper codes so that there be only One Notebook Dialog instance displaying the latest content(s)? Thanks!

Jerry
  • 2,459
  • 9
  • 15

1 Answers1

1

There are many additional features one could need here but let's address exactly what you have asked for:

  (* I will skip styling options for clarity*)

fPreview[content_, opts : OptionsPattern[]] := With[
  { nb = CreateDialog[ content, opts] }
, ClearAll[fPreview]
; fPreview[new_, nvm___] := NotebookWrite[
    First @ Cells[nb], 
    Cell @ BoxData @ ToBoxes @ new
  ]
]

In general you may need to do something based on this pattern: how to generate repeatable Unique streams?, just instead of i++& it should return a log function you can use to log your results. And this way you can have many notebooks at once.

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • Thanks! It comes $Failed when fPreview executed a second time if the Notebook Dialog created at the first time was closed, I try to fix it but get failure. – Jerry Jan 29 '19 at 15:25