Friday, September 16, 2022

Flask Tutorial for beginners - Part 2

WHAT IS FLASK?

Flask is a framework for building web applications using the Python programming language.

A framework makes the work of writing a program easier by providing various building blocks so that you do not have to write a lot of code. For example, most frameworks handle the work of passing requests from the user (at the browser) back to the server.

Flask is considered a lightweight framework not as complete as some other frameworks, for example Django. In Flask, much of the setup work is still left to the programmer and there is a lot of flexibility in case you need to do something that the framework does not provide. In the case of Django, for example, you get a fully functional web application without writing a single line of code. With Flask, however, you need to write a few lines of code to get a simple web page to show.

Because Flask allows you to study closely how the interaction between a browser (your user) and the server (where the data and application lives) works, I think it's a very good place for us to learn how to write web applications.

Here's a rough diagram illustrating the relationship between the user, Python and Flask.



  • The user requests something from the Internet by typing in an address in their browser. 
  • This request reaches your application on the server and Flask answers it by looking at the address.
  • Flask then processes the request by passing it to a function in your Python application.
  • Once your function has completed processing the request, it hand it back to Flask to return the answer to the user.
Here's what a typical request might look like:

The user types in: https://my.website.com/login

The user has requested your website (my.website.com) and path is /login.

In the rest of this tutorial, we'll learn how to manage the path (/login) where we will present the user with a login page for them to type in their username (email) and a password.



At the top-left, the user asks for my.website.com. The request goes through the user's browser to the Internet to the website. Once there, Flask and Python take over to fetch the /login data. Flask then sends that back to the user.

In the next few sections, let's see how that's done.





No comments:

Post a Comment