23
loading...
This website collects cookies to deliver better user experience
Flask
world. In this series, you are going to learn how to build a web application using the micro web python framework Flask
. Let's get started!mkdir flask
cd flask
For mac/unix users: python3 -m venv env
For windows users: py -m venv env
For mac/unix users: source env/bin/activate
For windows users: .\env\Scripts\activate
deactivate
pip install flask
mkdir core
cd core
__init__.py
and views.py
with the commands:touch __init__.py
touch views.py
from flask import Flask
app = Flask(__name__)
from core import views
Flask
class is imported from the installed flask package, and a __name__
variable is passed to it and they are finally assigned to an app
variable.__name__
variable which is automatically set by python helps to get the name/location of the module in which the __init__.py
script is called. In this case, __name__
is core
because the script file __init__.py
is located in the core
directory and this tells Flask where to look for resources such as templates and static files. You will understand this better when the application starts becoming large and new packages are created to separate concerns.views.py
module is imported from the project directory into the script. You must have noticed that the import is done at the bottom of the file, not at the top. The reason for this is to prevent the issue of circular imports. The routes module is being imported into the script but you will also need to import the app variable in the views.py
module which might lead to an error due to cyclical dependencies.from core import app
@app.route('/')
def index():
return 'Hello Ace'
app
variable declared in the __init__.py
script is imported here and used as a decorator. The line @app.route('/')
is a python decorator made available by flask and this is used to modify the index function called below it and to assign the URL "/" to the function. Thus, the index function gets executed whenever a user navigates to the specified URL mapped to it.flask
and create a base.py
filecd ..
touch base.py
from core import app
base.py
export FLASK_APP=base.py
flask run
FLASK_APP
. To do this you need to install the python-dotenv package.pip install python-dotenv
.flaskenv
file in the root directory and store the assignment of the script to the FLASK_APP
in it:touch .flaskenv
FLASK_APP=base.py
flask run
flask run
templates
in the core directory and create a file named index.html in it.cd core
mkdir templates
cd templates
touch index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test HTML</title>
</head>
<body>
<h1>{{greet}}</h1>
</body>
</html>
greet
variable included in the h1 element whenever the index.html file is being rendered. This will be explained better below.from flask import render_template
from core import app
@app.route('/')
def index():
greeting="Hello there, Ace"
return render_template('index.html', greet=greeting)
views.py
file. You have to import the render_template
function from the flask package. The function renders the template file index.html
and assigns the value of the greeting variable to the greet
variable in the HTML file. flask run