BeakerX has a Groovy API for creating forms that the user can fill in and trigger execution. It's easy to create a form with it, and easy to access the values entered. Just create a form object, add fields to it, and then return it so it's displayed for the user to interact with.
The 2nd parameter to the addButton method specifies a tag. All cells with that tag are executed when the button is pressed. Use the View → Cell Toolbar → Tags menu item to view and edit tags.
In [ ]:
f = new EasyForm("Form and Run")
f.addTextField("first")
f['first'] = "First"
f.addTextField("last")
f['last'] = "Last"
f.addButton("Go!", "run")
f
You can access the values from the form by treating it as an array indexed on the field names:
In [ ]:
"Good morning " + f["first"] + " " + f["last"]
In [ ]:
f['last'].reverse() + '...' + f['first']
The array works both ways, so you set default values on the fields by writing the array:
In [ ]:
f['first'] = 'Beaker'
f['last'] = 'Berzelius'
In [ ]:
f1 = new EasyForm("Form and Run")
f1.addTextField("first", 15)
f1.addTextField("last", 15).onInit({f1['last'] = "setinit1"}).onChange({text -> f1['first'] = text + " extra"})
button = f1.addButton("action", "action_button")
button.actionPerformed = {f1['last'] = 'action done'}
f1
In [ ]:
f1['last'] = "new Value"
In [ ]:
f1['first'] = "new Value2"
In [ ]:
g = new EasyForm("Field Types")
g.addTextField("Short Text Field", 10)
g.addTextField("Text Field")
g.addPasswordField("Password Field", 10)
g.addTextArea("Text Area")
g.addTextArea("Tall Text Area", 10, 5)
g.addCheckBox("Check Box")
options = ["a", "b", "c", "d"]
g.addComboBox("Combo Box", options)
g.addComboBox("Editable Combo", options, true)
g.addList("List", options)
g.addList("List Single", options, false)
g.addList("List Two Row", options, 2)
g.addCheckBoxes("Check Boxes", options)
g.addCheckBoxes("Check Boxes H", options, EasyForm.HORIZONTAL)
g.addRadioButtons("Radio Buttons", options)
g.addRadioButtons("Radio Buttons H", options, EasyForm.HORIZONTAL)
g.addDatePicker("Date")
g.addButton("Go!", "run2")
g
In [ ]:
result = [:]
g.keySet().each {
result[it] = g[it]
}
result
In [ ]:
gdp = new EasyForm("Field Types")
gdp.addDatePicker("Date")
gdp
In [ ]:
gdp['Date']
In [ ]:
easyForm = new EasyForm("Field Types")
// Setup date via String (format is yyyyMMdd) also works
easyForm.addDatePicker("Date").setDate("20180102")
easyForm
In [ ]:
h = new EasyForm("Default Values")
h.addTextArea("Default Value", value = "Initial value")
h.addCheckBox("Default Checked", value = true)
h.addButton("Press", tag = "check")
h
In [ ]:
result = [:]
h.keySet().each {
result[it] = h[it]
}
result
The widgets from JupyterJSWidgets are compatible and can appear in forms.
In [ ]:
import com.twosigma.beakerx.widget.IntSlider
w = new IntSlider()
widgetForm = new EasyForm("pyhton widgets")
widgetForm.addButton("Press", "widget_test")
widgetForm.addWidget("IntSlider",w)
widgetForm
In [ ]: