Skip to main content

Qt utilities to enable hot-reloading of python/Qt code

Project description

qtreload

Qt utilities to enable hot-reloading of python/Qt code

License PyPI Python Version Test codecov

Have you been using Jupyter Notebook's magic functions such as

%load_ext autoreload
%autoreload 2

where you might have been editing code in VSCode or Pycharm and executing actions in Jupyter Notebook?

Or have you previously used LiClipse which has Debugger Auto-Reload?

Well, qtreload provides similar capabilities by 'hot-reloading' python modules when there are changes to the source code. It operates by generating a list of all possible modules/submodules for a specific project and then using QFileWatcher to observe any changes to these files.

This library should be used when developing Qt code in Python and you are not interested in continually having to restart your application. (See limitations to find out when its still required).

Usage

You can instantiate the QtReloadWidget manually or using the install_hot_reload function.

Note! Make sure to instantiate QApplication before running this code.

Using QtReloadWidget:

from qtreload.qt_reload import QtReloadWidget

# you can specify list of modules that should be monitored
list_of_widgets = ["napari", "spyder", "..."]

widget = QtReloadWidget(list_of_modules)

That's pretty much it. Now every time you make changes to your source code in e.g. napari will be reflected in your interpreter.

Using install_hot_reload requires two environment variabels being set, namely:

QTRELOAD_HOT_RELOAD=1
QTRELOAD_HOT_RELOAD_MODULES="napari, spyder"

Then you can just execute the following:

from qtreload.install import install_hot_reload

install_hot_reload()

When it works like magic

There are countless examples where this approach really well. Some examples:

  • You are running your application where you have method on_run but when you execute this function, you notice that you misspelled some variable. In normal circumstances you would need to restart the application. Now, however, you can correct it in your IDE, save, and try running again.
  • You are running your application and are modifying the layout of a popup window. Now you can do this and each time the dialog is reshown, the new version of the dialog will be shown.

Limitations

While this approach can be extremely useful and can save a lot of time, it has a couple of limitations:

  • code within the ___init__.py cannot be reloaded
  • some changes to GUI code cannot be reloaded - if e.g. you are modying the QMainWindow and just added a new button, this button will now be shown. In order to show it, you will still need to restart the application. If, however, you were modyfing a plugin or a dialog that is shown upon clicking on e.g. menu item, these changes WILL take place.
  • modifying python properties (@setter/@getter) is not always reloaded

Acknowledgements

The hot-reload code is directly copied from the PyDev debugger developed by fabioz with minimal changes to remove any dependencies

See https://github.com/fabioz/PyDev.Debugger/blob/main/_pydevd_bundle/pydevd_reload.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qtreload-0.1.2.tar.gz (22.2 kB view hashes)

Uploaded Source

Built Distribution

qtreload-0.1.2-py3-none-any.whl (12.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page