Flask is a light-weight and versatile micro-framework for Python that makes it simple to get began with internet building. It’s designed to be easy and minimalistic, providing very important gear and contours wanted for development a internet software, whilst permitting builders to have complete regulate over how one can put in force further options.

Flask logoFlask logo

This is a “micro-framework,” which means that it doesn’t require explicit gear or libraries. This offers builders the liberty to make a decision how they wish to lengthen their software, making Flask a sensible choice for many who want flexibility and customization. If you happen to’re coming from PHP, this may well be a substitute for the use of different micro-frameworks like Slender. If you happen to’re coming from Ruby, you could to find Flask very similar to Sinatra.

Let’s see how we will get began with Flask and construct a easy internet web page.

Set up Flask

First, we want to set up Flask. We will be able to do that by way of working:

pip set up flask

Create the App Record

Subsequent, we want to create a Python report, for instance, app.py. To get began, we upload the next code:

from flask import Flask

app = Flask(__name__)

@app.course('/')
def house():
    go back "Hi Flask!"

The Flask magnificence is used to create an example of the app. The @app.course('/') decorator maps the homepage URL, /, to the house serve as, which returns the message “Hi Flask!”.

Run the App

Now, run the app the use of the next command:

flask run --debug

Once we seek advice from http://127.0.0.1:5000/ within the internet browser, we’ll see the message “Hi Flask!”.

On this case, we additionally run the app in debug mode, which mechanically reloads the server when you’re making adjustments to the code. This turns out to be useful for building, as you’ll see the adjustments in an instant with no need to restart the server. Understand that you’d wish to disable debug mode when deploying your software to a manufacturing server.

Routing

Some of the primary options supplied by way of Flask is routing. Routing is the method of mapping URLs to purposes that maintain requests. Within the instance above, we outlined a course for the homepage the use of the @app.course('/') decorator. This tells Flask to name the house serve as when the consumer visits the homepage URL, /.

We will be able to outline routes for different pages as neatly. As an example, to create an About web page, we will upload the next code:

@app.course('/about')
def about():
    go back "That is the About web page."

Now, if we load http://127.0.0.1:5000/about, we’ll see the message “That is the About web page.”.

Wrapping up

Flask is a smart selection for each rookies and skilled builders, particularly when you wish to have flexibility with out the complexity of bigger frameworks like Django.

On this article, we’ve coated the fundamentals of the way Flask works with some easy examples corresponding to how one can spin up the improvement server, and we discovered a bit of about routing in Flask to create static pages. In long run articles, we’ll discover extra complex subjects like rendering templates, running with bureaucracy, dealing with requests, and connecting to databases.

So keep tuned!

The submit Getting Began with Flask gave the impression first on Hongkiat.

WordPress Website Development Source: https://www.hongkiat.com/blog/getting-started-with-flask/

[ continue ]