Mathematica is a powerful tool for scientific computation. It is easy to create the simple interface within its notebook by using Maniplate function. However it is difficult to create the complex forms within its notebook because there is no visual form editor. The forms have to been generated by a lot of commands. Web interface is a powerful for web form. However Javascripts don’t run quickly in browsers. So we combine these two powerful tools to create web interface for mathematica.
Wolframscript
Not only Mathematica scripts run in the notebook in which the two dimensional math is used, but aslo run in the console in which the text math is used. The wolframscript program is in the root directory of the mathematica. It supports code directly and script. It starts a new session every time.
wolframscript -code a=2+2 // run the code directly.
wolframscript -file test.wl //run a script file.
The outputs don’t be displayed in the console except add -print all parameters. Arguments is behind the -args. It is composed to a string array $ScriptCommandLine in the script. The 1st element is the script name and the 2nd element is the first arguments and so on. ToExpression[string] evaluate the string as mathematica command.
If[Length[$ScriptCommandLine]>1,$ScriptCommandLine[[2]]//ToExpression];
Javascript objects is easy for the code to read. Export in mathematica saves the associated array to Javascript objects files.
json={"f"->Table[f,{f,220.4,221.6,0.02}],"Q"->Table[Q,{Q,1200,4000,100}],"r"->Table[reflect[f,q],{q,1200,4000,100},{f,220.4,221.6,0.02}]};
Export["D:\\data\\scan.json",json,"json"];
Web server and script
The web form can’t call the mathematica script in the server side directly from the browser. The web server and server side script have to been used to communicate. I use PHP script to transfer data between the web interface and the mathematica script. All the data from the web interface are stored in the $_REQUEST array. The PHP script read the array and send it to the mathematica script as an arguments of the script.
$cmd=sprintf('wolframscript -file math\\\\%s -args "geometry=<|\[Theta]1->%f Degree,\[Theta]2->%f Degree,L1->%f,L2->%f,L3->%f,R0->%f,m->%d,n->%d,f->%d|>"',"test.wl",$_REQUEST["theta1"],$_REQUEST["theta2"],$_REQUEST["L1"],$_REQUEST["L2"],$_REQUEST["L3"],$_REQUEST["R0"],$_REQUEST["m"],$_REQUEST["n"],$_REQUEST["f"]); //built the mathematica script for sending the arguments.
$handle=popen($cmd,"r"); //open a tunnels to run the script
pclose($handle);//close the tunnels.
readfile("temp.json"); //read the output file and send it to the web form.
Web form
Asynchronous JavaScript ans XML is a composed technology. It sends data and receives result from the server without refresh the whole web form. I use jQuery library to send and get data asynchronously.
$.get("json.php",{
m:m,
n:n,
L1:L1*0.001,
theta1:theta1,
L2:L2*0.001,
R0:R0*0.001,
L3:L3*0.001,
theta2:theta2,
f:fc,
Pout:Pout
},function(data,textStatus){
f=data.f;
Q=data.Q;
$("#f_val").html(format_physics(data.f,"GHz"));
$("#Q_val").html(data.Q.toFixed(0));
$("#r_val").html(data.r.toPrecision(4));
//console.log(data);
plot(data);
MathJax.typeset();
},"json");
Summary
An web interface for the mathematica script was developed by using PHP script as the messenger on the server side. It is easy to use and runs fast.