{
 "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",
    "Discover more on using widgets to add interactivity to your applications in the [how-to guides on interactivity](../how_to/interactivity/index.md). Alternatively, learn [how to set up callbacks and (JS-)links between parameters](../../how_to/links/index.md) or [how to use them as part of declarative UIs with Param](../../how_to/param/index.html).\n",
    "\n",
    "#### Parameters:\n",
    "\n",
    "For details on other options for customizing the component see the [layout](../../how_to/layout/index.md) and [styling](../../how_to/styling/index.md) how-to guides.\n",
    "\n",
    "* **``format``** (str): Format of the captured images, either 'png' (default) or 'jpeg'. Choose `jpeg` if you want the `VideoStream` to take high frequent snapshots as the image size is much smaller.\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
}
