Building a Python Desktop Application

This post shows how to create a Python desktop application and executable so users who do not have Python dependencies installed can use the application. The purpose of the application is to assist researchers in creating a metadata file. The metadata file describes the content of a data file from one of the most widely used statistical software applications (i.e., Stata, SAS, and SPSS). The metadata file is a simple text-based format that has been defined using Extended Backus–Naur form (EBNF).

The statistical software applications allow users to embed metadata. Nevertheless, the user must extract the metadata by executing a program in the statistical software application of choice. The program produces four output files with structured metadata according to the EBNF.

The embedded metadata does not include information about the data file itself. As a result, another application must collect the data file information, write it to file, and merge that file and the output files from the statistical software application into one metadata file.

Python Desktop Application: Requirements

Given the background described above, the Python desktop application requirements are specified in terms of features. The first requirement is to show a simple GUI with the following features:

  • The GUI must have a menu, a text view, and four buttons
  • The menu must have an option to exit the application
  • Each button must activate a new step in the process flow
  • The outcome of each step must be described and added to the text view

Tkinter GUI example:

Step 1: Select Data File

This step allows the user to select a data file:

  • Display an “open file dialog box” to select a file
  • Validate the file name and extension, and store the path to the file, the file name, and the file extension
  • In addition, do the following for SAS data
    • Check if a corresponding catalog file exists and if no, ask the user whether the catalog file is intentionally missing (yes/no)
    • If yes, accept the data file and if no, send instructions to the text view
  • Finally, send a status message to the text view when the data file is accepted

Step 2: Create Syntax

Create a syntax file in the statistical programming language of the selected data file. The syntax files have already been created:

  • Do the following for SAS data
    • Write SAS syntax to file
    • In addition, use another version of the SAS syntax if a corresponding catalog file exists
  • Do the following for Stata/SPSS data
    • Write Stata/SPSS syntax to file
  • Do the following before writing the syntax to file
    • Update the directory separator, the path to the data file, and the selected data file name
    • Use UTF-8 character encoding without byte order mark for SAS and Stata data
    • Use UTF-8 character encoding with byte order mark for SPSS data
  • Finally, send a status message to the text view

Instruct the user to open the statistical software application and execute the syntax without error in order to produce the requested output files.

Step 3: Add Data File Info

This step collects information about the data file:

  • Open a form with fields for data file name, data file description, and key variable(s)
  • Include “Done” and “Quit” buttons below the fields
  • Use the file name from step 1 as the default data file name
  • Use validation and invalid methods for data file name and key variable(s)
  • Accept empty strings as valid values
  • Validate the entry when “focus out” and when pressing the “Done” button

When the user presses the “Done” button:

  • Validate the entry text
  • Ask the user to add one or more data file references (yes/no)
  • Format all entries to fit the metadata file format
    • Write the formatted entries to file
    • Use UTF-8 character encoding without byte order mark
  • Finally, send a status message to the text view

If the user answered yes to add one or more data file references:

  • Open a form with fields for data file name, variable(s) other data file, and variable(s) this data file
  • Include “Add” and “Quit” buttons below the fields
  • Use validation and invalid methods for all entries
  • In addition, the user must fill out all fields
  • Validate the entry when “focus out” and when pressing the “Add” button
  • When the user presses the “Add” button
    • Store the data file reference
    • Ask the user to add a new data file reference (yes/no)
    • If yes, allow the user to add another data file reference (clear the entry form)

Both forms must show tooltips when the user hovers over the field:

Step 4: Create Output

The final step must combine up to five input files, namely four files with structured metadata from the statistical software application and one file with data file information from the previous step:

  • Validate the character encoding of the input files
    • Use UTF-8 character encoding without byte order mark
  • Append the input files in a sequential order
    • Append empty content, if the input file is missing
  • Write the combined metadata to file
    • Use UTF-8 character encoding without byte order mark
  • Finally, send a status message to the text view

Python Desktop Application: Framework

The entire application is written in Python 3.5 using the standard Python interface to the Tk GUI toolkit also known as Tkinter (Tcl 8.6). The project is managed with PyCharm and the executable file is made with PyInstaller 3.2. PyInstaller turns the Python modules into a true executable—known as frozen binaries in the Python world. Frozen binaries bundle together the byte code of source files, along with the Python interpreter and any support files the program needs, into a single package.

To create the executable file, simply add a temporary copy of a spec file to the package root directory, and execute the following shell command (Bash example shown here):

python3 pyinstaller_root_directory/pyinstaller.py package_root_directory/file.spec

The spec file content is:

a = Analysis(['main_application.py'],
	pathex = ['/home/user/raconverter'],
	hiddenimports = [],
	hookspath = None,
	runtime_hooks=None)
a.datas += [('config.ini', '/home/user/raconverter/config.ini', '.')]
pyz = PYZ(a.pure)
exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      name='RaConverter',
      debug=False,
      strip=None,
      upx=False,
      console=False)

The argument “a.datas” is a list with non-Python files to include in the executable file. The path to these files is relative to the package directory. However, the executable file creates a temporary directory with all files. Thus, to access the configuration file in unpacked as well as packed-mode, use the path returned by resource_path() in config.py.

Python Desktop Application: Source Code

The Python desktop application is organized into a package. The name of the package root directory is “raconverter” and it consists of 12 modules. Click on the module link below to see the source code:

The package directory also contains a configuration file (config.ini).

The code adheres to basic standards in Python (for example, read python-course.eu and pyguide). Please feel free to look at the source code.

Spread the love

Leave a Reply

Your email address will not be published.