Getting Started

Flask is the best micro web framework written in Python 🔥
Installation
1 | |
- Open your favourite code editor 😍
- Create a new
*.pyfile ✅ - Import
Flaskfrom theflaskframework and define your app:
1 2 3 | |
__name__is the import name for our flask app
Create simple view function that returns simple text/html response:
1 2 3 4 5 6 7 8 | |
Next, you will should add this view function on your app's url rules. For this, we have to use Flask's add_url_rule method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
flask.Flask.add_url_rule
ruleshould bestrviewshould be function or class For class based views,Flaskhasas_viewmethod.
Routes
The method of add_url_rule is used by flask developers very rarely. They use route decorator instead of using add_url_rule and it provides to assign URLs in our app to functions easily.
1 2 3 | |
"/"- url that our view function returns response.
route decorator also uses from add_url_rule method :
From flask's source code
You can return any of the string and binary related types: str, unicode , bytes , bytearray, or if you prefer, you can return an already built response object:
1 2 3 4 5 6 7 | |
View Functions
The names we chose for our view functions should be unique.
1 2 3 4 5 6 7 8 9 10 | |