Flask 小笔记

2022-02-25 20:17:35 浏览数 (1)

What is flask?

  • Python web application framework.

Why flask?

  • lightweight
  • ability to scale up.

How to use basic functionality of flask?

Here we list the basic functionality of flasj.

Install and activation

代码语言:javascript复制
$ mkdir myproject
$ cd myproject
$ python3 -m venv venv
$ . venv/bin/activate
代码语言:javascript复制
PS C:pathtoapp> py -m venv venv
PS C:pathtoapp> venvScriptsactivate

Real code example

代码语言:javascript复制
from flask import Flask
app = Flask(__name__)

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

Run code

代码语言:javascript复制
$ export FLASK_APP=hello.py
代码语言:javascript复制
PS C:pathtoapp> $env:FLASK_APP = "hello.py"

Then flask run or python -m flask run.

Any helpful points?

  • Make sure to not call your application flask.py because this would conflict with Flask itself.
  • If you have the debugger disabled or trust the users on your network, you can make the server publicly available simply by adding --host=0.0.0.0 to the command line.

Reference material.

  • https://flask.palletsprojects.com/en/1.1.x/quickstart/#quickstart

0 人点赞