Function Repository Resource:

DisplayWithProgress

Source Notebook

Display an expression with a progress panel

Contributed by: Jayanta Phadikar

ResourceFunction["DisplayWithProgress"][expr]

displays a progress indicator until expr is evaluated.

ResourceFunction["DisplayWithProgress"][expr,progress]

displays progress until expr is evaluated.

Details

The default progress indicator is ProgressIndicator[Appearence"Necklace"].
ResourceFunction["DisplayWithProgress"] works by creating a DynamicModule with an asynchronous initialization. All the code except the last element of a CompoundExpression is sent to Initialization in the DynamicModule. The progress pane is shown until the Initialization is over.
Monitor creates a new cell for displaying a progress pane. Thus a part of the interface cannot be updated while keeping the other parts as they are. ResourceFunction["DisplayWithProgress"] solves the problem by creating a DynamicModule.
The function generalizes the service provided by the resource function AsynchronousDynamicModule to include any general expressions, including Dynamic expressions.

Examples

Basic Examples (3) 

Display progress until "Ready" is shown:

In[1]:=
ResourceFunction["DisplayWithProgress"][Pause[3]; "Ready"]
Out[1]=

Displays "Loading…" until "Ready" is shown:

In[2]:=
ResourceFunction[
 "DisplayWithProgress"][(Pause[3]; "Ready"), "Loading..."]
Out[2]=

Display progress for an expression as an afterthought:

In[3]:=
(Pause[3]; "Ready") // ResourceFunction["DisplayWithProgress"]
Out[3]=

Scope (3) 

Display a progress pane until the contour plot loads:

In[4]:=
ResourceFunction["DisplayWithProgress"][
 ListContourPlot[
  Table[3*i^2 - 2*j^3, {i, -1.5, 1.5, 0.01}, {j, -1.5, 1.5, 0.01}], Sequence[PlotTheme -> {"Monochrome", 
RGBColor[1, 0.5, 0]}, FrameTicks -> None]],
 Style["Loading...", 15, FontColor -> Blue]
 ]
Out[4]=

Display a progress pane until a Dynamic loads:

In[5]:=
x = 5;
In[6]:=
ResourceFunction["DisplayWithProgress"][Dynamic[Pause[3]; x^2], Style["Loading...", 15, FontColor -> Blue]]
Out[6]=

Change the value of x and see the above getting updated:

In[7]:=
x = 10;

Display a ProgressIndicator until a DynamicModule finishes the initialization:

In[8]:=
ResourceFunction["DisplayWithProgress"][
 DynamicModule[{x = 0.5, y, f},
  Column[
   {
    Slider[Dynamic[x]],
    Dynamic[f[x]]
    }],
  Initialization :>
   (Pause[5];
    f[x_] := x^2)],
 ProgressIndicator[Appearance -> "Necklace"]
 ]
Out[8]=

Applications (8) 

Display a progress pane until an expensive graphic finishes rendering:

In[9]:=
graphic[] := Graphics[Flatten[
    Table[{RandomColor[], Disk[RandomReal[{0, 100}, 2], 0.3]}, 10^4]],
    PlotRange -> {{0, 100}, {0, 100}}, ImageSize -> {300, 300}];
In[10]:=
ResourceFunction["DisplayWithProgress"][graphic[], Pane[Style["Loading...", 20], Sequence[
  ImageSize -> {300, 300}, BaseStyle -> (Background -> LightBlue), Alignment -> Center]]]
Out[10]=

Display a progress pane while updating a time-consuming Dynamic with a slider:

In[11]:=
DynamicModule[{x},
 Column[
  {
   Slider[Dynamic[x], ContinuousAction -> False],
   ResourceFunction["DisplayWithProgress"][Dynamic[Pause[1.5]; x^2]]
   },
  Sequence[Alignment -> Center]]
 ]
Out[11]=

Use along with ControlActive:

In[12]:=
DynamicModule[{x},
 Column[
  {
   Slider[Dynamic[x]],
   Dynamic[
    ControlActive["Rendering...", ResourceFunction["DisplayWithProgress"][Pause[1.5]; x^2]]]
   },
  Sequence[Alignment -> Center]]
 ]
Out[12]=

Use with a time-consuming computation within a button:

In[13]:=
square[n_] := (Pause[2]; Row[{"Square of ", ToString[n], ": ", n^2}]);

n = 1;
Row[{
  Button[
   "Show squares",
   n = n + 1
   ],
  Spacer[10],
  ResourceFunction["DisplayWithProgress"][Dynamic[square[n]], "Computing..."]}
 ]
Out[7]=

Use with a checkbox:

In[14]:=
state = True;
Column[
 {
  Row[{Checkbox[Dynamic[state]], " Show time"}],
  Spacer[10], ResourceFunction["DisplayWithProgress"][
   Dynamic[If[state == True, Pause[3]; TimeObject[], ""]],
   "Computing..."
   ]
  }
 ]
Out[10]=

Display progress until the full interface loads:

In[15]:=
Grid[
 {{
   myFrame[Style[1, 70]],
   myFrame[
    ResourceFunction["DisplayWithProgress"][Pause[3]; Style[2, 70]]],
   myFrame[Style[3, 70]]
   }},
 Spacings -> -0.1, Alignment -> Center]
Out[15]=

Display ProgressIndicators in TabView:

In[16]:=
stylize[s_String] := Style[s, 40, Blue, FontFamily -> "Cambria"];

tab1 := ResourceFunction[
   "DisplayWithProgress"][(Pause[2]; stylize["Tab 1"])];
tab2 := ResourceFunction[
   "DisplayWithProgress"][(Pause[2]; stylize["Tab 2"])];
tab3 := ResourceFunction[
   "DisplayWithProgress"][(Pause[2]; stylize["Tab 3"])];

TabView[
 {
  Dynamic[tab1], Dynamic[tab2], Dynamic[tab3]
  },
 ImageSize -> Automatic, Alignment -> Center]
Out[17]=

Show the extent of the progress:

In[18]:=
i = 1;
ResourceFunction["DisplayWithProgress"][
 Do[
  i = i + 1; Pause[0.5], 19
  ]; "Ready",
 ProgressIndicator[Dynamic[i], {1, 20}]
 ]
Out[15]=

Properties and Relations (2) 

Monitor creates a new cell for displaying the progress pane:

In[19]:=
{1, Monitor[Pause[3]; 2, "Loading..."]}
Out[19]=

DisplayWithProgress can work on the specified part of the expression:

In[20]:=
{1, ResourceFunction["DisplayWithProgress"][Pause[3]; 2, "Loading..."]}
Out[20]=

DisplayWithProgress uses the resource function AsynchronousDynamicModule internally for rendering a DynamicModule:

In[21]:=
ResourceFunction["AsynchronousDynamicModule"][
 DynamicModule[{x = 0.5, y, f},
  Column[
   {
    Slider[Dynamic[x]],
    Dynamic[f[x]]
    }],
  Initialization :>
   (Pause[5];
    f[x_] := x^2)],
 ProgressIndicator[Appearance -> "Necklace"]
 ]
Out[21]=

Possible Issues (2) 

DisplayWithProgress works by creating a DynamicModule, thus might not be able to manipulate the output in typical ways:

In[22]:=
1 + ResourceFunction["DisplayWithProgress"][Pause[1]; 2, "Loading..."]
Out[22]=
In[23]:=
InputForm[%]
Out[23]=

The front end locks up while rendering (after evaluating the expression) an expensive graphics, and thus a progress pane containing an animation may hang for some time:

In[24]:=
graphic[] := Graphics[Flatten[
    Table[{RandomColor[], Disk[RandomReal[{0, 100}, 2], 0.3]}, 10^4]],
    PlotRange -> {{0, 100}, {0, 100}}, ImageSize -> {300, 300}];
In[25]:=
ResourceFunction["DisplayWithProgress"][graphic[], ProgressIndicator[Appearance -> "Necklace"]]
Out[25]=

It is better to use a static progress pane in such cases:

In[26]:=
ResourceFunction["DisplayWithProgress"][graphic[], Style["Loading...", 20, FontColor -> Blue]]
Out[26]=

Publisher

Jayanta Kumar Phadikar

Version History

  • 2.0.0 – 26 January 2021
  • 1.0.0 – 05 January 2021

Related Resources

License Information