Flask Installation and Setup

Posted By : Aakash Vishwakarma | 29-Jul-2022

Written in python language, Flask is a web framework that provides libraries to create lightweight web applications in python. It is developed by Armin Ronacher.

 

Flask Environment Setup

We should have python 2.7 or higher installed in order to install flask on the system. However, we recommend utilizing python 3 for the development in the flask.

Install virtual environment (virtualenv)

virtualenv is considered the virtual python environment builder which is leveraged to construct multiple python virtual environments side by side. It can be installed by using the following command.

$ pip install virtualenv

Once it is installed, we can build the new virtual environment into a folder as given below.

$ mkdir flask_project

$ cd flask_project

$ virtualenv venv

To activate the corresponding environment, use the following command on the Linux operating system.

$ source venv/bin/activate

We can now install the flask by using the following command.

$ pip install flask


After Flask installation go inside your flask project

$ cd flask_project

 

And now  create a html file login.html.

<html>

   <body>

      <form action = "http://localhost:5000/login" method = "post">

         <table>

            <tr>

               <td>Name</td>

               <td><input type ="text" name ="User NAme"></td>

            </tr>

            <tr>

               <td>Password</td>

               <td><input type ="password" name ="Password"></td>

            </tr>

            <tr>

               <td><input type = "submit"></td>

            </tr>

         </table>

      </form>

   </body>

</html>

Create python file login.py


 

from flask import *

app = Flask(__name__)

@app.route('/login',methods = ['POST'])

def login():

   username=request.form['uname']

   passwrd=request.form['pass']

   if uname=="aakash" and passwrd=="[email protected]":

      return f"Welcome {username}"

   return "Something Went Wrong"

if __name__ == '__main__':

   app.run(port=5000,debug = True)

 

now goto terminal and run login.py file

$python login.py

Then goto chrome and run login.html file and enter User name and Password and click on submit button it will redirect to the next page where you will get a message- Welcome <your name>


 

Related Tags

About Author

Author Image
Aakash Vishwakarma

Akash is working as an Odoo developer with an overall 1+ years of experience. His areas of expertise are Python, Odoo, and MySQL. He is honest about his work.

Request for Proposal

Name is required

Comment is required

Sending message..