DEV Community

James Candan
James Candan

Posted on

Lando and Flask

Requirements

You'll need Lando installed, and that's it.

Setting up Lando

Get into your project directory, and get things started with a Lando file.

cd my-project
touch .lando.yml

Name your Lando project and define a python service.

name: flask-example
services:
  python:
    type: python:3.5

Expose python and pip command line tools.

tooling:
  pip:
    service: python
  python:
    service: python

Install Flask

Fire up the container, and let's get the requirements going. From the command line, run:

lando start
lando pip install Flask
lando pip freeze > requirements.txt

Note: It may be the case that this will add carriage return characters to your requirements.txt file. In that case, you may have to run lando ssh -s python -c 'pip freeze > requirements.txt' instead.

Now tell lando that we want it to install requirements during build.

services:
  python:
    type: python:3.5
    build:
      - 'pip install -r /app/requirements.txt'

Build an app

Build your app.py file, specifying the host as 0.0.0.0, with no port.

from flask import Flask
app = Flask(__name__)


@app.route('/')
def hello():
    return "Hello World!"


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

Flask defaults to port 5000, so let's set up our Lando proxy with that port exposed internally.

proxy:
  python:
    - flask-example.lndo.site:5000

Tell lando to run the python app at startup.

services:
  python:
    type: python:3.5
    build:
      - 'pip install -r /app/requirements.txt'
    run:
      - python /app/app.py

Conclusion

Your Lando file will finally look like the following.

name: flask-example
proxy:
  python:
    - flask-example.lndo.site:5000
services:
  python:
    type: python:3.7
    build:
      - 'pip install -r /app/requirements.txt'
    run:
      - python /app/app.py
tooling:
  pip:
    service: python
  python:
    service: python

Enjoy!

Top comments (0)