{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python API to EasyForm" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from beakerx import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f = EasyForm(\"Form and Run\")\n", "f.addTextField(\"first\")\n", "f.addTextField(\"last\")\n", "f['first'] = \"First\"\n", "f['last'] = \"Last\"\n", "f.addButton(\"Go!\", tag=\"run\")\n", "f" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "run" ] }, "outputs": [], "source": [ "\"Good morning \" + f[\"first\"] + \" \" + f[\"last\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f['last'][::-1] + '...' + f['first']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f['first'] = 'Beaker'\n", "f['last'] = 'Berzelius'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "h = EasyForm(title=\"Form and Run\")\n", "\n", "h.addTextField(\"first\", width=10)\n", "h.addTextField(\"default\")\n", "h.addTextArea(\"Text Area 1\", height=5, width=20)\n", "h.addTextArea(\"Text Area 2\")\n", "h.addTextArea(\"Text Area 3\", height=10)\n", "h.addTextArea(\"Text Area 4\",width=20)\n", "h" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "g2 = EasyForm(\"Field Types\")\n", "options = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"]\n", "g2.addList(\"List Single\", options, multi=False)\n", "g2.addList(\"List Two Row\", options, rows=2)\n", "g2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f['last']+ \", \"+f['first']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f['last'] = \"new Value\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f['first'] = \"new Value2\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# All Kinds of Fields\n", "\n", "g = EasyForm(\"Field Types\")\n", "g.addTextField(\"Short Text Field\", width=10)\n", "g.addTextField(\"Text Field\")\n", "g.addPasswordField(\"Password Field\", width=10)\n", "g.addTextArea(\"Text Area\")\n", "g.addTextArea(\"Tall Text Area\", 10, 5)\n", "g.addCheckBox(\"Check Box\")\n", "options = [\"a\", \"b\", \"c\", \"d\"]\n", "g.addComboBox(\"Combo Box\", options)\n", "g.addComboBox(\"Combo Box editable\", options, editable=True)\n", "\n", "g.addList(\"List\", options)\n", "g.addList(\"List Single\", options, multi=False)\n", "g.addList(\"List Two Row\", options, rows=2)\n", "\n", "g.addCheckBoxes(\"Check Boxes\", options)\n", "g.addCheckBoxes(\"Check Boxes H\", options, orientation=EasyForm.HORIZONTAL)\n", "\n", "g.addRadioButtons(\"Radio Buttons\", options)\n", "g.addRadioButtons(\"Radio Buttons H\", options, orientation=EasyForm.HORIZONTAL)\n", "\n", "g.addDatePicker(\"Date\")\n", "\n", "g.addButton(\"Go!\", tag=\"run2\")\n", "g" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "run2" ] }, "outputs": [], "source": [ "result = dict()\n", "for child in g:\n", " result[child] = g[child]\n", "\n", "result" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdp = EasyForm(\"Field Types\")\n", "gdp.addDatePicker(\"Date\")\n", "gdp" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdp['Date']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f.put(\"first\", \"Micheal\")\n", "f.put(\"last\", \"Fox\")\n", "# Read values from form\n", "firstName = f.get(\"first\")\n", "lastName = f.get(\"last\")\n", "\n", "print(\"Good morning \" + firstName + \" \" + lastName)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f = EasyForm(\"actionPerformed demo\")\n", "f.addTextField(\"first\")\n", "f['first'] = \"First\"\n", "b = f.addButton(\"Action!\")\n", "b.actionPerformed = lambda: print(\"clicked \"+f[\"first\"])\n", "f" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import operator\n", "\n", "f1 = EasyForm(\"OnInit and OnChange\")\n", "f1.addTextField(\"first\", width=15)\n", "f1.addTextField(\"last\", width=15)\\\n", " .onInit(lambda: operator.setitem(f1, 'last', \"setinit1\"))\\\n", " .onChange(lambda text: operator.setitem(f1, 'first', text + ' extra'))\n", "\n", "button = f1.addButton(\"action\", \"action_button\")\n", "button.actionPerformed = lambda: operator.setitem(f1, 'last', 'action done')\n", "f1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Default Values and placeholder" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f3c = EasyForm(\"form3\")\n", "f3c = EasyForm(\"form3\")\n", "f3c.addTextArea(\"Default Value\", value = \"Initial value\")\n", "f3c.addTextArea(\"Place Holder\", placeholder = \"Put here some text\")\n", "f3c.addCheckBox(\"Default Checked\", value = True)\n", "f3c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result = dict()\n", "for child in f3c:\n", " result[child] = f3c[child]\n", "\n", "result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## JupyterJSWidgets work with EasyForm\n", "\n", "The widgets from JupyterJSWidgets are compatible and can appear in forms." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from beakerx import *\n", "from ipywidgets import * \n", "w = IntSlider()\n", "\n", "f = EasyForm(\"Form and Run\")\n", "f.addTextField(\"first\")\n", "f.addTextField(\"last\")\n", "f.addWidget(\"slider\", w)\n", "f['first'] = \"First\"\n", "f['last'] = \"Last\"\n", "f.addButton(\"Go!\", tag=\"run\")\n", "f" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f['slider']" ] } ], "metadata": { "celltoolbar": "Tags", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 1 }