[docs]classNotebookScreenshot:"""Display napari screenshot in the jupyter notebook. Functions returning an object with a _repr_png_() method will displayed as a rich image in the jupyter notebook. https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html Parameters ---------- viewer : napari.Viewer The napari viewer. canvas_only : bool, optional If True includes the napari viewer frame in the screenshot, otherwise just includes the canvas. By default, True. Examples -------- >>> import napari >>> from napari.utils import nbscreenshot >>> from skimage.data import chelsea >>> viewer = napari.view_image(chelsea(), name='chelsea-the-cat') >>> nbscreenshot(viewer) # screenshot just the canvas with the napari viewer framing it >>> nbscreenshot(viewer, canvas_only=False) """def__init__(self,viewer,*,canvas_only=False,alt_text=None,)->None:"""Initialize screenshot object. Parameters ---------- viewer : napari.Viewer The napari viewer canvas_only : bool, optional If False include the napari viewer frame in the screenshot, and if True then take screenshot of just the image display canvas. By default, False. alt_text : str, optional Image description alternative text, for screenreader accessibility. Good alt-text describes the image and any text within the image in no more than three short, complete sentences. """self.viewer=viewerself.canvas_only=canvas_onlyself.image=Noneself.alt_text=self._clean_alt_text(alt_text)def_clean_alt_text(self,alt_text):"""Clean user input to prevent script injection."""ifalt_textisnotNone:iflxml_unavailable:warn('The lxml_html_clean library is not installed, and is ''required to sanitize alt text for napari screenshots. ''Alt Text will be stripped altogether.')returnNone# cleaner won't recognize escaped script tags, so always unescape# to be safealt_text=html.unescape(str(alt_text))cleaner=Cleaner()try:doc=document_fromstring(alt_text)alt_text=cleaner.clean_html(doc).text_content()exceptParserError:warn('The provided alt text does not constitute valid html, so it was discarded.',stacklevel=3,)alt_text=''ifalt_text=='':alt_text=Nonereturnalt_textdef_repr_png_(self):"""PNG representation of the viewer object for IPython. Returns ------- In memory binary stream containing PNG screenshot image. """fromnapari._qt.qt_event_loopimportget_qappget_qapp().processEvents()self.image=self.viewer.screenshot(canvas_only=self.canvas_only,flash=False)withBytesIO()asfile_obj:imsave_png(file_obj,self.image)file_obj.seek(0)png=file_obj.read()returnpngdef_repr_html_(self):png=self._repr_png_()url='data:image/png;base64,'+base64.b64encode(png).decode('utf-8')_alt=html.escape(self.alt_text)ifself.alt_textisnotNoneelse''returnf'<img src="{url}" alt="{_alt}"></img>'