Getting Started
Flask is the best micro web framework written in Python 🔥
Installation
1 |
|
- Open your favourite code editor 😍
- Create a new
*.py
file ✅ - Import
Flask
from theflask
framework 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
rule
should bestr
view
should be function or class For class based views,Flask
hasas_view
method.
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 |
|