{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is highly experimental, but we could even imagine make the notebook reactive. This can be achieved by reaching into the ipython internals and overwriting the namespace. This has bugs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from io import BytesIO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from reactpy import Reactive, Interact, Plot, FileData, Output\n",
    "O = Output\n",
    "%matplotlib widget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = reactive = Reactive(lazy_eval=False)\n",
    "r.update(get_ipython().user_ns)\n",
    "get_ipython().user_ns =  r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7d24524706f6457991da4cf4a12df0d3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output(outputs=({'output_type': 'stream', 'name': 'stdout', 'text': '(2, 5)'},))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = 2\n",
    "b = r(lambda a: a+3)\n",
    "o = O(lambda a,b: (a, b))\n",
    "o"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "175966d5d7e34a67a8812d0589655d13",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(IntSlider(value=0, description='value', min=-100), Output()), _dom_classes=('widget-inte…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a15a032d9c654f37a0fcd1b3c0376547",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(IntSlider(value=0, description='value', min=-100), Output()), _dom_classes=('widget-inte…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "100f279a1b1f4f53b148ef6c62351531",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output(outputs=({'output_type': 'stream', 'name': 'stdout', 'text': '(0, 0)'},))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "c = Interact('c', (-100, 100, 1))\n",
    "d = Interact('d', (-100, 100, 1))\n",
    "oo = O(lambda c,d: (c,d))\n",
    "oo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "582f72c2d72a408e9c03740f027f1349",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "p = Plot(lambda ax, c, d: ax.plot([(x+c)*(x+d)*x for x in range(-100, 100)]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([0, 2, 5, 6, 8, 9, 11, 23, 21, 32], 6)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alist = [0, 2, 5, 6, 8, 9, 11, 23, 21, 32]\n",
    "item = 6\n",
    "alist, item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a4bde50dfe5a4c60a5203580c7a32445",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output(outputs=({'output_type': 'stream', 'name': 'stdout', 'text': '(0, 9, False)'},))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "first = 0\n",
    "last = alist.__len__()-1\n",
    "found = False\n",
    "mon = O(lambda  first, last, found: (first, last, found))\n",
    "mon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "while first<=last and not found:\n",
    "    midpoint = (first + last)//2\n",
    "    if alist[midpoint] == item:\n",
    "        found = True\n",
    "    else:\n",
    "        if item < alist[midpoint]:\n",
    "            last = midpoint-1\n",
    "        else:\n",
    "            first = midpoint+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 2\n",
    "y = 3\n",
    "@reactive\n",
    "def z(x, y):\n",
    "    return x+y\n",
    "z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6f31bc38da2349bd914ecd2a34864fbf",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output(outputs=({'output_type': 'stream', 'name': 'stdout', 'text': '8'},))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "@O\n",
    "def o(z):\n",
    "    return z\n",
    "o"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 39"
   ]
  }
 ],
 "metadata": {
  "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.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {
     "063bb22f24904c2ead97050ac5ca3d09": {
      "model_module": "jupyter-matplotlib",
      "model_module_version": "^0.7.4",
      "model_name": "MPLCanvasModel",
      "state": {
       "_cursor": "default",
       "_figure_label": "Figure 1",
       "_height": 480,
       "_message": "x=38.1137      y=-370025     ",
       "_width": 640,
       "layout": "IPY_MODEL_813d602ee53c475a82629732b2da3654",
       "toolbar": "IPY_MODEL_1477a9b1c4554b65874855f612313ff2",
       "toolbar_position": "left"
      }
     },
     "0aa6b2b8f5dd4062a78bb509b69f6410": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "1477a9b1c4554b65874855f612313ff2": {
      "model_module": "jupyter-matplotlib",
      "model_module_version": "^0.7.4",
      "model_name": "ToolbarModel",
      "state": {
       "layout": "IPY_MODEL_6225002f83ef4022b6f39af2f3dac4ae",
       "toolitems": [
        [
         "Home",
         "Reset original view",
         "home",
         "home"
        ],
        [
         "Back",
         "Back to previous view",
         "arrow-left",
         "back"
        ],
        [
         "Forward",
         "Forward to next view",
         "arrow-right",
         "forward"
        ],
        [
         "Pan",
         "Pan axes with left mouse, zoom with right",
         "arrows",
         "pan"
        ],
        [
         "Zoom",
         "Zoom to rectangle",
         "square-o",
         "zoom"
        ],
        [
         "Download",
         "Download plot",
         "floppy-o",
         "save_figure"
        ]
       ]
      }
     },
     "19a0876868e040238e25657dae5ad2bc": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "1fc27225a7d34d1d9ccbec6718a5cf22": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "IntSliderModel",
      "state": {
       "description": "d",
       "layout": "IPY_MODEL_797e5c39773443439943e7173ba26b99",
       "min": -100,
       "style": "IPY_MODEL_2c5c6dfd077046a1bb0dbcdf65164db9"
      }
     },
     "23abd14777b249dfb985085f237193cc": {
      "model_module": "@jupyter-widgets/output",
      "model_module_version": "1.0.0",
      "model_name": "OutputModel",
      "state": {
       "layout": "IPY_MODEL_0aa6b2b8f5dd4062a78bb509b69f6410",
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": "(2, 5)"
        },
        {
         "name": "stdout",
         "output_type": "stream",
         "text": "(3, 6)"
        }
       ]
      }
     },
     "2585fb08c08b4376b92823a2397ae952": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "VBoxModel",
      "state": {
       "_dom_classes": [
        "widget-interact"
       ],
       "children": [
        "IPY_MODEL_2ce08cbdab32471fbeab906a7954d170",
        "IPY_MODEL_b7c5c58243ce4674888fe6581d4e0caf"
       ],
       "layout": "IPY_MODEL_d8d13968e21945e5ac1a1951e4256092"
      }
     },
     "265c7ed5868844629a4972cd99ff3226": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "2812fbd6b3f54af2a9f701c059c8a08d": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "SliderStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "2c5c6dfd077046a1bb0dbcdf65164db9": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "SliderStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "2ce08cbdab32471fbeab906a7954d170": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "IntSliderModel",
      "state": {
       "description": "c",
       "layout": "IPY_MODEL_67057506f1524828867757030ff00308",
       "min": -100,
       "style": "IPY_MODEL_2812fbd6b3f54af2a9f701c059c8a08d"
      }
     },
     "6225002f83ef4022b6f39af2f3dac4ae": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "67057506f1524828867757030ff00308": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "6a4d0c0bbb8144ef8772e1d4c82b5213": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "70e4371be48f4961b95f9ff2dc3bace8": {
      "model_module": "@jupyter-widgets/output",
      "model_module_version": "1.0.0",
      "model_name": "OutputModel",
      "state": {
       "layout": "IPY_MODEL_b0cc4c2ed1ca45f9b42f249993468fbc",
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": "(0, 9, False)"
        },
        {
         "name": "stdout",
         "output_type": "stream",
         "text": "(0, 3, False)"
        },
        {
         "name": "stdout",
         "output_type": "stream",
         "text": "(2, 3, False)"
        },
        {
         "name": "stdout",
         "output_type": "stream",
         "text": "(3, 3, False)"
        },
        {
         "name": "stdout",
         "output_type": "stream",
         "text": "(3, 3, True)"
        }
       ]
      }
     },
     "797e5c39773443439943e7173ba26b99": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "813d602ee53c475a82629732b2da3654": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "858f5725622849b6a9d3c6c4b475e5d1": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "VBoxModel",
      "state": {
       "_dom_classes": [
        "widget-interact"
       ],
       "children": [
        "IPY_MODEL_1fc27225a7d34d1d9ccbec6718a5cf22",
        "IPY_MODEL_cbd8649a405e4aa4a5cb9d4c7b2b61f0"
       ],
       "layout": "IPY_MODEL_939c493d3bbc42c8a7a7c9fbe0f235ac"
      }
     },
     "939c493d3bbc42c8a7a7c9fbe0f235ac": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "b0cc4c2ed1ca45f9b42f249993468fbc": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "b7c5c58243ce4674888fe6581d4e0caf": {
      "model_module": "@jupyter-widgets/output",
      "model_module_version": "1.0.0",
      "model_name": "OutputModel",
      "state": {
       "layout": "IPY_MODEL_6a4d0c0bbb8144ef8772e1d4c82b5213"
      }
     },
     "cbd8649a405e4aa4a5cb9d4c7b2b61f0": {
      "model_module": "@jupyter-widgets/output",
      "model_module_version": "1.0.0",
      "model_name": "OutputModel",
      "state": {
       "layout": "IPY_MODEL_265c7ed5868844629a4972cd99ff3226"
      }
     },
     "d8d13968e21945e5ac1a1951e4256092": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "dc17d3cfc248467baf46c91e916f0a18": {
      "model_module": "@jupyter-widgets/output",
      "model_module_version": "1.0.0",
      "model_name": "OutputModel",
      "state": {
       "layout": "IPY_MODEL_19a0876868e040238e25657dae5ad2bc",
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": "(0, 0)"
        }
       ]
      }
     }
    },
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}