A web application's backend will spend most of its time handling requests from the front end. The response to the request depends on what address we requested and what HTTP verb we used. Most often, developers use two verbs:
- POST request
- GET request
You can learn more about HTTP protocol verbs in the corresponding course. For now, we'll clarify that the verb is the same part of the query as the URL in the browser address bar.
Virtually any web application will need to:
- Accept the request
- Determine which handler we should execute
- Run the handler and prepare the response
- Return the response to the customer
This set of actions remains virtually unchanged from project to project, which is why it is in the framework. We will discuss how microframeworks work and where to use them.
What is a framework?
A typical framework works in the "Don't call us, we'll call you ourselves" mode:
- The framework user embeds their functions in the ready-made framework
- The framework decides when and which functions to call
It is the difference between a framework and a regular library — libraries usually give control to the user. Let's look at an example of code built using a web framework:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
These five lines are a complete web application, even if they contain only one hello_world
handler. Notice how concise the handler code is: the handler returns a string and is as simple a function as possible. All the magic is in the @app.route
, which binds the handler to a specific path.
The path is the part of the address that comes after the domain name in the HTTP request. Let's take this address as an example:
https://foo.bar/this/is/a/path
Let's find a way into it:
/this/is/a/path
Let's go back to the first fragment:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
Here, hello_world
responds to requests along the '/'
path. It is a root — the shortest possible path.
For example, the final slash in the address https://hexlet.io/
is the root of the site, meaning its home page. The example above demonstrates the use of the Flask microframework. The prefix micro- usually says the framework only cares about routing — mapping paths to handlers. Sometimes, the micro-framework involves generating simple responses, e.g., text responses, as in the example above.
How do microframeworks differ from regular frameworks?
Microscopicity is often also related to the fact that small web applications implemented with microframeworks fit into just one code file! On the other hand, Django and other large frameworks require careful code distribution into packages and modules according to strict rules.
Most likely, you will add libraries with additional functionality to the microframework over time. A large framework is usually already packed with code for all occasions.
This division into "a reasonable minimum" and "everything at once and by strict rules" affects how easy it is to learn. It's a lot easier to get started with a microframework. We can master large frameworks later when we understand the basics of web development and decide to create something complex.
You might think microframeworks are playful or educational, but that's not right. Real projects utilize microframeworks, including large ones. The difference between large frameworks and micro frames with additional libraries can be barely noticeable. Often the choice depends on the developer's preferences.
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.