DEV Community

Anshika Bhatnagar
Anshika Bhatnagar

Posted on

Getting started with Flask

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

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

     > 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. 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>"

In line1, we created a route statement by using a decorator. To know what decorator is, click here to know more. 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.

           <h4> This is Home page</h4>

Now in
      > from flask import Flask,render_template
      > app = Flask(__name__)
      > @app.route('/home')
      > def home():
            return render_template('templates/index.html')

Now, look at the last line.
Here to run our application we need to include . 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.

Top comments (2)

amanpandya1162 profile image
Aman Pandya

Nice tutorial 👌👌

bhatnagaranshika02 profile image
Anshika Bhatnagar

Glad that you like it..!!