Start using Flask(Python) in 2 simple steps.

Start using Flask(Python) in 2 simple steps.

Learn Flask in 2 simple steps.

⚡ Lazy tutorial

tutorial 1

this tutorial is part lazy tutorial series and first in series where i help you learn a topic in bare minimum effort.

🥱BORING STUFF

designing a good quality application has never been a easy process. but now a days you can design industry level applications with bare minimum efforts.

the most important thing stands in this way is choosing the right toolset, and service for your application.

choosing right tools can boost your application efficiency by significant amount and help reducing your work load by major factor.

so, today we are going to learn about using flask as your application server in as simple way as possible.

🔵 INTRODUCTION

flask is a micro web framework designed for python and it does not require any other third party tools or libraries. flask is designed keeping in mind to make web application as quickly as possible without giving time to other boilerplate functionality like web server gateway interface.

🟢WHY FLASK?

  • flask is lightweight in contrast to django and other frameworks.
  • easy implementation
  • Extensive documentation
  • uses Jinja templating.
  • wsgi 1.0 compliant.

let's done with all this standard you don't care blah blah stuff and start to get your hands dirty and write some code. before that do install all the proper library and module required. well there is one in this case flask, obviously.

😎COOL STUFF

STEP 1

Installation

create your project directory and python virtual environment

mkdir mycoolapp
cd mycoolapp
python3 -m venv flask_project

then install flask using pip.

pip install Flask

create a python file "myapp.py" you can name it whatever you want.

i know you are still going to name it "myapp.py" that's what lazy programmers do copy and paste. now we are ready to go.

STEP 2

after you have setup your project---- import flask, you know how to import modules in python right well if not its a very complicated task go back learn that first you newbie.

i'm joking you can easily import modules likes this.

from flask import Flask

create a app instance that uses flask

app = Flask(__name__)

now you are almost done you just need to write routes for request in your application. how you do that. well that's easy. use this to define your routes:

@app.route('/your route', methods = ['GET'])

in this app.route thingy you have to give two things:

  • method which defines type request you are accepting(GET,POST).
  • request route(the route where you accept the request, so all the request coming to this route on your app server will be handled here)

HOW?🤔

well by simply defining a function right after the route

def my_route():

return "<p>my first flask app</p>"

in these routine the return type is used to send responses. all the responses will be send through here.

its seems easy now what?

well it is easy. now its time to start your server.

here we are using GET method and for POST method whose sole purpose is to accept we have to use requests to extract that data.

just import request sub module like this

from flask import request

now you can extract all the data from incoming request like this:

@app.route('/post_requests_route', methods = ['POST'])
def post_requests():
   name = request.form['username']
    return {
        "response":"data received"
    }

its time to start testing our app.

complete code:

from flask import Flask

@app.route('/my_route', methods = ['GET'])
def my_route():
return "<p>my first flask app</p>"

extra charged up code:

from flask import Flask

#for get request
@app.route('/my_route', methods = ['GET'])
def my_route():
return "<p>my first flask app</p>"

#for post request
@app.route('/my_post_request_route', methods = ['POST'])
def post_requests():
   name = request.form['username']
    return {
        "response":"data received"
    }
#for gunicorn lovers
if __name__ == '__main__':
    app.run()

inside your terminal run this to start Flask test server

export FLASK_APP=hello
flask run

Note:it is a flask test server and should not be used as production server.

so if you are extra charged up and want to this on production level server. here is list of some production level server for self hosting which integrate smoothly with flask.

  • gunicorn
  • uwsgi

you must be like what about them how to use them.

well this is lazy tutorial and you are becoming little bit of non lazy.

for you non lazy folks here is how you use gunicorn.

first install it in your virtual environment.

pip install gunicorn

add this little bit of code.

if __name__ == '__main__':
    app.run()

now you can easily run your app with gunicorn:

gunicorn myapp:app

you should probably see something like:

[2021-08-08 18:35:10 +0530] [343] [INFO] Starting gunicorn 20.1.0
[2021-08-08 18:35:10 +0530] [343] [INFO] Listening at: http://127.0.0.1:8000 (343)
[2021-08-08 18:35:10 +0530] [343] [INFO] Using worker: sync
[2021-08-08 18:35:10 +0530] [345] [INFO] Booting worker with pid: 345

now fire up your favourite browser ang go to this url

127.0.0.1:port_no/my_route

you should see your message:

my first flask app

congratulation🎉 you lazy programmer you have created your first bare minimum effort flask application.

apology for any incorrect grammar.

from a lazy programmer to lazy programmers.

comment if you have any question and suggestion.

Get in touch with me on twitter:

keep learning