Function Repository Resource:

AsynchronousDynamicModule

Source Notebook

A wrapper function for dynamic modules that causes a placeholder to be shown during initialization

Contributed by: Sjoerd Smit

ResourceFunction["AsynchronousDynamicModule"][DynamicModule[,Initialization(init)]]

returns a new DynamicModule that initializes the expression init asynchronously. During the initialization, a spinner will be shown and the content of the DynamicModule will not evaluate before the initialization has finished.

ResourceFunction["AsynchronousDynamicModule"][DynamicModule[],placeholder]

shows placeholder during initialization.

ResourceFunction["AsynchronousDynamicModule"][DynamicModule[],placeholder,flag]

uses the variable flag to determine if the initialization has finished.

Details

The SynchronousInitialization option of the original DynamicModule will be ignored. The returned DynamicModule will always have SynchronousInitializationFalse.
If no Initialization option is specified, ResourceFunction["AsynchronousDynamicModule"] will return the DynamicModule unchanged.

Examples

Basic Examples (2) 

The following DynamicModule shows a pink box during the initialization because the inner dynamic causes an error message:

In[1]:=
DynamicModule[{x},
 Dynamic[First[x]],
 SynchronousInitialization -> False,
 Initialization :> (
   x = {};
   Pause[3];
   AppendTo[x, 1]
   )
 ]
Out[1]=

AsynchronousDynamicModule automatically fixes this problem:

In[2]:=
ResourceFunction["AsynchronousDynamicModule"]@DynamicModule[{x},
  Dynamic[First[x]],
  SynchronousInitialization -> False,
  Initialization :> (
    x = {};
    Pause[3];
    AppendTo[x, 1]
    )
  ]
Out[2]=

Scope (2) 

The Print statement will not evaluate before the initialization finishes. All evaluation of the DynamicModule is prevented before the initialization has finished:

In[3]:=
ResourceFunction["AsynchronousDynamicModule"]@DynamicModule[{x},
  Dynamic[
   Print["Hi!"];
   First[x]
   ],
  SynchronousInitialization -> False,
  Initialization :> (
    x = {};
    Pause[3];
    AppendTo[x, 1]
    )
  ]
Out[3]=

Any placeholder can be used to show progress of the initialization:

In[4]:=
ResourceFunction["AsynchronousDynamicModule"][
 DynamicModule[{x},
  Dynamic[
   x
   ],
  SynchronousInitialization -> False,
  Initialization :> (
    x = {};
    Pause[1];
    AppendTo[x, 1];
    Pause[1];
    AppendTo[x, 2];
    Pause[1];
    AppendTo[x, 3];
    Pause[1];
    AppendTo[x, 4];
    )
  ],
 Dynamic@ProgressIndicator[Length[x], {0, 4}]
 ]
Out[4]=

Applications (1) 

You can specify a dynamically updated variable that returns the content to its initialization state. This update action can then be reused in the content of the DynamicModule:

In[5]:=
ResourceFunction["AsynchronousDynamicModule"][
 DynamicModule[{
   x,
   start,
   updateAction
   },
  Dynamic[
   Column[{
     x,
     Slider[Dynamic[start], {0, 10, 1}],
     Button["Update",
      updateAction[start],
      Method -> "Queued",
      ImageSize -> All
      ]
     }]
   ],
  SynchronousInitialization -> False,
  Initialization :> (
    start = 0;
    updateAction[startVal_] := (
      initQ = False;
      x = {};
      Pause[1];
      AppendTo[x, startVal + 1];
      Pause[1];
      AppendTo[x, startVal + 2];
      Pause[1];
      AppendTo[x, startVal + 3];
      Pause[1];
      AppendTo[x, startVal + 4];
      initQ = True
      );
    updateAction[start]
    )
  ],
 Dynamic@ProgressIndicator[Length[x], {0, 4}],
 initQ
 ]
Out[5]=

Publisher

Sjoerd Smit

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 2.0.2 – 13 July 2021

License Information