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 Flaskapp=Flask(__name__) #creating an instance of firstname.lastname@example.org("/")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 Flaskapp=Flask(__name__).route("/")def homepage():return "this is home page!").route("/loginpage") #creating a new routedef Loginpage(): #mapping this function which returns "this is login page" to /loginpage.return "this is the login page!"if __name__=="__main__":app.run()
something like this:
so download your favourite html code from the internet and save it here in templates folder.
something like this:
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_templateapp=Flask(__name__).route("/")def homepage():return render_template("index.html").route("/loginpage") #creating a new routedef 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.