Learning Web frameworks

Learning Web frameworks

What is a web framework?

Today let's learn about web frameworks. I know you are wondering what is a web and what is a framework. In simple terms, a web is system of interconnected public webpages accessible through the Internet. The Web is not the same as the Internet: the Web is one of many applications built on top of the Internet. A framework is a real or conceptual structure intended to serve as a support or guide for the building of something that expands the structure into something useful.

So a web framework is a piece of software that offers away to creat and run web application I hope that manes sense.

Types of web frameworks

There are many popular web frameworks available, each with its own set of features and benefits. Some of the most commonly used web frameworks include Django, Flask, Ruby on Rails, and Node.js.

Web development is a constantly evolving field, and web frameworks are a crucial part of the toolkit for any modern developer. They provide pre-built components and libraries that can help automate repetitive tasks, speed up development time, and create robust, scalable web applications.

Flask web framework

Today I will talk about Flask web framework. Flask was created by Armin Ronacher in 2010 and has since become one of the most popular web frameworks for Python.

Flask is a lightweight and flexible Python web framework, we'll take a closer look at how flask web framework works, and how it depends on the Model-View-Controller (MVC) pattern, and dive into the details of how Flask uses this pattern to create powerful web applications. Whether you're a seasoned developer or just getting started with web development, having this kind of knowledge will provide you with valuable insights into the world of web frameworks and Flask.So let's go deeper to understand this concept.

Let's talk about MVC Pattern

Model

The model represents the data and business logic of the application. It is responsible for managing data storage, retrieval, and manipulation, as well as enforcing business rules and logic.

View

The View layer is responsible for displaying data to users. It handles user input and renders the appropriate response based on the data it receives from the Model layer.

Controller

The Controller layer acts as the intermediary between the Model and View layers. It receives input from the View layer, processes it, and updates the Model layer accordingly. It then communicates the results back to the View layer for rendering.

When using Flask in the context of MVC, the application is typically organized into a set of Python modules. These modules contain the code for the Model, View, and Controller layers of the application.

The Flask application itself acts as the Controller layer, handling requests from the View layer and updating the Model layer as needed.

The Model layer is typically implemented using a database or other data storage mechanism, while the View layer is implemented using HTML templates or other rendering mechanisms.

Getting Started with Flask

To get started with Flask, you first need to install it. Flask can be installed using pip, which is the Python package manager. You can install Flask by running the following command in your terminal

pip install flask

Once you have installed Flask, you can create a new Flask application by creating a new Python file and importing the Flask class: example

from flask import Flask

app = Flask(name)

The __name__ argument is a special Python variable that is set to the name of the current module. This is necessary for Flask to know where to find resources such as templates and static files.

@app.route('/hello/')

def hello(name):

return 'Hello, {}'.format(name)

In the above example, the route /hello/<name> maps to the hello() function. The variable part of the route is captured and passed to the hello() function as the name argument.

Run the app: Finally, run the app using the run() method. This method starts the Flask development server, which listens for incoming HTTP requests and routes them to the appropriate functions. Here's an example:

if name == 'main':

app.run()

This runs the app on the default port (5000) on the local machine.

Templates in Flask

Flask uses the Jinja2 templating engine by default. Templates are typically stored in a templates folder in the root directory of the Flask application. Flask provides developers with a render_template() function that can be used to render templates. We can say;

from flask import render_template

@app.route('/hello/')

def hello(name):

return render_template('hello.html', name=name)

In the above example, the render_template() function is used to render the hello.html template, passing the name variable to the template.

Flask Extensions

Flask extensions are packages that provide additional functionality to Flask applications. Flask extensions can be installed using pip, just like Flask itself, an example is SQLAlchemy extension that provides SQLAlchemy database toolkit for flask applications.

Bellow I will be attaching links for some of the resources that helped me learn web framework concept that I found helpful feel free check them out.

In conclusion

Overall, Flask provides a simple and flexible framework for building web applications using the Model-View-Controller pattern. By following this pattern, developers can create well-structured and maintainable code that is easy to understand and modify over time.

If you would like to connect with me personally, do send me DM on my twitter @myrajarenga, I would love to hear from you if you enjoyed reading it as I enjoyed writing it. Thank you for your time. You can also support me by following me on this blog

Reference

https://intelegain-technologies.medium.com/what-are-web-frameworks-and-why-you-need-them-c4e8806bd0fb

https://jinja.palletsprojects.com/en/2.9.x/templates/#comments

https://flask.palletsprojects.com/en/1.0.x/quickstart/#rendering-templates

https://palletsprojects.com/p/flask/

https://youtu.be/DUg2SWWK18I

https://youtu.be/1Q1G_Rz_aW4?list=PLEt8Tae2spYmDzocUL1pz8_Yc7plUg4bI