{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import panel as pn\n",
    "pn.extension()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ``VideoStream`` widget displays a video from a local stream (for example from a webcam) and allows accessing the streamed video data from Python.\n",
    "\n",
    "For more information about listening to widget events and laying out widgets refer to the [widgets user guide](../../user_guide/Widgets.ipynb). Alternatively you can learn how to build GUIs by declaring parameters independently of any specific widgets in the [param user guide](../../user_guide/Param.ipynb). To express interactivity entirely using Javascript without the need for a Python server take a look at the [links user guide](../../user_guide/Param.ipynb).\n",
    "\n",
    "#### Parameters:\n",
    "\n",
    "For layout and styling related parameters see the [customization user guide](../../user_guide/Customization.ipynb).\n",
    "\n",
    "* **``format``** (str): Format of the captured images, either 'png' or 'jpeg'\n",
    "* **``paused``** (boolean): Whether the video stream is paused\n",
    "* **``timeout``** (int): Interval between snapshots (if None then snapshot only taken if snapshot method is called)\n",
    "* **``value``** (string): String representation of the current snapshot\n",
    "\n",
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ``VideoStream`` widget by default simply displays the video stream:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "video_stream = pn.widgets.VideoStream(name='Video Stream')\n",
    "video_stream"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To sync the state of the stream with Python we have two options. First, we can call the ``snapshot`` method, which will trigger the ``value`` of the widget to be updated:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "video_stream.snapshot()\n",
    "\n",
    "html = pn.pane.HTML(width=320, height=240)\n",
    "\n",
    "def update(event):\n",
    "    html.object = '<img src=\"'+event.new+'\" width=320 height=240 />'\n",
    "    \n",
    "video_stream.param.watch(update, 'value')\n",
    "\n",
    "html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Alternatively, we can create a video stream with a `timeout` that specifies how frequently the video stream will be updated:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "video = pn.widgets.VideoStream(timeout=100)\n",
    "html = pn.pane.HTML()\n",
    "pause = pn.widgets.Toggle(name='Pause')\n",
    "\n",
    "pause.jslink(video, value='paused')\n",
    "video.jslink(html, code={'value': \"\"\"\n",
    "target.text = `<img src=\"${source.value}\" width=320 height=240 />`\n",
    "\"\"\"})\n",
    "\n",
    "pn.Column(pause, pn.Row(video, html))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lastly, the video stream can also be paused from Python:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "video.paused = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Controls\n",
    "\n",
    "The `VideoStream` widget exposes a number of options which can be changed from both Python and Javascript. Try out the effect of these parameters interactively:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pn.Row(video_stream.controls(jslink=True), video_stream)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
