INTRODUCTION
Have you ever wished you knew how to program, but you have no idea where to start from?.@Lux Tech Academy will guide you on how to Learn Programming, Software Engineering, Data Science, Data Structures, Algorithms and Developer Relations The Right Way.
For the past three weeks during the Python Bootcamp I have;
Gained basic Python programming concepts. You can check the python basics Here.
Learnt how to develop real-world Python applications.
Learnt how Python object-oriented programming can be applied to develop software more effectively Here.
Learnt how to develop Python functions, and how to use flow control statements in python. Check it Here.
Been able to apply Python programming techniques in specific fields such as Web Development, Data Science, Machine Learning, and AI.
Applying all the basic knowledge from introduction to python to application of python for web development, This article will provide you with the basic concepts of the Python Flask framework.
The article is designed for beginners and professionals.
Introduction to Flask Web Framework.
What is a Web Framework?
- A Web Framework represents a collection of libraries and modules that enable web application developers to write applications
What is Flask?
- Flask is a web framework that provides libraries to build lightweight web applications in python.
- It was developed by Armin Ronacher, who led a team of international Python enthusiasts called Poocco.
- Flask is based on the Werkzeg WSGI toolkit and the Jinja2 template engine.
What is WSGI?
- It is an acronym for web server gateway interface which is a standard for python web application development considered as the specification for the universal interface between the web server and web application.
What is Jinja2?
*Jinja2 is a web template engine which combines a template with a certain data source to render the dynamic web pages.
Flask Environment Setup.
To install flask on the system, we need to have python 3 or higher installed on our system.
Install virtual environment (virtualenv)
Virtualenv is is a tool to create isolated Python environments.
It can be installed by using the following command;
pip install virtualenv
Once it is installed, we can create the new virtual environment into a folder as given below.
$ mkdir app
$ cd app
$ virtualenv venv
To activate the corresponding environment, use the following command;
On Linux
$ venv/bin/activate
On Windows
$ venv\scripts\activate
We can now install the flask by using the following command
$ pip install flask
First Flask application
To create your first program in the Flask, open file app.py under the app directory and add the following code.
from flask import Flask
app = Flask(__name__) #creating the Flask class object
@app.route('/') #decorator
def home():
return "Hello world";
if __name__ =='__main__':
app.run(debug = True)
Save the code and run this python code on the command line as follows.
Since it is a web application, therefore it is to be run to on the browser at http://localhost:5000.
The output is;
Summary.
To build the python web application, we need to import the Flask module. An object of the Flask class is considered as the WSGI application.
We need to pass the name of the current module, i.e. name as the argument into the Flask constructor.
The route() function of the Flask class defines the URL mapping of the associated function. The syntax is given below.
app.route(rule, options)
It accepts the folowing parameters;
rule: It represents the URL binding with the function.
options: It represents the list of parameters to be associated with the rule object.
- The run method of the Flask class is used to run the flask application on the local development server.
The syntax is given below.
app.run(host, port, debug, options)
host :The default hostname is 127.0.0.1, i.e. localhost.
port: The port number to which the server is listening to. The default port number is 5000.
debug: The default is false. It provides debug information if it is set to true.
options: It contains the information to be forwarded to the server.
Top comments (3)
I would argue that
venv
which is part of the standard library is much easier to useThanks for your feedback. Will check on that.
Congratulations on your first flask application. Nice article.