This is a basic Flask tutorial for beginners in which we will see what is Flask and how to use it in projects.
What is Flask?
Flask is a web framework of Python language that helps to build web applications easily and manageably.
If you want to create a simple web application using HTML, CSS, and JavaScript as frontend and Python as backend, then Flask framework will help you create it quickly. The framework has lots of libraries and functionalities that help to make a perfect dynamic website like routing from one page to another, getting the user form data and storing it into the database, etc.
A framework is a pre-built structure that allows building applications on it easily without doing everything from scratch.
Using frameworks saves time and reduces the risk of errors. You don’t need to write everything from point zero, so there’s less chance of introducing errors. Plus, frameworks have already been tested, so there’s less to worry about.
Some popular Frameworks in Python are:
- Flask
- Django
- CherryPy
- Pyramid
- Web2Py
Out of all the above, the most commonly used Frameworks are Django and Flask. Django is very reliable and robust and is used for creating big sites with lots of functionalities.
But if you are planning to learn Django any time, we would recommend you to learn Flask first as it is easy to learn and best suited for small and easy projects. Having the knowledge of Flask makes you understand Django better and quickly.
Installing Flask
To install Flask on your machine, first, you need to have Python installed with version 2.7 or higher. You can check the presence of Python or its version in your system (Win/Mac/Linux) using the command:
python --version
To install Flask on your system, just run this piece of code:
pip install flask
Creating a basic Flask application
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World'
if __name__ == '__main__':
app.run()
In the above example, we first called the Flask class which is necessary to use it. Then in the second line, we declared the app. In the mid-section, we have created a route, which means what would happen when the user hit that route. As we can see just the slash (/), which means when the user hit the URL of the root folder, a function named ‘hello_world’ is triggered which in return prints the word ‘Hello World’. Finally, the run() method of the Flask class runs the application on the local development server.
Running the above code and getting the result
Let’s suppose the above code is saved with the file name ‘first.py’. We can run this file in our Python machine by typing this in the terminal:
python first.py
This will give you a URL of your local machine with default port 5000. When you click the URL, a web browser will open with the page written “Hello World”. This is what we have written in the above code.
Working with Flask routes
The word ‘route’, in general, is used for way or path. Here in Flask ‘route’ also means URL address path. It helps us to create a response when the user is going to hit that route in the address bar. Let’s see the example below:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_home():
return 'Home Page'
@app.route('/about')
def hello_about():
return 'About Page'
@app.route('/contact')
def hello_contact():
return 'Contact Page'
if __name__ == '__main__':
app.run()
In the above code, it can be seen that when the user will hit the home route (‘ / ‘), the function ‘hello_home’ will trigger and show the word ‘Hello Home’ written.
When the route (‘/about’) is hit in the browser, the function ‘hello_about’ is triggered which returns the word ‘About Page’ in the browser.
Similarly, the function ‘hello_contact’ will trigger on hitting the route ‘/contact’ and will show ‘Contact Page’ in the browser.
Working with Templates: HTML page rendering
Doesn’t matter how powerful the backend language is, a website is complete only if it has some HTML pages in it for user interaction. In Python Flask, we can easily render HTML pages as output.
The HTML pages should be stored in a folder named ‘templates’. Let’s suppose you have an HTML page named ‘home.html’ stored in the templates folder which you want to render as the first page of your website. Let ‘app.py’ is your Python file, then simply use the below code.
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template("home.html")
if __name__ == "__main__":
app.run()
Here we are calling the ‘render_template’ functionality from the flask library. On hitting the route (‘/’) by the user in his address bar, the function ‘home’ will be triggered and which in return renders the HTML page named ‘home.html’.