Quick template/example of setting up a python GUI app using Flask, and flaskwebgui
Part of my exploration in creating GUI applications with python, to know more check out my blog
Clone this repo and set up a virtual environment to install required modules
git clone https://github.com/Jasperabez/PythonGUI-Flask.git
cd PythonGUI-Flask
virtualenv .venv
pip install -r requirements.txt
Start the application by running run.py
, which would look something like this:
Contains the flask code (templates, views, models). Tried to follow closely to the layout highlighted by the flask docs
separate the additional "backend" processing you need to do from the GUI, currently a dummy function is being utilized by the flask code. It also contains a FTP + API function for my usecase.
Feel free to change the name however you would like for your own projects.
Optional step to freeze your app as a executable, primarily for distribution
In RecordingUploaderGUI\__init__.py
this section of code (existing) ensures templates, static folder is directed properly when frozen
if getattr(sys, 'frozen', False):
base_dir = os.path.join(sys._MEIPASS)
app = Flask(__name__,
static_folder=os.path.join(base_dir, 'static'),
template_folder=os.path.join(base_dir, 'templates'))
else:
app = Flask(__name__)
When freezing using pyintaller, use flag --add-data
for any templates and static folder.
pyinstaller --add-data "RecordingUploaderGUI/templates:templates" run.py
- https://elc.github.io/posts/executable-flask-pyinstaller/ for
--add-data
flag - Stackoverflow for correct folder redirection in
__init__.py