{ "cells": [ { "cell_type": "markdown", "id": "f011941f", "metadata": {}, "source": [ "# Push example\n", "The default behavior of `jupyter_rfb` is to automatically call `get_frame()` when a new draw is requested and when the widget is ready for it. In use-cases where you want to push frames to the widget, you may prefer a different approach. Here is an example solution." ] }, { "cell_type": "code", "execution_count": null, "id": "c278a5e8", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from jupyter_rfb import RemoteFrameBuffer\n", "\n", "\n", "class FramePusher(RemoteFrameBuffer):\n", " def __init__(self, **kwargs):\n", " super().__init__(**kwargs)\n", " self._queue = []\n", "\n", " def push_frame(self, frame):\n", " self._queue.append(frame)\n", " self._queue[:-10] = [] # drop older frames if len > 10\n", " self.request_draw()\n", "\n", " def get_frame(self):\n", " if not self._queue:\n", " return\n", " self.request_draw()\n", " return self._queue.pop(0)\n", "\n", "\n", "w = FramePusher(css_width=\"100px\", css_height=\"100px\")\n", "w" ] }, { "cell_type": "code", "execution_count": null, "id": "0bade53a", "metadata": {}, "outputs": [], "source": [ "w.push_frame(np.random.uniform(0, 255, (100, 100)).astype(np.uint8))" ] }, { "cell_type": "code", "execution_count": null, "id": "2156c042", "metadata": {}, "outputs": [], "source": [ "# Push 20 frames. Note that only the latest 10 will be shown\n", "for _ in range(20):\n", " w.push_frame(np.random.uniform(0, 255, (100, 100)).astype(np.uint8))\n", "len(w._queue)" ] }, { "cell_type": "code", "execution_count": null, "id": "1ed1d6a9", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.9" } }, "nbformat": 4, "nbformat_minor": 5 }