Skip to content

Commit

Permalink
Add examples on usage
Browse files Browse the repository at this point in the history
Moved from the base flask image repository.
  • Loading branch information
frimro committed Feb 9, 2022
1 parent 587074e commit 774f900
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: 'Lint Python code'

on: [push, pull_request]

jobs:
lint-python:
runs-on: ubuntu-latest
strategy:
matrix:
python_version: ['3.6', '3.8', '3.10']
steps:
- name: Setup repository
uses: actions/checkout@v2

- name: Set up python ${{ matrix.python_version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python_version }}

- name: Install essential tools
run: pip install flake8

- name: Run flake8
run: flake8
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*~
*.swp
*.py[co]
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM docker.io/ypcs/flask:latest
FROM ghcr.io/seravo/flask:latest

ARG APT_PROXY

Expand Down
53 changes: 52 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
# flask-template
# Flask template

Flask project template

## Usage

To run hello world app, :8080 provides HTTP endpoint (to uWSGI), and :9000 provides uWSGI endpoint. So, let's try accessing via HTTP:

docker run --rm -it -v $(pwd)/app:/app -e FLASK_APP=hello:app -p 127.0.0.1:8080:8080 ghcr.io/seravo/flask:latest

now try to open http://127.0.0.1:8080/ , you should see familiar greeting.

To publish your own app, mount your app to `/app`, and provide FLASK_APP environment variable with correct value.

Eg.

docker run --rm -it -v $(pwd)/app:/app -e FLASK_APP=app:app -p 127.0.0.1:8080:8080 ghcr.io/seravo/flask:latest

if you had app code like

app/app.py:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def some_view():
return "Hello myapp!"

Usually you shouldn't expose this HTTP endpoint directly to internet, but use eg. `ypcs/nginx:latest` as a reverse proxy.

## Live reload

By default, you can manually reload the application by touching `/tmp/reload-app`. If you want your application to reload automatically after every save, set `FLASK_RELOAD` as `true`.

docker run --rm -it -v $(pwd)/app:/app -e FLASK_APP=app:app -e FLASK_RELOAD=true -p 127.0.0.1:8080:8080 ghcr.io/seravo/flask:latest

Now, if you modify the code in `app.py` and save, the app will reload and your changes will be in effect.

## Tests

Tests can be ran by executing `pytest-3` inside the container. For example, to run the tests for `hello.py` you would do it like this:

docker exec -t {CONTAINER_NAME} pytest-3 hello.py -vv

Test output should look something like this:

platform linux -- Python 3.9.2, pytest-6.0.2, py-1.10.0, pluggy-0.13.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /app
collected 1 item

hello.py::test_hello PASSED
13 changes: 13 additions & 0 deletions app/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
# -*- coding: utf-8 -*-

from flask import Flask
import pytest

app = Flask(__name__)


@app.route("/")
def hello():
return "Hello World!"
Expand All @@ -15,5 +17,16 @@ def db():
return "FIXME: Return database status if some variable has been set..."


@pytest.fixture
def client():
with app.test_client() as client:
yield client


def test_hello(client):
response = client.get("/")
assert response.data == b"Hello World!"


if __name__ == '__main__':
app.run()

0 comments on commit 774f900

Please sign in to comment.