1
$\begingroup$

In recent versions of Mathematica, a number of functions have the feature that, while they are thinking (or, for example, loading data from Mathematica servers), they pop-up a little light-blue box that then disappears when the thinking/loading is complete. An easy way to see an example of this is to evaluate a Quantity function after resetting the Mathematica Kernel via Quit[]; for example:

(* any recognized or unrecognized quantity should work here, 
   so long as Mathematica was just restarted: *)
Quantity[1, "quuxatons"]

Immediately after entering this, I see a small blue box that says "Interpreting unit ...." which quickly disappears and is replaced with the appropriate error. Notably, this seems to be an output cell that is then either modified or deleted.

I'm wondering how these are created/managed. I'm not overly concerned that the appearance be identical (though it would be nice for consistency); I'm mostly interested in how the cell gets deleted. An example of code I've written in the past is below, and it works just fine, except that the progress message never goes away.

SomeLongRunningFunction[] := Module[
  {progress = 0},
  Print@Row[
    {"Progress: ", Dynamic[progress], "%"},
    BaseStyle -> Directive[11, FontFamily -> "Arial", Background -> LightBlue],
    RoundingRadius -> 0,
    Frame -> True,
    FrameMargins -> {{30, 30}, {10, 10}},
    Alignment -> {Center, Center}];
  Do[
   (Pause[0.1]; (* ... *)
    progress += 10),
   {10}]]

Is there a built-in function for making/managing these temporary messages?
If not, how does one delete a cell that was just printed (like in the example above)?

$\endgroup$
  • $\begingroup$ I think you probably just want PrintTemporary ??? Replace your Print with it and see whether that does what you want... $\endgroup$ – Albert Retey Jan 12 '16 at 22:58
2
$\begingroup$

Here is one quick way of implementing this behaviour:

Module[{i = 0, nb = EvaluationNotebook[]},

 CellPrint@Cell[BoxData[
    ToBoxes@Panel[Style["Hello World ....", "Button",
       GrayLevel[0.5],
       FontColor -> GrayLevel[0.5]],
      Alignment -> {Center, Center}, 
      Appearance -> {"Default" -> 
         FrontEnd`FileName[{"Typeset", "PrintTemporary"}, 
          "LightBlue.9.png"]}, FrameMargins -> {{12, 12}, {8, 12}}]], 
   "PrintTemporary", CellTags -> {"$temp"}];

 Do[i++; Pause[.2];
  If[i == 10,
   NotebookLocate["$temp"];
   NotebookDelete[nb]
   ], {10}];

 CellPrint@Cell[BoxData[ToBoxes@i], "Output"];
 SelectionMove[nb, Previous, Cell, 2];
 SelectionMove[nb, After, Cell];
 ]

The SelectionMove stuff at the end is there due to some autoscrolling that took place. The blue colour in the box seems to have been bleached out while making the GIF.

enter image description here

Edit

I'd forgotten about PrintTemporary but Alberts comment reminded me:

Module[{i = 0}, 
 PrintTemporary@
  Panel[Style["Hello World ....", "Button", GrayLevel[0.5], 
    FontColor -> GrayLevel[0.5]], Alignment -> {Center, Center}, 
   Appearance -> {"Default" -> 
      FrontEnd`FileName[{"Typeset", "PrintTemporary"}, 
       "LightBlue.9.png"]}, FrameMargins -> {{12, 12}, {8, 12}}];

 Do[i++; Pause[.2], {10}];
 i]
$\endgroup$
  • $\begingroup$ Cool, thanks! Can you confirm a few things for me? It looks like this works by tagging the cell "$temp" (which really could be anything generated with Unique I imagine) then finding that cell and deleting it, yes? Also, it looks like you grabbed some official styling using the Appearance -> {...}; is the "LightBlue.9.png" officially documented? And do you know if there is any documentation of the available options for Appearance? I couldn't find much (the help pages don't appear to list anything like what you've used). $\endgroup$ – nben Jan 12 '16 at 21:53
  • $\begingroup$ @user21382 yes you can call the tag whatever you want as long as it is unique in the notebook. I don't know how this is implemented by Wolfram. I just made this on the fly by looking at the box code of the blue box when it was generated. $\endgroup$ – Mike Honeychurch Jan 12 '16 at 22:01
  • $\begingroup$ @user21382 I forgot to say that alternatively you could use SelectionMove to select the temporary cell but I preferred this option. $\endgroup$ – Mike Honeychurch Jan 13 '16 at 5:04
  • $\begingroup$ @AlbertRetey thanks. I made my edit too fast without checking! $\endgroup$ – Mike Honeychurch Jan 13 '16 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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