DEV Community

Cover image for How to run a Flask Linux-only app on windows
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer

Posted on • Originally published at pythonkitchen.com

How to run a Flask Linux-only app on windows

There are in the Python world many Flask Linux-only apps. However in many case, with some twerking we can make them work on Windows. We'll take the case of the AFPy site.

First fork the AFPy site

Then clone it

git clone https://github.com/<your-username>/site.git
Enter fullscreen mode Exit fullscreen mode

The makefile looks like this

VENV = $(PWD)/.env
PIP = $(VENV)/bin/pip
PYTHON = $(VENV)/bin/python
FLASK = $(VENV)/bin/flask
ISORT = $(VENV)/bin/isort
BLACK = $(VENV)/bin/black

all: install serve

install:
    test -d $(VENV) || python3 -m venv $(VENV)
    $(PIP) install --upgrade --no-cache pip setuptools -e .[test]

clean:
    rm -fr dist
    rm -fr $(VENV)
    rm -fr *.egg-info

check-outdated:
    $(PIP) list --outdated --format=columns

test:
    $(PYTHON) -m pytest tests.py afpy.py --flake8 --isort --cov=afpy --cov=tests --cov-report=term-missing

serve:
    env FLASK_APP=afpy.py FLASK_ENV=development $(FLASK) run

isort:
    $(ISORT) -rc .isort.cfg afpy.py tests.py

black:
    $(VENV)/bin/black afpy.py tests.py

.PHONY: all install clean check-outdated test serve isort black

Enter fullscreen mode Exit fullscreen mode

We also do not see any requirements.txt but see them listed in setup.py

    install_requires=[
        'Flask',
        'Flask-Caching',
        'libsass',
        'docutils',
        'feedparser',
        'python-dateutil',
        'itsdangerous',
    ],
Enter fullscreen mode Exit fullscreen mode

We need to install the package to get the setup working

First setup a virtual environment. If you checked the .gitignore you will see that it is not the usual github one. You will need to add venv/ to it. See reapplying gitignore. Let's name our virtual environment venv

python -m venv venv
Enter fullscreen mode Exit fullscreen mode

Then we install the package

pip install -e .
Enter fullscreen mode Exit fullscreen mode

Flask apps use flask run to run. But first some variables must be set. From the makefile:

serve:
    env FLASK_APP=afpy.py FLASK_ENV=development $(FLASK) run
Enter fullscreen mode Exit fullscreen mode

Translated to batch

set FLASK_APP=afpy.py
set FLASK_ENV=development
Enter fullscreen mode Exit fullscreen mode

On running you can see a locale error. You can comment it out and rerun.

The AFPy promotes Python in the French world and is a combination of many organising bodies

Note of caution: recomment out the locale line before comitting!

Testing

For testing you will need to run black, flake8, tests.py

Top comments (2)

Collapse
 
gravesli profile image
gravesli

I think you are great! I built a display machine state using Python3 with Flask!
Flask State Github๏ผšgithub.com/yoobool/flask-state .
Should i can get some improvement suggestions from you? Thanks~

Collapse
 
abdurrahmaanj profile image
Abdur-Rahmaan Janhangeer

XD found i already starred the project sometimes ago! Will do!