Make Cross Platform Desktop Apps with Flask
CREATE YOUR FIRST CROSS PLATFORM DESKTOP APP IN UNDER 15 MINUTES [PYTHON OR HTML]
In today's times, most websites have a standalone application. There are many ways to create desktop apps, C++, Electron, etc.
However, this tutorial is for all of you python lovers.
Prior Knowledge Required -
This tutorial uses Flask. If you don't know flask, don't worry! This tutorial can easily be understood to handle HTML files too!
Packages required -
Make sure you download these PyPi
shudder packages before hand -
pip3 install PyQTWebEngine
pip3 install flask
pip3 install pyinstaller
This is best done locally, as pyinstaller automatically detects OS. If you run on replit, it will build for Ubuntu.
Use this python code in your main.py -
from flask import Flask from PyQt5.QtCore import * from PyQt5.QtWebEngineWidgets import * from PyQt5.QtWidgets import QApplication from threading import Timer import sys # ^^ Start using all the regular flask logic ^^ flask_app = Flask(__name__) # Initiate flask app @flask_app.route("/") #Define what happens on the home page def hello(): #Function can really be named anything return "Hello World"# Define function for QtWebEngine def ui(location): #Initiate PyQT5 app qt_app = QApplication(sys.argv) web = QWebEngineView() web.setWindowTitle("Window Name") #Rename to change your window name. # ^ This cannot change between pages web.resize(900, 800) # Set a size web.setZoomFactor(1.5) # Enlarge your content to fit screen web.load(QUrl(location)) #Load Home page at startup web.show() #Show the window sys.exit(qt_app.exec_()) if __name__ == "__main__": # start sub-thread to open the browser. Timer(1,lambda: ui("http://127.0.0.1:5000/")).start() #Show the home page on startup. Change the URL backend (http://127.0.0.1:5000/cool_backend, etc) flask_app.run(debug = False) #Start flask engine, debug is False so that your users see ` Internal Server Error ` instead of the actual error.
Now when you run this, a window with your flask content comes!
Bundle with PyInstaller
Again, recommended to do this locally.
pip3 install pyinstaller
Open up your Command Prompt, Terminal, etc. And
cd into your apps directory.
App Directory -
Powershell, cmd, etc. Command to use -
PS C:\a_user> cd C:\a_user\myapp PS C:\a_user\myapp>
Note: If your path has spaces, put it in quotes
cd into your directory, run this command -
pyinstaller -n APP NAME main.py
Use these flags to enhance your app - (Put them between
APP NAME and
|Flag||What it does|
|Console Project only|
|Window Project only|
|If your project has other files such as a |
ADD DATA EXAPMLE
;for windows and
:for Unix (mac, linux)
--add-data 'templates;templates' --add-data 'README.md;.'
LINUX AND MAC
--add-data 'templates:templates' --add-data 'README.md:.'
Now, you should see a folder called dist. Open it- there should be one folder, named
YOUR APP NAME. Open that. Finally launch the exe, and voila! You can use this to generate an installer for your app.