#!/usr/bin/env python # coding: utf-8 # # Python API to EasyForm # In[ ]: from beakerx import * # In[ ]: f = EasyForm("Form and Run") f.addTextField("first") f['first'] = "First" f.addTextField("last") f['last'] = "Last" f.addButton("Go!", tag="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'][::-1] + '...' + 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' # ## Event Handlers for Smarter Forms # # You can use `onInit` and `onChange` to handle component events. For button events use `actionPerformed` or `addAction`. # In[ ]: import operator f1 = EasyForm("OnInit and OnChange") f1.addTextField("first", width=15) f1.addTextField("last", width=15)\ .onInit(lambda: operator.setitem(f1, 'last', "setinit1"))\ .onChange(lambda text: operator.setitem(f1, 'first', text + ' extra')) button = f1.addButton("action", tag="action_button") button.actionPerformed = lambda: operator.setitem(f1, 'last', 'action done') f1 # In[ ]: f1['last'] + ", " + f1['first'] # In[ ]: f1['last'] = 'new Value' # In[ ]: f1['first'] = 'new Value2' # ## All Kinds of Fields # In[ ]: g = EasyForm("Field Types") g.addTextField("Short Text Field", width=10) g.addTextField("Text Field") g.addPasswordField("Password Field", width=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("Combo Box editable", options, editable=True) g.addList("List", options) g.addList("List Single", options, multi=False) g.addList("List Two Row", options, rows=2) g.addCheckBoxes("Check Boxes", options) g.addCheckBoxes("Check Boxes H", options, orientation=EasyForm.HORIZONTAL) g.addRadioButtons("Radio Buttons", options) g.addRadioButtons("Radio Buttons H", options, orientation=EasyForm.HORIZONTAL) g.addDatePicker("Date") g.addButton("Go!", tag="run2") g # In[ ]: result = dict() for child in g: result[child] = g[child] TableDisplay(result) # ### Dates # In[ ]: gdp = EasyForm("Field Types") gdp.addDatePicker("Date") gdp # In[ ]: gdp['Date'] # ### SetData # In[ ]: easyForm = EasyForm("Field Types") easyForm.addDatePicker("Date", value=datetime.today().strftime('%Y%m%d')) easyForm # ### Default Values and placeholder # In[ ]: h = EasyForm("Default Values") h.addTextArea("Default Value", value = "Initial value") h.addTextArea("Place Holder", placeholder = "Put here some text") h.addCheckBox("Default Checked", value = True) h.addButton("Press", tag="check") h # In[ ]: result = dict() for child in h: result[child] = h[child] TableDisplay(result) # ## JupyterJSWidgets work with EasyForm # # The widgets from JupyterJSWidgets are compatible and can appear in forms. # In[ ]: from ipywidgets import * w = IntSlider() widgetForm = EasyForm("python widgets") widgetForm.addWidget("IntSlider", w) widgetForm.addButton("Press", tag="widget_test") widgetForm # In[ ]: widgetForm['IntSlider']