WLJS LogoWLJS Notebook

FrontSubmit

Wolfram Kernel

To send an expression to be evaluated on frontend / browser:

FrontSubmit[expr_, opts___]

where expr can be a small subset of Wolfram Language symbols for basic math operations or any frontend symbols/expressions.

To sends an raw Javascript expression to be executed on frontend / browser, use:

FrontSubmit[expr_String, opts___]

where expr is any valid JS expression represented as a string.

Options

"Window"

specifies a window object, where an expression will be evaluated. Use CurrentWindow to fetch a window object from the evaluation context.

Be aware of a context loss in a case of an handler function called from outside. For example with Parallel kernels or SetTimeout one have to explicitly provide a WindowObj

With[{win = CurrentWindow[]},
	(* Current evaluation context *)
	
	SetTimeout[
		(* Evaluation context lost!!! *)
		FrontSubmit[Alert["Boom"], "Window"->win]
	, 3000];
	
	"Hi there!"
]

Examples

Evaluating frontend symbols

FrontSubmit[Alert["Hello World!"]]

which will produce a pop-up modal window.

Or to call a custom-defined Javascript function

cell 1
.js
core.reversed = async (args, env) => {
	const text = await interpretate(args[0], env);
	return (text..split("").reverse().join(""));
}
cell 2
FrontSubmit[reversed["Must be reversed..."] // Alert];

Append objects to a static graph

We can append anything to a graphics canvas without reevaluation of a cell

cell 1
scene = FrontInstanceReference[];
Plot[x, {x,0,1}, Epilog->{scene}]

and then

cell 2
FrontSubmit[Arrow[RandomReal[{0,1}, {2,2}]], scene]

to append an arrow to an existing graph.

Overwritting ViewBox

A typical graphics figure is usually a ViewBox. Here we can reference it using FrontInstanceReference

plot = FrontInstanceReference[];
Plot[x, {x,0,1}, Epilog->{plot}]

and then we can destroy it and replace with some other text

FrontSubmit[ViewBox`OuterExpression["BAM!"], plot] 

On this page