{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import panel as pn\n",
    "import hvplot.pandas\n",
    "\n",
    "from sklearn.cluster import KMeans\n",
    "from bokeh.sampledata import iris\n",
    "pn.extension(sizing_mode=\"stretch_width\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This app provides an example of building a simple dashboard using Panel. It demonstrates how to take the output of  k-means clustering on the Iris dataset (performed using scikit-learn), parameterizing the number of clusters and the x and y variables to plot. The entire clustering and plotting pipeline is expressed as a single reactive function that returns a plot that responsively updates when one of the widgets changes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flowers = iris.flowers.copy()\n",
    "cols = list(flowers.columns)[:-1]\n",
    "\n",
    "x = pn.widgets.Select(name='x', options=cols)\n",
    "y = pn.widgets.Select(name='y', options=cols, value='sepal_width')\n",
    "n_clusters = pn.widgets.IntSlider(name='n_clusters', start=1, end=5, value=3)\n",
    "\n",
    "@pn.depends(x.param.value, y.param.value, n_clusters.param.value)\n",
    "def get_clusters(x, y, n_clusters):\n",
    "    kmeans = KMeans(n_clusters=n_clusters)\n",
    "    est = kmeans.fit(iris.flowers.iloc[:, :-1].values)\n",
    "    flowers['labels'] = est.labels_.astype('str')\n",
    "    centers = flowers.groupby('labels').mean()\n",
    "    return (flowers.sort_values('labels').hvplot.scatter(x, y, c='labels', size=100, height=500) *\n",
    "            centers.hvplot.scatter(x, y, marker='x', color='black', size=400,\n",
    "                                   padding=0.1, line_width=5))\n",
    "\n",
    "pn.Column(\n",
    "    '# Iris K-Means Clustering',\n",
    "    pn.Row(pn.WidgetBox(x, y, n_clusters), get_clusters)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## App\n",
    "\n",
    "Lets wrap it into nice template that can be served via `panel serve iris_kmeans.ipynb`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pn.template.FastListTemplate(\n",
    "    site=\"Panel\", title=\"Iris Kmeans\", \n",
    "    sidebar=[x, y, n_clusters],\n",
    "    main=[\n",
    "        \"This app provides an example of **building a simple dashboard using Panel**.\\n\\nIt demonstrates how to take the output of **k-means clustering on the Iris dataset** using scikit-learn, parameterizing the number of clusters and the variables to plot.\\n\\nThe entire clustering and plotting pipeline is expressed as a **single reactive function** that responsively returns an updated plot when one of the widgets changes.\\n\\n The **`x` marks the center** of the cluster.\"\"\",\n",
    "        get_clusters\n",
    "        ]\n",
    ").servable();"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
