Flask Python Tutorial for Beginners

In this Flask Tutorial we are going to learn about Flask Python Tutorial for Beginners, Flask is a lightweight and flexible Python web framework and it is perfect for building web applications quickly and easily. in this tutorial we want to cover the basics of Flask and show you how to build a simple web application from scratch.

 

 

These are the 10 Python Flask topics that we want to cover in this tutorial:

 

 

Now let’s start from installing Python Flask.

 

 

Installing Python Flask

First step in building Flask web application is to install the Flask framework. you can install Flask using pip like this.

 

 

 

Creating a Flask App

After that you have installed Flask, you can create a new Flask application by creating a Python file with  .py extension. this is a simple Flask app that will display “Welcome to codeloop.org” when you run it:

 

 

 

 

 

If you run the code and go to http://127.0.0.1:5000/ URL, this will be the result.

Flask Python Tutorial for Beginners
Flask Python Tutorial for Beginners

 

 

 

Flask Routes

In Flask routes are used to map URLs to view functions that return HTML content. in the above example @app.route(‘/’) decorator maps the root URL to the hello() function, which returns a string. you can create routes for different URLs by creating more view functions and using different decorators.

 

 

 

Flask Templates

Flask uses templates to separate the presentation logic from the application logic. Templates are HTML files that contain placeholders for dynamic content. you can use Flask render_template() function to render templates and pass data to them, in this example hello() function passes the name variable to the hello.html template, which can use it to display a greeting.

 

 

 

This is our HTML file, make sure that you have created templates folder and in that folder create index.html file.

 

 

 

 

Run the code and this will be the result

Flask Python Tutorial for Beginners
Flask Python Tutorial for Beginners

 

 

 

Flask Forms

Flask also provides support for handling HTML forms. you can create forms using Flask-WTF extension and use them to capture user input. this is an example, in this example NameForm class defines a form with a single name field and a submit button. hello() function renders an index.html template that displays the form. when the user submits the form, the validate_on_submit() function checks if the form data is valid, and if it is hello() function renders a index.html template that displays the user name, also you need to install Flask-WTF using pip pip install Flask-WTF.

 

 

This is index.html

 

 

This is hello.html

 

 

And this will be the result

Flask Python Tutorial
Flask Python Tutorial

 

 

 

Flask SQLAlchemy

Flask also supports working with databases using SQLAlchemy extension. you can use SQLAlchemy to connect to a database, define database models and execute queries. this is an example of using SQLAlchemy to define a simple database model:

 

 

 

For creating the database model, first you open flask shell, by writing flask shell in your terminal, after that you need to import db from app.py file and create the database.

 

 

 

This will be the result and you can see we have our database and table with the fields.

Flask Python Tutorial for Beginners - Flask SQLAlchemy
Flask Python Tutorial for Beginners – Flask SQLAlchemy

 

 

 

Flask Sessions

Flask also provides support for using sessions to store user-specific data between requests. you can use the session object to store and retrieve data in the session. this is an example of using sessions to store and retrieve a user name:

 

 

 

Run the code go to http://127.0.0.1:5000/login URL, write something in the field and submit the form.

Flask Session
Flask Session

 

 

 

Flask Deployment

After that you have developed your Flask application, it is time to deploy it. there are several options for deploying Flask applications, It depends on your choice, for example we can use Platform as a Service (PaaS) provider, such as Heroku or Google App Engine. these services provides a simple way to deploy web applications without worrying about server setup and configuration. this is an example of deploying a Flask application to Heroku:

 

 

First we need to create a Procfile in the root directory of our application, and it should contain the command to run our application. for example if our application is in a file called app.py, Procfile should contain:

This tells Heroku to use gunicorn server to run the app object in the app.py file.

 

 

After that we have created requirements.txt file in the root directory of your application, and it should contain a list of all Python packages your application requires. you can generate this file automatically using pip freeze command like this.

 

 

Create a new Heroku application using Heroku CLI:

 

 

Deploy your application to Heroku using Git:

 

 

Open your application in a web browser:

 

 

 

Flask RESTful API

Flask is used for building RESTful APIs. this is an example of creating a simple RESTful API with Flask, this API provides two endpoints: /api/books to get a list of all books, and /api/books/<int:book_id> to get the details of a specific book. jsonify function is used to serialize Python objects to JSON.

 

 

 

This will be the result by going to http://127.0.0.1:5000/api/books/1

Flask REST API
Flask REST API

 

 

 

Flask Authentication

Flask provides several extensions for implementing authentication in your applications. one popular extension is Flask-Login. this is an example of using Flask-Login to implement user authentication in a Flask application, make sure that you have installed flask-login. you can install using pip pip install Flask-Login.

In the above example Flask-Login is used to handle user authentication. User class is a simple user object that inherits from UserMixin. user_loader function is used to load a user object from the user ID. login function handles the login form submission, and the protected function is protected by the @login_required decorator. and lastly the logout function logs the user out and redirects them to the login page.

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×