Flask-Introduction

#flask #webdevelopment #machinelearning

chaitanya ashish Oct 21 2020 · 1 min read
Share this

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. 

Third party provided libraries helps to increase flexibility of our program by simplyfying various tasks like  we can connect our flask framework to database using SQL Alchemy, use forms using FORMS and many more. 

Before using flask we have to install flask. we can do that simply by:

pip install flask

after using this command, flask will be installed. 

now lets  develop a basic wesbite using flask. 

from flask import Flask  
app=Flask(__name__) #creating an instance of flask
@app.route("/")
def homepage(): #creating a function which is mapped to the / url.
return "this is home page!"

if __name__=="__main__":
app.run() #running our created flask app.


And,there you go!  you created your first flask app. run this code in your IDE and you will be given an ip address in the terminal. open that in your browser and voila!  thats your homepage (/). 

Now, this was simple. lets try to add one more page to our application. 

from flask import Flask
app=Flask(__name__)
@app.route("/")
def homepage():
return "this is home page!")

@app.route("/loginpage") #creating a new route
def Loginpage(): #mapping this function which returns "this is login page" to /loginpage.
return "this is the login page!"

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


And there you go! you have created your second page too! But there is a problem now. Our website looks plain and boring. So we have to add some design elements into this, to make our website look more attractive. so lets use HTML,CSS and javascript. but wait a minute! we dont know how to do that right? why worry! flask got us covered. this process of using html codes in our flask website is called template rendering and those html pages are called template rendering. but before using template, there is one important thing to do. we have to create two folders inside our project directory, we have to create two folders named "static" and "templates".

something like this:

As you can see my static and templates folder is along with my projecct.py where im coding my web app using flask. 

In templates we store the html files and in static we store the files related to css and javascript. 

so download your favourite html code from the internet and save it here in templates folder. 

something like this:

As you can see i have  placed my html files here. 

once you have downloaded, its time to apply them onto your code. so lets do it. for that we will use render template. we will show index.html page on the home page and results.html in the login page. 

from flask import Flask,render_template
app=Flask(__name__)
@app.route("/")
def homepage():
return render_template("index.html")

@app.route("/loginpage") #creating a new route
def Loginpage(): #mapping this function which returns "this is login page" to /loginpage.
return render_template("results.thml")

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


and done! pretty simple isnt it? 

so that was the basic tutorial on flask of setting up and using templates. 

Any suggestions and corrections are welcome.

peace!

Comments
Read next