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.
Let's start with the configuration file config.py:
class Config(object):
pass
class ProdConfig(Config):
pass
class DevConfig(Config):
DEBUG = True
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()
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
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
GitHub Projects: https://gzileni.github.io/flask-bootstrap/
Top comments (1)
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!