# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:naparienv] *
#     language: python
#     name: conda-env-naparienv-py
# ---

# +
import time
import napari
from qtpy.QtWidgets import QPushButton
import numpy as np

from napari.qt.threading import thread_worker
def update_layer(new_image):
         try:
             viewer.layers['result'].data = new_image
         except KeyError:
             viewer.add_image(
                 new_image, name='result', contrast_limits=(-0.8, 0.8)
             )

@thread_worker
def yield_random_images_forever():
 i = 0
 while True:  # infinite loop!
     yield np.random.rand(512, 512) * np.cos(i * 0.2)
     i += 1
     time.sleep(0.05)




with napari.gui_qt():
        viewer = napari.Viewer()
        worker = yield_random_images_forever()
        worker.yielded.connect(update_layer)
        worker.start()
        # add a button to the viewew that, when clicked, stops the worker
        button = QPushButton("STOP!")
        button.clicked.connect(worker.quit)
        worker.finished.connect(button.clicked.disconnect)
        viewer.window.add_dock_widget(button)
        
# -



StarcoderdataJupyterScriptsDedupFiltered