florian test
This is a simple python flask application to host a simple web app.
After starting the flask application you can access following URL's on the server:
Function | URL |
---|---|
index | http://<server>:<port>/ |
json | http://<server>:<port>/json/ |
hello | http://<server>:<port>/hello/ |
hello <name> | http://<server>:<port>/hello/<name> |
primes 100 | http://<server>:<port>/primes/ |
primes <count> | http://<server>:<port>/primes/<count> |
You can run the application in 2 ways:
Requirements:
- Docker installed
Build the docker image in the project root folder with:
docker build . -t flask-example-cicd:latest
To run the docker image in interactive mode:
docker run --rm -it -p 8080:8080/tcp --name flask-example flask-example-cicd:latest
To run the docker image in detached mode:
docker run --rm -d -p 8080:8080/tcp --name flask-example flask-example-cicd:latest
You can change the environmental variable for flask in the Dockerfile, for example you can change the port by changing ENV FLASK_RUN_PORT=8080
.
Stop the running container:
docker stop flask-example
Requirements:
- python3
- python3-dev
- python3-pip
- gcc (to compile cython)
- musl-dev (to compile cython)
- Install requirements:
apt update
apt install gcc musl-dev python3 python3-dev python3-pip
- Build/Install project:
. build.sh
- Run the server on localhost:8080:
. run.sh
For local development you can use a virtual environment โ How to install virtualenv.
Create a virtual environment:
# linux example
python3 -m venv "venv"
source venv/bin/activate
With deactivate
you can disable the virtual environment.
You can debug the flask app by running following commands:
Linux (Bash)
export FLASK_APP=flaskr.app export FLASK_ENV=development flask run
Windows (CMD)
set FLASK_APP=flaskr.app set FLASK_ENV=development flask run
Windows (PowerShell)
$env:FLASK_APP = "flaskr.app" $env:FLASK_ENV = "development" flask run
When using an external debugger, the app should still be in debug mode, but it an be useful to disable the built-in debugger and reloader, which can interfere.
flask run --no-debugger
flask run --no-reload
flask run --no-debugger --no-reload
Example configuration for VS Code
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "flaskr/app.py",
"FLASK_ENV": "development",
"FLASK_RUN_PORT" : "8080",
"FLASK_DEBUG": "1"
},
"args": [
"run",
"--no-debugger"
],
"jinja": true
}
]
}
To run unit tests:
# install pytest manually or use "reset-dev.sh"
pip3 install pytest
pytest
You can use the script reset-dev.sh to reset your development environment:
. reset-dev.sh
The script will do following:
- (Re)create the virtual environment venv without dependencies installed
- Activating venv in current terminal
- Install development/testing tools in venv
- flake8 (Linting)
- pylint (Linting)
- autopep8 (Formatting)
- pytest (Testing)
- Remove all folders created by build/install
- package info (*egg-info)
- build folder
- dist folder
- pycache folders
- built cython files (*.so, *.pyd)
Now you can build the project manually or with build.sh.