Flask for beginner

Flask for beginner

Kaitou
Kaitou

Flask?

Flask is a web framework. It is a Python module that help you develop web applications easily.

It is having a small and easy-to-extend core: it's a micro-framework that doesn’t include an ORM (Object Relational Manager) or such features.

It does have many cool features like URL routing, template engine. It is a WSGI web app framework.

Flask is flexible, allowing you to experiment and switch directions easily.

Why is Flask good?

A lightweight web application framework designed to get results fast and leave room to make the app more detailed in the future. With it, your project's code always consists only of what developers put in it, with no unnecessary code responsible for features you don’t use.

It's simplicity, resulting in clean, concise code.

Active communities (on Facebook and Reddit)

Useful documentation makes it easy to find relevant information, appropriate extensions, and simple solutions to encountered difficulties.

When to use Flask?

Flask works best when you take advantage of its flexibility and customization capabilities. If you’re delaying some technological decisions for later, it can be beneficial to depend on easily customizable Flask. You can also use Flask for simple Web apps that have limited functionalities.

Tutorial: Hello World

Need ensure installed python

Setup

Create Virtual Environment


Open a terminal (see below how to open one quickly). Then install python3-venv.
On Ubuntu Linux you can run this command:

> sudo apt-get install python3-venv

First create a project directory with the command

> mkdir pflask
> cd pflask

Then you can create a new virtual environment with the command:

> python3 -m venv venv >

Activate Virtual Environment

The virtual environment has been created, but it’s not yet active.
Activate the virtual environment on Linux, use the command:

> ./venv/bin/activate

On Microsoft Windows use this instead:

> .\venv\Scripts\activate

img

Install Flask

The first step is to install Flask. Python comes with a package manager named pip. It uses the the official Python package repository named PyPI.

You can install a Python package with the command:

> pip install flask

Hello world

Great! Now that everything is installed you can create your first Flask App.

Use the line below to import Flask in Python.

from flask import Flask

Create app, that hosts the application

app = Flask(__name__)

Then you need a route that calls a Python function. A route maps what you type in the browser (the url) to a Python function.

@app.route('/')
    def index():

The function should return something to the web browser,

return 'Web App with Python Flask!'

Almost done, the server needs to be started. This starts the web app at port 81.

app.run(host='0.0.0.0', port=81)

Code summary:

app.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'Web App with Python Flask!'

app.run(host='0.0.0.0', port=81)

img

That's all about Flask basics. You can learn more about it with useful modules.

https://flask.palletsprojects.com/en/2.1.x/
https://flask-restful.readthedocs.io/en/latest/

Thank you for reading :)