DEV Community

Giuseppe Zileni
Giuseppe Zileni

Posted on

Flask Bootstrap

Flask is very simple to use to create REST APIs with Python even with a single file. The repository flask-bootstrap is a template to start developing more complex applications with Flask.

Alt Text

Let's start with the configuration file config.py:

class Config(object):
  pass

class ProdConfig(Config):
  pass

class DevConfig(Config):
  DEBUG = True
Enter fullscreen mode Exit fullscreen mode

The root file main.py with ENDPOINT:

from flask import Flask
from config import DevConfig

app = Flask(__name__)
app.config.from_object(DevConfig)

@app.route('/')
def home():
  return '<h1>Hello World!</h1>'

if __name__ == '__main__':
  app.run()
Enter fullscreen mode Exit fullscreen mode

The template is a simple REST API Server with an ENDPOINT '/' to display the classic "Hello World!" in the browser at the address to http://127.0.0.1:5000.

The project is structured as follows:

  • Dockerfile: Instructions for applying with Flask in a docker container
  • requirements.txt: Application dependencies
  • /venv: Developmental python environment
  • .gitignore: Files to exclude in commits for Git
  • main.py: The main file
  • config.py: Application configuration

To run the first flask project:

git clone https://github.com/gzileni/flask-bootstrap.git
cd flask-bootstrap

pip install -r requirements.txt
export FLASK_APP=main.py
flask run
Enter fullscreen mode Exit fullscreen mode

An easy way to run and share the application with the team and send it to production is by using Docker:

docker build -t flask-bootstrap .
docker run -p 5000:5000 flask-bootstrap
docker container list
Enter fullscreen mode Exit fullscreen mode

GitHub Projects: https://gzileni.github.io/flask-bootstrap/

Top comments (1)

Collapse
 
james_dev_b83870886a profile image
James dev

Thanks for your explanation

Hello, Could you help me?
Now I'm developing one flask app.
When I render the template, use CMD => "flask run".
But whenever I change the template HTML or CSS codes, I have to reinput flask run...

It's a bit stressful, how can I automatically debug the flask app....
Please help me

Best regards!