DEV Community

James Candan
James Candan

Posted on

3 2

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
Enter fullscreen mode Exit fullscreen mode

Name your Lando project and define a python service.

name: flask-example
services:
  python:
    type: python:3.5
Enter fullscreen mode Exit fullscreen mode

Expose python and pip command line tools.

tooling:
  pip:
    service: python
  python:
    service: python
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Enjoy!

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay