Flask Run

Quickstart

  • On PowerShell

    1
    2
    3
    4
    5
    6
    mkdir flk
    cd flk
    py -m venv venv
    pip install flask
    mk hello.py
    New-Item hello.py -type file
  • 在 hello.py 写入

    1
    2
    3
    4
    5
    6
    from flask import Flask
    app = Flask(__name__)

    @app.route('/')
    def hello_world():
    return 'Hello, World!'
  • run

    1
    2
    $env:FLASK_APP = "hello.py"
    flask run
  • get / post

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    @app.route('/')
    def hello_world():
    return 'Hello, World!'


    @app.route('/name')
    def elias():
    return 'Hello, elias!'

    @app.route('/name/<username>') #<>内默认 String
    def elias(username):
    return 'Hello, %s!' % username

    # @app.route('/name/<float:a>') #float
    @app.route('/name/<int:a>') # int
    def elias(a):
    return 'Hello, %s!' % (a + a)

    @app.route('/login', methods=['GET', 'POST'])
    def login():
    if request.method == 'POST':
    return "A"
    else:
    return "B"

Reference

< - 僕らは知らない - >