{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![View notebook](https://img.shields.io/static/v1?label=render%20on&logo=github&color=87ce3e&message=GitHub)](https://github.com/open-atmos/PyPartMC/blob/main/examples/widgets_playground.ipynb)   \n",
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/open-atmos/PyPartMC/blob/main/examples/widgets_playground.ipynb)    \n",
    "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/open-atmos/PyPartMC.git/main?urlpath=lab/tree/examples/widgets_playground.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: You are using pip version 21.2.3; however, version 22.1.2 is available.\n",
      "You should consider upgrading via the '/usr/local/bin/python3 -m pip install --upgrade pip' command.\n",
      "WARNING: You are using pip version 21.2.3; however, version 22.1.2 is available.\n",
      "You should consider upgrading via the '/usr/local/bin/python3 -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import subprocess\n",
    "\n",
    "if 'google.colab' in sys.modules:\n",
    "    packages = ('PyPartMC',)\n",
    "else:\n",
    "    packages = ()\n",
    "\n",
    "for package in packages:\n",
    "    subprocess.check_call([\n",
    "        sys.executable, \"-m\", \"pip\", \"install\", \"--quiet\", package\n",
    "    ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipywidgets import Tab, SelectMultiple, IntSlider, FloatSlider, HBox, VBox, Output, Button\n",
    "from matplotlib import pyplot\n",
    "from IPython.display import display, clear_output\n",
    "import PyPartMC as ppmc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "gas_data_widget = SelectMultiple(options=(\"H2SO4\", \"HNO3\", \"HCl\", \"NH3\", \"NO\", \"NO2\"))\n",
    "gas_data_widget.value = gas_data_widget.options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "humidity_widget = FloatSlider(description='RH [%]', min=90, max=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_x_widget = IntSlider(description='x [1]', min=1, max=9, value=2)\n",
    "param_y_widget = IntSlider(description='y [1]', min=0, max=3, value=2)\n",
    "button = Button(description= 'Run')\n",
    "output = Output()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "preview_output = Output()\n",
    "with preview_output:\n",
    "    fig, ax = pyplot.subplots(1, 1)\n",
    "    ax.set_xlim(param_x_widget.min, param_x_widget.max)\n",
    "    ax.set_ylim(param_y_widget.min, param_y_widget.max)\n",
    "    line_x = ax.plot([param_x_widget.value]*2, ax.get_ylim())\n",
    "    line_y = ax.plot(ax.get_xlim(), [param_y_widget.value]*2)\n",
    "    pyplot.show()\n",
    "    \n",
    "def plot_update():\n",
    "    with preview_output:\n",
    "        clear_output(wait=True)\n",
    "        display(fig)\n",
    "    \n",
    "param_x_widget.observe(lambda change: line_x[0].set_xdata([change.new]*2) or plot_update(), 'value')\n",
    "param_y_widget.observe(lambda change: line_y[0].set_ydata([change.new]*2) or plot_update(), 'value')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "tabs = Tab(\n",
    "    children=(\n",
    "        gas_data_widget,\n",
    "        humidity_widget,\n",
    "        VBox((\n",
    "            HBox((param_x_widget, param_y_widget)),\n",
    "            preview_output\n",
    "        ))\n",
    "    )\n",
    ")\n",
    "tabs.set_title(0, \"GasData\")\n",
    "tabs.set_title(1, \"Scenario\")\n",
    "tabs.set_title(2, \"AeroState\")\n",
    "gui = VBox((tabs,button,output))\n",
    "def action(_):\n",
    "    with output:\n",
    "        clear_output(wait=True)\n",
    "        print(param_x_widget.value, \",\", param_y_widget.value)\n",
    "        gas_data = ppmc.GasData(gas_data_widget.value)\n",
    "        print(gas_data)\n",
    "\n",
    "button.on_click(action)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "761d1bf26db842a2b8f9c1bbc72234e6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Tab(children=(SelectMultiple(index=(1,), options=('H2SO4', 'HNO3', 'HCl', 'NH3', 'NO', 'NO2'), …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(gui)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.0 64-bit",
   "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.10.0"
  },
  "vscode": {
   "interpreter": {
    "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}