[PYTHON] Flask development introduction

Major features

--Microweb framework for Python. --Flask is a web framework that is lightweight and doesn't have that many features. --It doesn't have as many features as Django. There are no extra features.

Django is MVC, Flask is MVT

For MVC models such as Django, see below

Model: Describe business logic. Often involves database access View: In charge of screen drawing. Accepts input from users Controller: The role of the control tower that distributes URLs. Pass the data from the view to the model and vice versa

On the other hand, Flask takes the form of MVT instead of MVC. This is an acronym for Model, View, Template. MVC and MVT have almost the same content, but their roles are different.

Model: Same role as Model in MVC model View: Same role as Controller in MVC model Template: Same role as View in MVC model

Virtual environment construction-Flask installation procedure

environment

  1. Move to the directory of the created project. This time the project name is Flask App.
cd FlaskApp
  1. Create a virtual environment with venv (the last venv is the file name, so anything is fine)
python3 -m venv venv
  1. Run the virtual environment
source venv\bin\activate
  1. Install Flask
pip install Flask
  1. Make sure Flask is installed properly
pip freeze 

If it looks like the following, it's OK for the time being

Click==7.0
Flask==1.1.1
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
Werkzeug==0.16.0

Try to create a simple application

Create the following source. Name the file views.py.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World'

if __name__ == '__main__':
    app.debug = True
    #When launching the app under Linux environment, it is better to explicitly specify the IP and port number as shown below. The port number 5100 is appropriate.
    app.run(debug=True, host='0.0.0.0', port=5100)

[Supplement to the 11th line of the source code] --host keyword argument: Specify the IP address of the server. --port keyword argument: Specify the port number. If not specified, the port number is 5000 by default. --debug keyword argument: Specifies whether to enable debug mode. If not specified, it is disabled by default.

Run the application with the following code

python views.py

OK if Hello World is displayed on the browser

Recommended Posts

Flask development introduction
Web application development with Flask
flask
flask
Application development with Docker + Python + Flask
DORA development
First Flask
kivy introduction
Flask memo
Introduction to Tornado (3): Development using templates [Practice]
Introduction to Python Let's prepare the development environment
Introduction to Tornado (2): Introduction to development using templates-Dynamic page generation-
Understand Python for Pepper development. -Introduction to Python Box-