5. Packaging - imxade/PictoPyV2 GitHub Wiki

Introduction

Packaging PictoPy as a native application involves several steps to ensure it runs seamlessly on various operating systems. This section will guide you through the process of using pywebview and pyinstaller to create standalone executables, and the usage of build_compose.yaml to build these executables. Additionally, we will cover how to use artifact.yaml to upload the built executables as artifacts.

Tools and Technologies

  • Pywebview: A lightweight wrapper around a webview component to display HTML content in its own window.
  • PyInstaller: A tool to bundle a Python application and all its dependencies into a single package.
  • Docker Compose: A tool for defining and running multi-container Docker applications.

Pywebview

Purpose: Pywebview allows you to create a webview window displaying your HTML content, making it possible to package your web-based Flask application as a native desktop application.

Usage: Pywebview can be integrated into your Flask application to provide a native window for the web interface. Here's a basic example of how to use Pywebview:

import webview
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

if __name__ == '__main__':
    webview.create_window('PictoPy', app)
    webview.start()

PyInstaller

Purpose: PyInstaller bundles a Python application and all its dependencies into a single package, making it easy to distribute and run on any system without needing a Python installation.

Usage: To package your Flask application using PyInstaller, follow these steps:

  1. Install PyInstaller:

    pip install pyinstaller
    
  2. Create Executable:

    pyinstaller --onefile --icon=static/asset/favicon.ico main.py
    
  3. Distribute Executable: The resulting executable will be located in the dist directory and can be run on the respective operating system.

Building Executables with Docker Compose

Using Docker Compose allows you to build executables for different operating systems within isolated environments. The build_compose.yaml file is used for this purpose.

Content of build_compose.yaml:

version: '3.8'

services:
  windows-build:
    image: batonogov/pyinstaller-windows:latest
    volumes:
      - ./:/src

  osx-build:
    image: batonogov/pyinstaller-osx:latest
    volumes:
      - ./:/src

  linux-build:
    image: batonogov/pyinstaller-linux:latest
    volumes:
      - ./:/src

Steps to Build Executables:

  1. Build and start the containers:

    docker-compose -f build_compose.yaml up --build
    
  2. Check for the built executables: The executables will be created in the dist directory within each container.