{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import panel as pn\n",
    "\n",
    "from bokeh.plotting import figure\n",
    "from bokeh.models import ColumnDataSource\n",
    "\n",
    "pn.extension()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = figure(sizing_mode='stretch_width', title='Bokeh streaming example')\n",
    "\n",
    "xs = np.arange(1000)\n",
    "ys = np.random.randn(1000).cumsum()\n",
    "x, y = xs[-1], ys[-1]\n",
    "\n",
    "cds = ColumnDataSource(data={'x': xs, 'y': ys})\n",
    "\n",
    "p.line('x', 'y', source=cds)\n",
    "\n",
    "bk_pane = pn.pane.Bokeh(p)\n",
    "bk_pane.servable()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def stream():\n",
    "    global x, y\n",
    "    x += 1\n",
    "    y += np.random.randn()\n",
    "    cds.stream({'x': [x], 'y': [y]})\n",
    "    pn.io.push_notebook(bk_pane) # Only needed when running in notebook context\n",
    "    \n",
    "pn.state.add_periodic_callback(stream, 100)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
