[PYTHON] Tips when amateurs use Flask in Visual Studio

I will omit the installation of Flask.

Initial configuration of Flask

image.png

Start file (runserver.py)

By default, the startup file is runserver.py. The role of this runserver.py file is to "only" start the app by executing the "app.run" function.

image.png

There is a file called "init.py" directly under the "_201114FlaskScraping" folder in Solution Explorer. If you create a file with this name, you will be able to import _201114FlaskScraping as a package. "Runserver.py" just imports "_201114FlaskScraping package" and starts it. 「from _201114FlaskScraping import app」

runserver.py


"""
This script runs the _201114FlaskScraping application using a development server.
"""

from os import environ
from _201114FlaskScraping import app

if __name__ == '__main__':
    HOST = environ.get('SERVER_HOST', 'localhost')
    try:
        PORT = int(environ.get('SERVER_PORT', '5555'))
    except ValueError:
        PORT = 5555
    app.run(HOST, PORT)

Role of init.py

init.py has two main roles. ① Represents the directory where the python script is located (2) Describe the initialization process such as importing the required module and initialize it.

__init__.py


"""
The flask application package.
"""

from flask import Flask
app = Flask(__name__)

import _201114FlaskScraping.views

routing

image.png

view.py


"""
Routes and views for the flask application.
"""
from datetime import datetime
from flask import render_template

from _201114FlaskScraping import app

@app.route('/')
@app.route('/home')
def home():
    """Renders the home page."""
    return render_template(
        'index.html',
        title='Home Page',
        year=datetime.now().year,
    )

@app.route('/contact')
def contact():
    """Renders the contact page."""
    return render_template(
        'contact.html',
        title='Contact',
        year=datetime.now().year,
        message='Your contact page.'
    )

@app.route('/about')
def about():
    """Renders the about page."""
    return render_template(
        'about.html',
        title='About',
        year=datetime.now().year,
        message='Your application description page.'
    )

Pass the value to the server side with form / input

"Method =" POST "action =" / about "" and throw it to the server

index.html


    <form method="POST" action="/about">
        <label>text:</label>
        <input type="text" class="form-control" id="name" name="name" placeholder="Name">
        <button type="submit">Send</button>
    </form>

Receive as "@ app.route ('/ about', methods = ['POST'])"

views.py


@app.route('/about',methods=['POST'])
def about():
    """Renders the about page."""
    return render_template(
        'about.html',
        title='About',
        year=datetime.now().year,
        message='Your application description page.'
    )

The / about page is displayed

image.png

Direction to add Flask functions

The basic policy is to increase the functionality of "views.py". However, since the program becomes complicated, I will use "BluePrint" ... (continued)

Recommended Posts

Tips when amateurs use Flask in Visual Studio
Time when terminal disappears in Visual Studio Code
Japanese output when dealing with python in visual studio
Install numpy in Visual Studio 2019
Bottle Pug in Visual studio 2019
Python development in Visual Studio 2017
Python development in Visual Studio
How to use linux commands in Visual Studio Code terminal
Use <input type = "date"> in Flask
Remote debugging in Visual Studio (Linux)
A memo for those who use Python in Visual Studio (me)
How to hide the command prompt when running python in visual studio 2015
Minimum knowledge to use Form in Flask
Tips for building large applications in Flask
Run Python in C ++ on Visual Studio 2017
[Tips] Easy-to-read writing when connecting functions in Python
Run Python YOLOv3 in C ++ on Visual Studio 2017
Data Science Workloads and RTVS in Visual Studio 2017
[Question] What happens when I use% in python?
Settings for Python coding in Visual Studio Code
Date of Address already in use error in Flask
Error in Flask OSError: [Errno 98] Address already in use
Use pydantic when reading environment variables in Python
Translator in Python from Visual Studio 2017 (Microsoft Translator Text API)
Preparing to use Tensorflow (Anaconda) with Visual Studio Code
App development to tweet in Python from Visual Studio 2017
How to install Google Test / Google Mock in Visual Studio 2019
Use communicate () when receiving output in a Python subprocess
Until you run server Django in Visual Studio Code
AWS SDK for Python (Boto3) development in Visual Studio 2017