{
 "metadata": {
  "name": "",
  "signature": "sha256:d6c3d375d222873a0f2294238d380c35a2bdb28b9ad6ad14cbc1ddd0ffb276fd"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "import time\n",
      "import datetime\n",
      "\n",
      "import collections\n",
      "\n",
      "from bokeh.plotting import *\n",
      "from bokeh.objects import Glyph"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "*To run these examples you must execute the command `python bokeh-server` in the top-level Bokeh source directory first.*"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "output_notebook(url=\"default\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "TS_MULT_us = 1e6\n",
      "UNIX_EPOCH = datetime.datetime(1970, 1, 1, 0, 0) #offset-naive datetime\n",
      "\n",
      "def int2dt(ts, ts_mult=TS_MULT_us):\n",
      "    \"\"\"Convert timestamp (integer) to datetime\"\"\"\n",
      "    return(datetime.datetime.utcfromtimestamp(float(ts)/ts_mult))\n",
      "        \n",
      "def td2int(td, ts_mult=TS_MULT_us):\n",
      "    \"\"\"Convert timedelta to integer\"\"\"\n",
      "    return(int(td.total_seconds()*ts_mult))\n",
      "        \n",
      "def dt2int(dt, ts_mult=TS_MULT_us):\n",
      "    \"\"\"Convert datetime to integer\"\"\"\n",
      "    delta = dt - UNIX_EPOCH\n",
      "    return(int(delta.total_seconds()*ts_mult))\n",
      "        \n",
      "def int_from_last_sample(dt, td):\n",
      "    return(dt2int(dt) - dt2int(dt) % td2int(td))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "TS_MULT = 1e3\n",
      "td_delay = datetime.timedelta(seconds=0.5)\n",
      "delay_s = td_delay.total_seconds()\n",
      "delay_int = td2int(td_delay, TS_MULT)\n",
      "\n",
      "value = 1000 # initial value\n",
      "N = 100 # number of elements into circular buffer\n",
      "\n",
      "buff = collections.deque([value]*N, maxlen=N)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "t_now = datetime.datetime.utcnow()\n",
      "ts_now = dt2int(t_now, TS_MULT)\n",
      "t = collections.deque(np.arange(ts_now-N*delay_int, ts_now, delay_int), maxlen=N)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "line(list(t), list(buff), color=\"#0000FF\", tools=\"pan,wheel_zoom,box_zoom,reset,previewsave\",\n",
      "    name=\"line_example\", x_axis_type=\"datetime\"\n",
      ")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "UNIX_EPOCH = datetime.datetime(1970, 1, 1, 0, 0) #offset-naive datetime"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "renderer = [r for r in curplot().renderers if isinstance(r, Glyph)][0]\n",
      "ds = renderer.data_source\n",
      "show()\n",
      "while(True):\n",
      "    ts_now = dt2int(datetime.datetime.utcnow(), 1e3)\n",
      "    t.append(ts_now)\n",
      "    ds.data['x'] = list(t)\n",
      "\n",
      "    value += np.random.uniform(-1, 1)\n",
      "    buff.append(value)\n",
      "    ds.data['y'] = list(buff)\n",
      "\n",
      "    ds._dirty = True\n",
      "    cursession().push(*ds.dump(docid=cursession().docid))\n",
      "    \n",
      "    time.sleep(delay_s)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}