What is a framework in programming?

A framework is a “development tool whose overall processing flow is determined to some extent”. When creating web applications and smartphone applications, it takes a lot of time if you start from scratch. A framework solves that problem.

In this article, after introducing the outline of the framework mainly focusing on the web framework, I will show you how the framework works to make the image clearer.

Let’s master the framework and be able to develop apps at explosive speed!

Table of contents

  1. What is a framework
  2. Difference from library
  3. What is a web framework
  4. Web framework type
  5. Get started with web frameworks
  6. Summary: Frameworks are guideposts for system development

What is a framework

Suddenly, if you were asked to create an application with a blog function, how would you do it? If you’ve made an app even once, you’ll have a grasp of the flow of development, but if you’ve never made one, you’re probably completely out of your mind. You don’t even know where to start, and you should be at a loss. This is because programming is a world that is extremely free and extends both vertically and horizontally.

In theory, if you can program, you can develop any system or application. However, if you have too much freedom, you will be unable to do anything.

A framework solves this problem.

A framework is a word with meanings such as “framework”, “skeleton” and “structure”, and it gives certain rules and restrictions to the world of programming that is too free. There are many frameworks on the Internet, so choosing the framework that allows you to create the application you want to create will lead you to the goal of application development in the shortest distance. This is the advantage of using frameworks.

Difference from library

Libraries exist in similar systems, but while frameworks make development smooth frameworks and rules, libraries have the nuance of “a group of convenient systems that can be used at key points in the system.” is.

It is good to remember that “framework is the framework of the whole system” and ” library is a small system “.

What is a web framework?

A web framework is a framework suitable for developing web-based systems and applications.

Simply speaking, the term “framework” usually refers to a web framework, and famous programming languages ​​have both official and informal frameworks (frameworks are developed by the developers of the language). can be made by a third party).

The most basic part of any web framework is controlling HTTP communication. The core is to request the necessary data from the web server via HTTP, get the response and return it.  However, that alone is boring, so most web frameworks have the following features attached.

  • Model: Save POSTed data to the database
  • View: Display the acquired HTML on the screen
  • Controller: Receives HTTP requests and issues commands to models and views

Also, a web framework with these three frameworks is called an “MVC framework”.

Web framework type

There are frameworks for each programming language, but here we will introduce the Python web framework.

Flask

Flask is a great web framework for developing simple web applications with minimal functionality.

In Flask official, a framework with only minimal functionality is called a “micro-framework”.

Django

Django is a web framework that has all the features of web development. Such frameworks are called “full-stack frameworks”.

In addition, Django has functions similar to the MVC framework, although not completely, and it allows you to write neat source code by dividing the code for each function.

Get started with web frameworks

To give you a deeper understanding of web frameworks, here we will demonstrate how to create an application using Flask. This time, we will create a simple counter that increments by 1 when the button is pressed.

Flask is a web framework, but it can also be imported as a library.

The code is below.

Code (main.py):

from flask import Flask, render_template, request, redirect, url_for
import osapp = Flask(__name__)@app.route(‘/’)
def index():
return render_template(“index.html”, counter=0)@app. route(‘/plus’, methods=[“POST”])
def plus():
counter = int(request.form[‘counter’]) + 1
return render_template(“index.html”, counter=counter)

app. run(debug=True, host=os.getenv(‘APP_ADDRESS’, ‘localhost’), port=8001)

I wrote a lot, but the important one is @app.route. @app.route(‘/’) can operate the index function under it by specifying “http://localhost:8001/”. Similarly, ‘plus’ can operate the function under it by specifying “http://localhost:8001/plus”.

In other words, it routes which process to the call according to the URL. index() is opening index.html. At that time, 0 is assigned to a variable called counter and passed. plus/ adds 1 to the counter sent from the javascript side and opens index.html in the same way as an index().

I made index.html like this (only the body part is excerpted).
Let’s save index.html by creating a templates directory next to main.py.

Code (index.html):

<!DOCTYPE html>
<html lang=”JA”><head>
<meta charset=”utf-8″ />
<script
src=”https://code.jquery.com/jquery-3.5.1.js”
integrity=”sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=”
crossorigin=”anonymous”></script>
</head><body>
<form name=”f” method=”post”>
<p>count:</p>
< button type=”button” name=”btn_plus”>+1</button>
<input type=”hidden” name=”counter” value=””>
</form>
<script>
$(document).ready(function(){
$(‘button[name=”btn_plus”]’).click(function() {
$(‘form[name=”f”]’).attr(‘action’, ‘/plus’ );
$(‘form[name=”f”]’).submit();
});
});
</script>
</body></html>

Briefly, contains the counter variable passed from the index() and (). Also, if you press the +1 button, it will request at http://localhost:8001/plus. That’s it for the implementation. When I run this program and request HTTP:localhost:8001/, I get the following screen.

image 1

The “0” in count:0 displays the counter variable passed from the index().
This is what happens when you press the +1 button.

image 2

The URL has changed to http://localhost:8001/plus and the count is now 1. This is because plus() on the Python side is called.
By pressing the +1 button several times, it will increase by 1.

Image 3

In this way, by using Flask, you can create a simple flow of “receiving a request” → “internal processing” → “screen display of results”.

I didn’t include it this time, but in general applications, there are many cases where the database is handled by internal processing. Even in that case, Flask can be relatively easily linked with a database by adding Flask’s own database operation library.

This concludes the demonstration.

Summary: Frameworks are guideposts for system development

Frameworks play a role as guideposts for system development in that they clarify what you should do in the world of programming that is too free. If you want to create something, the first thing you should do is find out what frameworks are available, which is very important for smooth development.

Also, I only introduced the Python framework this time, but you can think that any famous programming language has a framework.

Once you find out what frameworks exist for the language you’re studying, you might be able to find an opportunity to grow!

Leave a Comment