Verified Commit 901dccef authored by Bruno Petrus's avatar Bruno Petrus
Browse files

feat: Attaching with IPython event loop.

The app now checks if it is already running inside an QApplication instance. In which case it does not start the `exec_` function. That means that IPython can attach to Qt's event loop, hence creating the main application does not block the interactive shell.
parent 9f74ce05
Loading
Loading
Loading
Loading
+41 −14
Original line number Diff line number Diff line
@@ -179,18 +179,48 @@ def print_help(name: str) -> None:
          f"Filepath specifies path to an image file. It is optional.\n\n")


def run_app(input_: Union[str, ArrayLike], bitdepth: int = -1, mode: str = ""):
    app_ = QtWidgets.QApplication(sys.argv)
    if type(input_) == str:
        window_ = MainWindow(filepath=input_)
    elif isinstance(input_, np.ndarray):
        window_ = MainWindow(data=input_, bitdepth=bitdepth, mode=mode)
def run_app(data: Union[str, ArrayLike], bitdepth: int = -1, mode: str = ""):
    app = QtCore.QCoreApplication.instance()
    app_created = False
    if app is None:
        print("Creating app")
        app = QtWidgets.QApplication(sys.argv)
        app_created = True

    if type(data) == str:
        _window = MainWindow(filepath=data)
    elif isinstance(data, np.ndarray):
        _window = MainWindow(data=data, bitdepth=bitdepth, mode=mode)
    else:
        print("Error: Invalid input data. Cannot run the app.")
        return False
    window_.show()
    app_.exec_()
    return app_
    _window.show()

    if app_created:
        app.exec_()
    return _window


# Inspired by:
# https://cyrille.rossant.net/making-pyqt4-pyside-and-ipython-work-together/
def create_window(filepath: str = None):
    app_created = False
    app = QtCore.QCoreApplication.instance()
    if app is None:
        print("Creating app")
        app = QtWidgets.QApplication(sys.argv)
        app_created = True
    app.references = set()
    if filepath is not None:
        _window = MainWindow(filepath=filepath)
    else:
        _window = MainWindow()
    app.references.add(_window)
    _window.show()

    if app_created:
        app.exec_()
    return _window


if __name__ == "__main__":
@@ -199,14 +229,11 @@ if __name__ == "__main__":
        print_help(sys.argv[0])
        exit(1)

    app = QtWidgets.QApplication(sys.argv)
    if len(sys.argv) == 2:
        if type(sys.argv[1]) == str and sys.argv[1] == "--help":
            print_help(sys.argv[0])
            exit(0)

        window = MainWindow(filepath=sys.argv[1])
        window = create_window(filepath=sys.argv[1])
    else:
        window = MainWindow()
    window.show()
    app.exec_()
        window = create_window()
+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ import app

def main() -> None:
    # TODO: Make some cool visualisation using sin/cos etc.
    app.run_app(input_=np.zeros((1, 20, 20)), bitdepth=8, mode="grayscale")
    app.run_app(data=np.zeros((1, 20, 20)), bitdepth=8, mode="grayscale")


if __name__ == "__main__":