Obviously there are plenty of webpages out there around minimalistic flask apps including the Flask Mega Tutorial or the full comprehensive flask project website itself. However, if found myself always googling for the same error messages and / or tutorials. Only to notice, that I clicked more than one Stack Overflow SERP more than once. After the app worked, I made the same mistake(s) again while deploying to a cloud.
Therefore I created for my further projects minimalistic working flask examples for plain APIs, apps with SQL databases and apps with user management. All examples can be easily extended and are able to grow in case the projects gets bigger during development.
You can go directly to GitHub in case you are not interested in further explanations.
Minimalistic flask app
The shortest flask app one can think of might be something like this: one file, one route (or endpoint) and no function at all.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
This kind of app runs locally, e.g. on you Windows or Linux system by executing on a console python app.py
. The single active route can be access via a browser at http://localhost:5000/
. The result will be a written “Hello world!” in you browser window. Not so bad, but not so usable also. In case you want to run this app on a cloud solution like Microsoft Azure or Pythoneverywhere.
Flask application factory
If you don’t know what the word factory means in the context of coding, remember the application factory as something like a best practice on how to structure files and folders in an efficient way to manage (bigger / growable) projects within a scope of certain concepts (accessing databases or creating http routes)
Folder and file structure
It is best to move away from the single file flask app into a folder structure like this:
├── application
│ ├── routes
│ │ ├── routes.py
│ │ ├── __init__.py
│ ├── __init__.py
├── config.py
├── README.md
├── requirements.txt
├── wsgi.py
The advantages are, that you can easily expand to more modules (like user or database management) and organize your app into logical components. Such as database management, PayPal integration or user interface etc.
The Flask package brings its own build in server for running the WebAPI locally. The app runs on your Windows or Linux by starting via the Python command python wsgi.py
, see GitHub readme for further instructions. You are now ready to deploy the app to a cloud like Pythonanywhere or to the Azure cloud.
One Comment