Skip to main content

Create desktop application by using Flask and QtWebEngine

Project description

PySideFlask

Create desktop application by using Flask and Pyside2.

This is a fork of PyFladesk, where PyQT5 has been exchanged with PySide2

Idea

Rather than open Flask app in user browser, create a QWebview and then run Flask app on it.

By default, every internal link is open inside the app and every external link is open in the default browser.

Dependencies

  • Python3
  • Flask
  • Pyside2

Installation with pip

If you want to install PySideFlask with pip you just run.

pip install pysideflask

Usage

You just need to change two lines:

Add an import at the top:

from pysideflask import init_gui

And wherever you run the app (app.run) replace it with:

init_gui(app)

Then run your app as usual

Example

from flask import Flask
from pysideflask import init_gui

app = Flask(__name__)

from routes import *

if __name__ == '__main__':
    init_gui(app)

Parameters

The init_gui function has some optional parameters that you may find useful:

init_gui(application, port=5000, width=300, height=400,
             window_title="PySideFlask", icon="appicon.png", argv=None)
  • port: choose in which port the application will run.
  • width: The initial width of the main window.
  • height: The initial height of the main window.
  • window_title: The main window title.
  • icon: the path to the icon file of the main window.
  • argv: additional parameters to the QApplication instance.

Packaging

For a full guide on how to package the app in one executable with PyInstaller file check this blog post

This avoids the need for the Python interpreter and the packages you use inside your project.

If you haven't already, install it with pip (if you use virtual environments you should install it inside it).

pip install pyinstaller

Some parameters to consider:

  • F - Bundles everything in a single file
  • w - Avoid displaying a console
  • --add-data - Add Folders to the directory/executable

Since Flask relies on a directory structure you should pass it to the folder, in the example case we only have two folders: templates and static, in case you use a database or some other directory structure you should adapt this.

Note: For more complex scenarios check the PyInstaller Docs

If we want everything in one executable file we can

Windows:

pyinstaller -w -F --add-data "templates;templates" --add-data "static;static" app.py

Linux:

pyinstaller -w -F --add-data "templates:templates" --add-data "static:static" app.py

Depending on the Linux version, you might need to install sudo apt install libpython3.x-dev

This will create a folder dist with our executable ready to be shipped. The executable will open the main window of our app.

Define this in a helper script:

def resource_path(relative_path):
 """ Get absolute path to resource, works for dev and for PyInstaller """
    base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, relative_path)

Include this at the top, under imports

if getattr(sys, 'frozen', False):
    template_folder = resource_path('templates')
    static_folder = resource_path('static')
    app = Flask(__name__, template_folder=template_folder, static_folder=static_folder)
else:
    app = Flask(__name__)

Since Qt is quite big, your executables will be big too. The example app of this repository is 70 MB (69 MB of which are the Qt Component for displaying HTML). This is reasonable taking into account that we are shipping a self contain web browser. In case size is crucial, you can follow this suggestions

Contributing Oportunities

Feel free to open issues and pull requests for new features and improvements. This is a guide for things that may be useful for the project:

  • Add different backends (wxPython, TKinter, etc)
  • Test performance of HTML5 and CSS3
  • Add Directory structure for large projects (Flask Patterns)
  • Test other micro web frameworks (Bottle, etc)
  • Make sample apps

Thanks

Thanks to ​Saeed Moqadam, for the original Pyfladesk

Project details


Release history Release notifications | RSS feed

This version

2.0

Download files

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

Source Distribution

PySideFlask-2.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

PySideFlask-2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file PySideFlask-2.0.tar.gz.

File metadata

  • Download URL: PySideFlask-2.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.5

File hashes

Hashes for PySideFlask-2.0.tar.gz
Algorithm Hash digest
SHA256 f8a59c75b9da2e138a57c0c6ceb24d38bf1c2c8b20683c859aaf309d460fb8a0
MD5 4a1fbb07acb8067a96ffec9b15dd418e
BLAKE2b-256 46521b64167f73976245be680230194ea916ad25ecad245b86cbd3e55cb3c88c

See more details on using hashes here.

File details

Details for the file PySideFlask-2.0-py3-none-any.whl.

File metadata

  • Download URL: PySideFlask-2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.5

File hashes

Hashes for PySideFlask-2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a5151f179e041470bd06eadd8e6011cd27f8c40c9e16d050bc5b8cf0d5849a7
MD5 e1abe42dd8313984e4d5f17fbc310907
BLAKE2b-256 21365cea255712f3f0e49936f0bfcacf3ff8d5e09b1569a4c5f89c7ea00e0833

See more details on using hashes here.

Supported by

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