{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Easy Form\n", "\n", "Beaker has a Groovy API for creating forms that the user can fill in and trigger execution.\n", "It's easy to create a form with it, and easy to access the values entered.\n", "Just create a form object, add fields to it, and then return it so it's displayed for the\n", "user to interact with.\n", "\n", "The 2nd parameter to the addButton method specifies a tag.\n", "All cells with that tag are executed when the button is pressed.\n", "Use the **View → Cell Toolbar → Tags** menu item to view and edit tags." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "f = new EasyForm(\"Form and Run\")\n", "f.addTextField(\"first\", 250)\n", "f['first'] = \"First\"\n", "f.addTextField(\"last\", 250)\n", "f['last'] = \"Last\"\n", "f.addButton(\"Go!\", \"run\")\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can access the values from the form by treating it as an array indexed on the field names:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "tags": [ "run" ] }, "outputs": [ { "data": { "text/plain": [ "suilezreB...Beaker" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f['last'].reverse() + '...' + f['first']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The array works both ways, so you set default values on the fields by writing the array:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Berzelius" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f['first'] = 'Beaker'\n", "f['last'] = 'Berzelius'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use `onInit` and `onChange` to handle component events. For button events use `actionPerformed` or `addAction`." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "f1 = new EasyForm(\"Form and Run\")\n", "f1.addTextField(\"first\", 15)\n", "f1.addTextField(\"last\", 15).onInit({f1['last'] = \"setinit1\"}).onChange({text -> f1['first'] = text + \" extra\"})\n", "button = f1.addButton(\"action\", \"action_button\")\n", "button.actionPerformed = {f1['last'] = 'action done'} \n", "f1" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "tags": [ "action_button" ] }, "outputs": [ { "data": { "text/plain": [ "action done, action done" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f1['last'] + \", \" + f1['first']" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "new Value" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f1['last'] = \"new Value\"" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "new Value2" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f1['first'] = \"new Value2\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## All Kinds of Fields" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "g = new EasyForm(\"Field Types\")\n", "g.addTextField(\"Short Text Field\", 10)\n", "g.addTextField(\"Text Field\")\n", "g.addTextArea(\"Text Area\")\n", "g.addCheckBox(\"Check Box\")\n", "options = [\"a\", \"b\", \"c\", \"d\"]\n", "g.addComboBox(\"Combo Box\", options)\n", "g.addComboBox(\"Editable Combo\", options, true)\n", "\n", "g.addList(\"List\", options)\n", "g.addList(\"List Single\", options, false)\n", "g.addList(\"List Two Row\", options, 2)\n", "\n", "g.addCheckBoxes(\"Check Boxes\", options)\n", "g.addCheckBoxes(\"Check Boxes H\", options, EasyForm.HORIZONTAL)\n", "\n", "g.addRadioButtons(\"Radio Buttons\", options)\n", "g.addRadioButtons(\"Radio Buttons H\", options, EasyForm.HORIZONTAL)\n", "\n", "g.addDatePicker(\"Date\")\n", "\n", "g.addButton(\"Go!\", \"run2\")\n", "g" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true, "tags": [ "run2" ] }, "outputs": [], "source": [ "result = [:]\n", "g.keySet().each {\n", " result[it] = g[it]\n", "}\n", "result" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "gdp = new EasyForm(\"Field Types\")\n", "gdp.addDatePicker(\"Date\")\n", "gdp" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "class java.util.Date" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gdp['Date'].getClass()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import com.twosigma.beakerx.widgets.integers.IntSlider\n", "\n", "w = new IntSlider()\n", "\n", "widgetForm = new EasyForm(\"pyhton widgets\")\n", "widgetForm.addButton(\"Press\", \"widget_test\")\n", "widgetForm.addWidget(\"IntSlider\",w)\n", "widgetForm" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "tags": [ "widget_test" ] }, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "widgetForm['IntSlider']" ] } ], "metadata": { "anaconda-cloud": {}, "beakerx_kernel_parameters": {}, "celltoolbar": "Tags", "classpath": [], "imports": [], "kernelspec": { "display_name": "Groovy", "language": "groovy", "name": "groovy" }, "language_info": { "codemirror_mode": "groovy", "file_extension": ".groovy", "mimetype": "", "name": "Groovy", "nbconverter_exporter": "", "version": "2.4.3" }, "widgets": { "state": { "90dc3e2a-7242-492c-905f-a7dc4ce972fa": { "views": [ { "cell_index": 7 } ] } }, "version": "1.2.0" } }, "nbformat": 4, "nbformat_minor": 1 }