Getting started with Flask
Tags: Flask, python, tutorial, webdev
Published: 2020-05-07 08:05:57

Flask is a web development framework of python. It works very smoothly for small scale development. Flask is very easy to use and meanwhile, you can develop simple, interactive, and multi-page applications with the help of flask.

Let's first look at how we can install flask.

First, we need to create a virtual environment. __virtualenv__ is a virtual Python environment builder. It helps a user to create multiple Python environments side-by-side. Thereby, it can avoid compatibility issues between the different versions of the libraries.

Step1: Install __virtualenv__ by the following command:-
             
                      pip install virtualenv

Step2: Create a folder of any name say, MyFirst.
       
                      > mkdir MyFirst
                      > cd MyFirst
                      > virtualenv venv

Step3: Now activate the virtualenv by the following command:-

                      > venv\scripts\activate

Step4: Now install Flask in this environment:-
 
                      > pip install flask


Once, flask gets install on your laptop, you can get started with your first flask application.

 Step1:- Inside the folder, create a python file say main.py.

 Step2:- Now let's go with our first flask app.
         
           main.py

         > from flask import Flask
         > app = Flask(__name__)

So, in line1, we are importing Flask module from flask library and in Line2 we are creating an object of Flask so that we can use its methods in our application easily. The '__name__'  is namespace in python. To know more about namespace,[ click here]( https://www.google.com/amp/s/www.geeksforgeeks.org/namespaces-and-scope-in-python/amp/). If we importing something from outside , __ name __ 
otherwise, 'main' will get stored in __ name __.

         > @app.route('/home')
         > def home():
              return "<h4>This is Home</h4>"
         > app.run(debug=True)

In line1, we created a route statement by using a decorator. To know what decorator is,[ click here to know more](https://www.google.com/amp/s/www.geeksforgeeks.org/decorators-in-python/amp/). Python allows us to create a decorator using a simple @ symbol. We create separate routes for every method. The '/home' indicates that when the user will write '/home' in the link, this method should get called.
In line2, we created a method that returns a simple text, when it gets called.

Now, what if we want to develop the proper HTML page with some interactive navigation bar, footer and many more. In that case, the return statement will not work for a larger code. So here we can use 'render_template'.

Step1:- Create two folders 'templates' and 'static' inside __MyFirst__ folder. Inside the templates folder, we can create our HTML document and inside the static folder, we can create our CSS and javascript files.

Step2:- Create a HTML document say,index.html.

          index.html
          <html>
            <head></head>
              <body>
               <h4> This is Home page</h4>
              </body>
          </html> 

Now in main.py:-

            main.py
          > from flask import Flask,render_template
          > app = Flask(__name__)
          > @app.route('/home')
          > def home():
                return render_template('templates/index.html')
          > app.run(debug=True)

Now, look at the last line.
Here to run our application we need to include __object_name.run(debug=True)__ . In our case our object is app. Passing __Debug=True__ will allow us to modify our app again and again.
So, guys, this was all about a simple and small application using Flask. 

For more projects using flask, you can visit my GitHub profile.
https://github.com/bhatnagaranshika02.