Skip to main content

Python module to automatically compile UI and RC files in PyQt5 to Python files

Project description

CI PyPI - Python Version PyPI PyPI - License

PyQt5 Auto Compiler (pyqt5ac)

pyqt5ac is a Python package for automatically compiling Qt's UI and QRC files into Python files.

In PyQt5, Qt Designer is the application used to create a GUI using a drag-and-drop interface. This interface is stored in a .ui file and any resources such as images or icons are stored in a .qrc file.

These two filetypes must be compiled into Python files before they can be used in your Python program. There are a few ways to go about this currently:

  1. Manually compile the files using the command line and pyuic5 for .ui files and pyrcc5 for .qrc files.
  2. Compile the files each time the application is started up by calling pyuic5 and pyrcc5 within your Python script

The downside to the first method is that it can be a tedious endeavor to compile the files, especially when one is faced with a larger project with many of these files that need to be compiled. Although the second method eliminates the tediousness of compilation, these files are compiled every time you run your script, regardless of if anything has been changed. This can cause a hit in performance and take longer to startup your script.

Enter pyqt5ac!

pyqt5ac provides a command-line interface (CLI) that searches through your files and automatically compiles any .ui or .qrc files. In addition, pyqt5ac can be called from your Python script. In both instances, ui and resource files are only compiled if they have been updated.

Installing

pyqt5ac is currently available on PyPi. The simplest way to install alone is using pip at a command line

pip install pyqt5ac

which installs the latest release. To install the latest code from the repository (usually stable, but may have undocumented changes or bugs)

pip install git+https://github.com/addisonElliott/pyqt5ac.git

For developers, you can clone the pyqt5ac repository and run the setup.py file. Use the following commands to get a copy from GitHub and install all dependencies

git clone https://github.com/addisonElliott/pyqt5ac.git
cd pyqt5ac
pip install .[dev]

to install in 'develop' or 'editable' mode, where changes can be made to the local working code and Python will use the updated code.

Getting Started

Running from Command Line

If pyqt5ac is installed via pip, the command line interface can be called like any Unix based program in the terminal

pyqt5ac [OPTIONS] [IOPATHS]...

In the interface, the options have slightly different names so reference the help file of the interface for more information. The largest difference is that the ioPaths argument is instead a list of space delineated paths where the even items are the source file expression and the odd items are the destination file expression.

The help file of the interface can be run as

pyqt5ac --help

Running from Python Script

The following snippet of code below demonstrates how to call pyqt5ac from your Python script

import pyqt5ac

pyqt5ac.main(rccOptions='', uicOptions='--from-imports', force=False, initPackage=True, config='',
             ioPaths=[['gui/*.ui', 'generated/%%FILENAME%%_ui.py'],
                     ['resources/*.qrc', 'generated/%%FILENAME%%_rc.py']])

Configuration Options

All of the options that can be specified to pyqt5ac can also be placed in a configuration file (JSON or YAML). My recommendation is to use a configuration file to allow easy compilation of your software. For testing purposes, I would use the options in the command line interface to make get everything working and then transcribe that into a configuration file for repeated use.

Whether running via the command line or from a script, the arguments and options that can be given are the same. The valid options are:

  • rccOptions - Additional options to pass to the resource compiler. See the man page of pyrcc5 for more information on options. An example of a valid option would be "-compress 1". Default is to pass no options.
  • uicOptions - Additional options to pass to the UI compiler. See the man page of pyuic5 for more information on options. An example of a valid option would be '--from-imports'. Default is to pass no options.
  • force - Specifies whether to force compile all of the files found. The default is false meaning only outdated files will be compiled.
  • config - JSON or YAML configuration file that contains information about these parameters.
  • ioPaths - This is a 2D list containing information about what source files to compile and where to place the source files. The first column is the source file global expression (meaning you can use wildcards, ** for recursive folder search, ? for options, etc to match filenames) and the second column is the destination file expression. The destination file expression recognizes 'special' variables that will be replaced with information from the source filename:
    • %%FILENAME%% - Filename of the source file without the extension
    • %%EXT%% - Extension excluding the period of the file (e.g. ui or qrc)
    • %%DIRNAME%% - Directory of the source file
  • variables - custom variables that can be used in the definition of the paths in ioPaths. For example, to limit the search of files to a specific directory, one can define a variable BASEDIR and then use it as %%BASEDIR%%/gui/*.ui*
  • init_package - If specified, an empty __init__.py file is also generated in every output directory if missing. Does not overwrite existing __init__.py. Default value is True.

Example

Take the following file structure as an example project where any UI and QRC files need to be compiled. Assume that pyuic5 and pyrcc5 are located in /usr/bin and that '--from-imports' is desired for the UIC compiler.

|-- gui
|   |-- mainWindow.ui
|   |-- addDataDialog.ui
|   `-- saveDataDialog.ui
|-- resources
|   |-- images
|   |-- stylesheets
|   |-- app.qrc
|   `-- style.qrc
|-- modules
|   |-- welcome
|   |   |-- module.ui
|   |   `-- resources
|   |       |-- images
|   |       `-- module.qrc
|   `-- dataProbe
|       |-- module.ui
|       `-- resources
|           |-- images
|           `-- module.qrc

The sections below demonstrate how to setup pyqt5ac to compile the necessary files given the file structure above.

Option 1: YAML Config File (Recommended)

ioPaths:
  -
    - "gui/*.ui"
    - "generated/%%FILENAME%%_ui.py"
  -
    - "resources/*.qrc"
    - "generated/%%FILENAME_%%%%EXT%%.py"
  -
    - "modules/*/*.ui"
    - "%%DIRNAME%%/generated/%%FILENAME%%_ui.py"
  -
    - "modules/*/resources/*.qrc"
    - "%%DIRNAME%%/generated/%%FILENAME%%_rc.py"

uic_options: --from-imports
init_package: True
force: False

Now run pyqt5ac from the command line or Python script using your config file:

pyqt5ac --config config.yml

or

import pyqt5ac

pyqt5ac.main(config='config.yml')

Option 2: JSON Config File (Deprecated)

{
  "ioPaths": [
    ["gui/*.ui", "generated/%%FILENAME%%_ui.py"],
    ["resources/*.qrc", "generated/%%FILENAME%%_rc.py"],
    ["modules/*/*.ui", "%%DIRNAME%%/generated/%%FILENAME%%_ui.py"],
    ["modules/*/resources/*.qrc", "%%DIRNAME%%/generated/%%FILENAME%%_rc.py"]
  ],
  "rcc_options": "",
  "uic_options": "--from-imports",
  "init_package": true,
  "force": false
}

Now run pyqt5ac from the command line or Python script using your config file:

pyqt5ac --config config.yml

or

import pyqt5ac

pyqt5ac.main(config='config.yml')

Option 3: Python Script

import pyqt5ac

pyqt5ac.main(uicOptions='--from-imports', force=False, initPackage=True, ioPaths=[
        ['gui/*.ui', 'generated/%%FILENAME%%_ui.py'],
        ['resources/*.qrc', 'generated/%%FILENAME%%_rc.py'],
        ['modules/*/*.ui', '%%DIRNAME%%/generated/%%FILENAME%%_ui.py'],
        ['modules/*/resources/*.qrc', '%%DIRNAME%%/generated/%%FILENAME%%_rc.py']
    ])

Option 4: Command Line

pyqt5ac --uic_options "--from-imports" gui/*.ui generated/%%FILENAME%%_ui.py resources/*.qrc generated/%%FILENAME%%_rc.py modules/*/*.ui %%DIRNAME%%/generated/%%FILENAME%%_ui.py modules/*/resources/*.qrc %%DIRNAME%%/generated/%%FILENAME%%_rc.py

Resulting File Structure

|-- gui
|   |-- mainWindow.ui
|   |-- addDataDialog.ui
|   `-- saveDataDialog.ui
|-- resources
|   |-- images
|   |-- stylesheets
|   |-- app.qrc
|   `-- style.qrc
|-- generated
|   |-- __init__.py_
|   |-- mainWindow_ui.py
|   |-- addDataDialog_ui.py
|   |-- saveDataDialog_ui.py
|   |-- app_rc.py
|   `-- style_rc.py
|-- modules
|   |-- welcome
|   |   |-- module.ui
|   |   |-- resources
|   |   |   |-- images
|   |   |   `-- module.qrc
|   |   `-- generated
|   |       |-- module_ui.py
|   |       `-- module_rc.py
|   `-- dataProbe
|       |-- module.ui
|       |-- resources
|       |   |-- images
|       |   `-- module.qrc
|       `-- generated
|           |-- module_ui.py
|           `-- module_rc.py

Support

Issues and pull requests are encouraged!

Bugs can be submitted through the issue tracker.

Pull requests are welcome too!

License

pyqt5ac has an MIT-based license.

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

pyqt5ac-1.2.1.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

pyqt5ac-1.2.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file pyqt5ac-1.2.1.tar.gz.

File metadata

  • Download URL: pyqt5ac-1.2.1.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for pyqt5ac-1.2.1.tar.gz
Algorithm Hash digest
SHA256 21a2b629d8e29f13d17cb76cb8d6b3c0cade3edfe3490e35e9da83d10ed8f374
MD5 1758e37a81c50f4e9c47a210e35b5254
BLAKE2b-256 5d5ae73da02df574053fd8c7a67041a69bc7371af583d6db587b2a0c42373b34

See more details on using hashes here.

File details

Details for the file pyqt5ac-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: pyqt5ac-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for pyqt5ac-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 26743d24d744c45ea6c6d031c006ef5bd386ef3da0cea18bf3f19f216d8b8e57
MD5 00d3d9893533d385f0d48d53e25715d4
BLAKE2b-256 dec77d90f616603a5483a12a85663dfcfb90bac6a512eb5f9d922b420ee11e88

See more details on using hashes here.

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