{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "from seeq import spy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"background-color: #EEFFEE;text-align: left;\">Logged in to <strong>http://localhost:34216/api</strong> successfully as <strong>agent_api_key</strong>.<br>Seeq Server Version: <strong>R21.0.44.04</strong><br>Seeq Python Module Version: <strong>0.44.4.142</strong></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Log into the Seeq Server if you're not using Seeq Data Lab:\n",
    "spy.login(url='http://localhost:34216', credentials_file='../credentials.key', force=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# spy.widgets\n",
    "\n",
    "A set of ipython widgets designed specifically for working with Seeq.\n",
    "\n",
    "These widgets can be used on their own or incorporated into an ipywidgets UI."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## SeeqItemSelect\n",
    "\n",
    "The Seeq Item Selector is a configurable item selector. Creating a basic item selector that allows searching for \n",
    "seeq items by name and type can be created with no arguments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "416ca8739d994e41b4a6f230fc9d8e55",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "SeeqItemSelect(children=(HTML(value='', layout=Layout(display='none')), GridBox(children=(Accordion(children=(…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "item_selector1 = spy.widgets.SeeqItemSelect()\n",
    "display(item_selector1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Once a search has been performed, an item may be selected in the results window and information on that item is \n",
    "available through the `selected_value` attribute. Perform a search in the above selector and select an item before \n",
    "running the next cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "item_selector1.selected_value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "There are a number of options available which can be found in the widget's documentation. In this example, the widget\n",
    " will be shown with:\n",
    "  - search fields of name, type, a datasource dropdown and a selector to include archived items\n",
    "  - type options limited to 'Signal', 'Condition', and 'Scalar'\n",
    "  - a default type of 'Condition\n",
    "  - a collapsible help window\n",
    "  - a results window 15 rows long\n",
    "  - a maximum of 200 results displayed\n",
    "  - the ability to select multiple items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0379ec803ec440ed87d812308cd3bb1e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "SeeqItemSelect(children=(HTML(value='', layout=Layout(display='none')), GridBox(children=(Accordion(children=(…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "item_selector2 = spy.widgets.SeeqItemSelect(\n",
    "    show_fields=['Name', 'Type', 'Datasource Dropdown', 'Archived'],\n",
    "    type_options=['Signal', 'Condition', 'Scalar'],\n",
    "    item_type='Condition',\n",
    "    show_help=True,\n",
    "    results_box_rows=15,\n",
    "    max_displayed_results=200,\n",
    "    mulit_selct=True\n",
    ")\n",
    "display(item_selector2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Logging Widget and WidgetLogHandler\n",
    "\n",
    "This tutorial shows how to use the `spy.widgets.LogWindowWidget` with the `spy.widgets.WidgetLogHandler`. Keep in mind that the `WidgetLogHandler` can be used with any ipywidget that has a `value` property, so you are not required to use the `LogWindowWidget` to display your logs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setting up the widget and handler\n",
    "\n",
    "First, create the logging widget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "log_widget = spy.widgets.LogWindowWidget('<b>Log Output</b>')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then, create a log handler and configure it's behavior"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "widget_log_handler = spy.widgets.WidgetLogHandler()\n",
    "widget_log_handler.set_widget(log_widget.message_window)\n",
    "widget_log_handler.setLevel(log_widget.log_level)\n",
    "formatter = logging.Formatter('[%(levelname)s] %(asctime)s - %(message)s')\n",
    "widget_log_handler.setFormatter(formatter)\n",
    "widget_log_handler.concat='prepend'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To make the `widget_log_handler` handling level change when the widget level changes, we need to register the handler with the logging widget."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "log_widget.handlers.append(widget_log_handler)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll display the widget here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1bba0f74a72a40e9b2abad7e1fb445d1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "LogWindowWidget(children=(HBox(children=(HTML(value='<b>Log Output</b>', layout=Layout(width='auto')), Dropdow…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "log_widget"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, get the logger that you want to use and set it's log level and register the `WidgetLogHandler` as one of its handlers.  Remember Python loggers have a variety of ways that levels are set, but generally you set a low level for the logger, then higher levels for your handlers.\n",
    "\n",
    "I'm going to use the root logger.  This likely isn't what you want as it will also handle jupyter logs.  If you get a higher level logger, remember that it will inherit it's log level from the root logger (or any logger below it), so if you are using multiple loggers, it's good practice to set the root logger to NOTSET or DEBUG since the default is WARNING.  Also, if you register the handler with multiple loggers that see the same message (loggers have inheritance!) you'll see your message multiple times in the window."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "logger = logging.getLogger()\n",
    "logger.setLevel(logging.NOTSET)\n",
    "# add the handler\n",
    "logger.addHandler(widget_log_handler)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# log some messages!\n",
    "# You can change the widget log level and see how it's behaviour changes when you re-run this cell.\n",
    "logger.info('An info message')\n",
    "logger.warning('A warning!')\n",
    "logger.error('Uh oh...an ERROR!!!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "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.7.3"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
