Application Building with Mathematica

First steps in sharing your work

Jon McLoone - Wolfram Research Europe

The aim of this presentation is to look in more detail at the steps the developer must take to develop and deploy a Mathematica based application.

Application Development

Program code

Documentation

GUI development

Web Deployment

Code Development

Interactive experiments

Clear[x]

Series[Sin[x], {x, 0, 10}]

Normal[%]

p1 = Plot[%, {x, -10, 10}]

p2 = Plot[Sin[x], {x, -10, 10}]

Show[p1, p2]

Package

BeginPackage["SeriesCompare`"];

\(\(\(														\)\( (*Usage\ messages*) \)\)\)

SeriesCompare::usage = "SeriesCompare[fn,order,range] generates a plot of fn[x] for x from -range to range together with the series expansion up to x^order";

SeriesCompare::usage::French = "SeriesCompare[fn,order,range] generates a plot of fn[x] for x from -range to range together with the series expansion up to x^order";

Begin["`Private`"]; 									(*Hide symbols in private context*)

SeriesCompare[fn_, n_Integer, (range_)?NumberQ, opts___] := 
   Block[{ser}, 
   ser = Normal[Series[fn[x], {x, 0, n}]]; 				(*Generate series expansion and 																convert to polynomial*)
   Plot[{fn[x], ser},{x, -range, range}, 				(*Plot together with original*)
   		PlotStyle -> {GrayLevel[0], RGBColor[0, 0, 1]}, (*Color plots differently*)
   		opts]];

End[]; 													(*End package contexts*)

EndPackage[];

Summary

• Mechanism provided for building function libraries
• Automatic support for templates, multi-lingual messages and help
• Local scoping to prevent conflicts between packages

Documentation Development

The Mathematica Help Browser

First steps

Create content in notebooks

Catagorising for the Mathematica Help Browser

Author Tools Palette

Summary

• Tools provided for creation of add-on documentation
• Tree based browser for fast access
• Contents copyable, editable and evaluatable but protected against change
• Automatic creation of central index

GUI Development

Example

First steps

<< GUIKit`

\(\(ref = 
        GUIRun[
Widget["TextField", {"text" -> "Inital value"}, 
            Name -> "fieldname"]
\ \ \ \ \ \ \ \ \ \ ];\)\)

ref@PropertyValue[{"fieldname", "text"}]

ref@
        SetPropertyValue[{"fieldname", "text"}, "new \
value"];

Widget gallery

Panel

Button

Slider

CheckBox

RadioButton

Label

TextField

PasswordField

List

ComboBox

TextArea

TextPanel

Icon, ImageLabel

MenuBar
Menu
MenuItem

ToolBar

PopupMenu

Tree

Table

ColorChooser

FontChooser

Wizard

A complete interface

\(Widget["Panel", 
      WidgetGroup[{

{Widget["Label", {"text" -> "Function to compare:", "horizontalAlignment" -> PropertyValue["Right"]}], 
WidgetAlign[], 

Widget["TextField", {"text" -> "Sin", BindEvent["action", Script[updatePlot[]]]}, 
              WidgetLayout -> {"Stretching" -> {Maximize, 
                      None}}, 
              Name -> "functionField"]}, 

{Widget["Label", {"text" -> "Number of terms: ", "horizontalAlignment" -> PropertyValue["Right"]}], 
            WidgetAlign[], 

Widget["TextField", {"text" -> "5", "horizontalAlignment" -> 
                  PropertyValue["Right"], 
                BindEvent["action", Script[updatePlot[]]]}, 
              WidgetLayout -> {"Stretching" -> {Maximize, 
                      None}}, Name -> "termsField"], 
            WidgetSpace[
              10], 

Widget["Label", {"text" -> "Range: ±", "horizontalAlignment" -> PropertyValue["Right"]}], 

Widget["TextField", {"text" -> "4", "horizontalAlignment" -> 
                  PropertyValue["Right"], 
                BindEvent["action", Script[updatePlot[]]]}, 
              WidgetLayout -> {"Stretching" -> {Maximize, 
                      None}}, 
              Name -> "rangeField"]}, 

Widget["MathPanel", {"preferredSize" -> 
                Widget["Dimension", {"width" -> 
                      250, "height" -> 250}], "usesFE" -> 
                True}, WidgetLayout -> {"Stretching" -> {Maximize, Maximize}}, 
            Name -> "canvas"], 

BindEvent["componentResized", 
            Script[updatePlot[]]], 
BindEvent["endModal", 
            Script[
              Show[expr]]], 
Script[\(Needs["SeriesCompare`"];\)], 
          Script[updatePlot[] := 
              Block[{functionExpr, terms, 
                  range, $DisplayFunction = Identity}, 
                functionExpr = 
                  ToExpression[
                    PropertyValue[{"functionField", "text"}]]; 
terms = 
                  ToExpression[
                    PropertyValue[{"termsField", "text"}]]; 
range = 
                  ToExpression[
                    PropertyValue[{"rangeField", "text"}]]; 
expr = 
                  SeriesCompare[functionExpr, terms, 
                    range]; 
SetPropertyValue[{"canvas", "mathCommand"}, 
                  ToString[expr, InputForm]]; 
                InvokeMethod[{"canvas", "repaintNow"}];]; 
            updatePlot[]]}, 
WidgetLayout -> Column], 
      WidgetLayout -> {"Stretching" -> {True, 
              True}}]\)

GUI Re-use

Quit[];

<< GUIKit`

GUIRunModal["SeriesCompareGUI"]

\(GUIRunModal[
Widget["Panel", \ {
Widget["Label", {"text" -> "Multiple Plot 
sessions"}], 
{Widget["SeriesCompareGUI", \ 
              ExposeWidgetReferences -> {"canvas" -> "canvas1"}], 
Widget["SeriesCompareGUI", 
              ExposeWidgetReferences -> {"canvas" -> "canvas2"}]}, 


BindEvent["endModal", 
Script[\ 
              Show[\ GraphicsArray[
\ 
                  Partition[
\(ToExpression[
                          PropertyValue[{#, "mathCommand"}]] &\)\  /@ \ 
Select[\ "WidgetNames" /. GUIInformation[], \ 
                        StringMatchQ[#, "canvas*"] &], 
2]]]\ \ ]]
}]
]\)

Summary

• Language extension for providing graphic user interfaces
• Wide collection of pre-built widgets and examples
• Support for modular development and widget library extension

Web Deployment

Example: Star chart

First steps

webMathematicaAuthor Palette

http://193.112.242.140/webMathematica/seriescompare.jsp

Summary

• Tools for quick conversion of Mathematica notebooks to active webMathematica pages
• Supports all standard HTML (can use other web development tools)
• Does not require client plugins

Summary: Tools used in this presentation

Presentation & code development: 	Mathematica 5.1

Documentation: 	Author Tools (included with Mathematica 5.1 and 5.0)

GUI: 	GUIKit (included with Mathematica 5.1)

Web page creation:	 webMathematicaAuthor (available free http://www.wolfram.com/products/webmathematica/resources/author/)

Web deployment: 	webMathematica and Apache Tomcat