async def _handle_message(self, message, batch=False): action = message.pop("action") if action == "split": self.viewer.split(**message) elif action == "collapse": self.viewer.focused.toggle_collapse(**message) self.viewer.events.send_redraw() elif action == "add": descriptor = Descriptor(**message, scroll_down=True) handle = self.viewer.add(descriptor) if descriptor.wait: if batch: return handle else: await handle elif action == "save": self.viewer.events.send_save() elif action == "load": await self.viewer.load(**message) elif action == "quit": self.viewer.events.send_quit() elif action == "batch": handles = [] for action in message["actions"]: handle = self._handle_message(action) if handle: handles.append(handle) if handles: await asyncio.gather(*handles)
def add(self, obj, title=None, box_height=None, thread_safe=False): descriptor = Descriptor(obj=obj, title=title, box_height=box_height, scroll_down=self.viewer is not None) self.descriptors.append(descriptor) if self.viewer: self.viewer.add(descriptor, thread_safe=thread_safe)
def split(self, title, box_height, stream_id): self.descriptors_queue.put_nowait( DescriptorQueueItem( descriptor=Descriptor(SPLIT, title, box_height, stream_id=stream_id), redraw=True, num_boxes=None, ))
async def load(self, export_dir): viewer = self.viewer async with aiofiles.open(os.path.join(export_dir, "metadata.json")) as f: metadata = json.loads(await f.read()) viewer.initial_add([ Descriptor( obj=f"file://{export_dir}/{box['filename']}", title=C.from_dict(box["title"]) if isinstance( box["title"], dict) else box["title"], box_height=box["box_height"], collapsed=box["collapsed"], wrap=box["wrap"], ) for box in metadata["boxes"] ]) view = metadata["view"] viewer.maximized = view["maximized"] viewer.collaped_all = view["collapsed"] viewer.wrapped_all = view["wrapped"]