Flask is a web application framework written in Python. A web framework is a collection of modules and packeges which facilitates or supports the user to create dynamic web applications. It facilitates creation,development and publishing of web applications. Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine.It includes a built-in development server, unit tesing support, and is fully Unicode-enabled with RESTful request dispatching and WSGI compliance.
Web Server Gateway Interface is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language. It acts as an interface between web servers and web applications.
Jinja2 is a template engine written in pure Python. It combines a template with a certain data source to render dynamic web pages.
- HTML
- Python
Now lets look at the installation of flask and execution of a basic Application "Hello world"
Make sure to install the latest version of python in the system using the following link: Python download
-
Python Virtual environment is used to create an isolated environment for Python projects. A project can have its own dependencies, regardless of what dependencies every other project has. Type the following command in the windows command prompt to create a new directory and virtual environment.
The above command "py -3 -m venv venv " is for virtual environment creation
-
Now type the following command in the command prompt to activate the virtual environment.
-
After activation you could notice "(venv)" on the left side implying the creation of environment and now type the following command to install flask"
"pip install flask"
-
Now use a text editor and create a ".py" file and save it in the same directory created previously i.e "app3". The code to execute "Hello world" in flask:
from flask import Flask
app = Flask(name)
@app.route('/')
def index():
return '<h1>Hello World!</h1>'
Imports the Flask module and all the related libraries
Creates the App object.
Flask uses route() to say that if the browser requests the address /, then the app should route that request to this index() function.
return '<h1>Hello World!</h1>'
Defines a function to return "Hello world!" when the app executes
-
To get the output type the following commands
-
After running the above command copy the address provided by the prompt after successful execution of the application and paste it in the browser, click enter to see the output.
The following is the link to the above python file:app2.py