View notebook on Github Download

Push exampleΒΆ

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.

[ ]:
import numpy as np
from jupyter_rfb import RemoteFrameBuffer

class FramePusher(RemoteFrameBuffer):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._queue = []

    def push_frame(self, frame):
        self._queue.append(frame)
        self._queue[:-10] = [] # drop older frames if len > 10
        self.request_draw()

    def get_frame(self):
        if not self._queue:
            return
        self.request_draw()
        return self._queue.pop(0)

w = FramePusher(css_width='100px', css_height='100px')
w
[ ]:
w.push_frame(np.random.uniform(0, 255, (100, 100)).astype(np.uint8))
[ ]:
# Push 20 frames. Note that only the latest 10 will be shown
for i in range(20):
    w.push_frame(np.random.uniform(0, 255, (100, 100)).astype(np.uint8))
len(w._queue)
[ ]: