暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Python Flask:Hello world

Nephilim 2023-04-29
81

Tips:一些记录,一些笔记



2023/4/29

SATURDAY

Out with the old, in with the new.

旧的不去,新的不来。





01

Flask


Flask
官方网站
https://flask.palletsprojects.com/en/2.3.x/
官方Github
https://github.com/pallets/flask



02

PIP:install flask


安装Flask模块:

    adamhuan@leviathan ~ % sudo pip install --upgrade pip
    WARNING: The directory '/Users/adamhuan/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
    Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
    Requirement already satisfied: pip in Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (22.2.2)
    Collecting pip
    Downloading https://mirrors.aliyun.com/pypi/packages/08/e3/57d4c24a050aa0bcca46b2920bff40847db79535dc78141eb83581a52eb8/pip-23.1.2-py3-none-any.whl (2.1 MB)
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 7.2 MB/s eta 0:00:00
    Installing collected packages: pip
    Attempting uninstall: pip
    Found existing installation: pip 22.2.2
    Uninstalling pip-22.2.2:
    Successfully uninstalled pip-22.2.2
    Successfully installed pip-23.1.2
    WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
    adamhuan@leviathan ~ %


    adamhuan@leviathan ~ % sudo pip install flask
    Password:
    WARNING: The directory '/Users/adamhuan/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
    Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
    Collecting flask
    Downloading https://mirrors.aliyun.com/pypi/packages/9f/27/fc83394961f97e74c860d05f9bc848af00259b8c84ca021ab31250b9915e/Flask-2.3.1-py3-none-any.whl (96 kB)
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.0/97.0 kB 1.9 MB/s eta 0:00:00
    Collecting blinker>=1.6.2
    Downloading https://mirrors.aliyun.com/pypi/packages/0d/f1/5f39e771cd730d347539bb74c6d496737b9d5f0a53bc9fdbf3e170f1ee48/blinker-1.6.2-py3-none-any.whl (13 kB)
    Requirement already satisfied: Jinja2>=3.1.2 in Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from flask) (3.1.2)
    Collecting Werkzeug>=2.3.0
    Downloading https://mirrors.aliyun.com/pypi/packages/80/b2/7b7467a8c766121d061bae61f378fc62726c49511c5c8ae435a76d146361/Werkzeug-2.3.2-py3-none-any.whl (242 kB)
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 242.2/242.2 kB 1.9 MB/s eta 0:00:00
    Collecting click>=8.1.3
    Downloading https://mirrors.aliyun.com/pypi/packages/c2/f1/df59e28c642d583f7dacffb1e0965d0e00b218e0186d7858ac5233dce840/click-8.1.3-py3-none-any.whl (96 kB)
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 96.6/96.6 kB 8.3 MB/s eta 0:00:00
    Collecting itsdangerous>=2.1.2
    Downloading https://mirrors.aliyun.com/pypi/packages/68/5f/447e04e828f47465eeab35b5d408b7ebaaaee207f48b7136c5a7267a30ae/itsdangerous-2.1.2-py3-none-any.whl (15 kB)
    Requirement already satisfied: MarkupSafe>=2.0 in Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from Jinja2>=3.1.2->flask) (2.1.1)
    Installing collected packages: Werkzeug, itsdangerous, click, blinker, flask
    Successfully installed Werkzeug-2.3.2 blinker-1.6.2 click-8.1.3 flask-2.3.1 itsdangerous-2.1.2
    WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv


    [notice] A new release of pip available: 22.2.2 -> 23.1.2
    [notice] To update, run: pip install --upgrade pip
    adamhuan@leviathan ~ %


    adamhuan@leviathan ~ % pip list
    Package Version
    -------------------- -----------
    appdirs 1.4.4
    Appium-Python-Client 2.7.1
    APScheduler 3.7.0
    aria2p 0.11.2
    async-generator 1.10
    async-timeout 4.0.2
    attrs 22.1.0
    Automat 20.2.0
    beautifulsoup4 4.11.1
    blinker 1.6.2
    certifi 2022.9.24
    cffi 1.15.1
    charset-normalizer 2.1.1
    click 8.1.3
    constantly 15.1.0
    coverage 6.5.0
    cryptography 38.0.1
    cssselect 1.1.0
    Deprecated 1.2.13
    distlib 0.3.6
    Django 2.2.28
    django-apscheduler 0.6.0
    django-cors-headers 3.7.0
    djangorestframework 3.13.1
    exceptiongroup 1.0.4
    filelock 3.8.0
    fire 0.4.0
    flake8 5.0.4
    Flask 2.3.1
    furl 2.1.3
    gerapy 0.9.11
    gevent 21.12.0
    greenlet 1.1.3
    h11 0.14.0
    hyperlink 21.0.0
    idna 3.4
    importlib-metadata 5.0.0
    incremental 21.3.0
    iniconfig 1.1.1
    itemadapter 0.7.0
    itemloaders 1.0.6
    itsdangerous 2.1.2
    Jinja2 3.1.2
    jmespath 1.0.1
    loguru 0.6.0
    lxml 4.9.1
    MarkupSafe 2.1.1
    mccabe 0.7.0
    mock 4.0.3
    orderedmultidict 1.0.1
    outcome 1.2.0
    packaging 21.3
    parsel 1.6.0
    pip 23.1.2
    platformdirs 2.5.2
    pluggy 1.0.0
    Protego 0.2.1
    py 1.11.0
    pyasn1 0.4.8
    pyasn1-modules 0.2.8
    pycodestyle 2.9.1
    pycparser 2.21
    PyDispatcher 2.0.6
    pyee 8.2.2
    pyflakes 2.5.0
    pymongo 4.2.0
    PyMySQL 1.0.2
    pyOpenSSL 22.1.0
    pyparsing 3.0.9
    pyppeteer 1.0.2
    pyquery 1.4.3
    PySocks 1.7.1
    pytest 7.1.3
    pytest-cov 4.0.0
    python-scrapyd-api 2.1.2
    pytz 2022.4
    queuelib 1.6.2
    redis 4.3.4
    requests 2.28.1
    requests-file 1.5.1
    Scrapy 2.6.3
    scrapy-redis 0.7.3
    scrapy-splash 0.8.0
    selenium 4.6.1
    service-identity 21.1.0
    setuptools 58.1.0
    six 1.16.0
    sniffio 1.3.0
    sortedcontainers 2.4.0
    soupsieve 2.3.2.post1
    sqlparse 0.4.3
    superset 0.30.1
    termcolor 2.1.1
    tldextract 3.4.0
    toml 0.10.2
    tomli 2.0.1
    tox 3.26.0
    tqdm 4.64.1
    trio 0.22.0
    trio-websocket 0.9.2
    Twisted 22.8.0
    typing_extensions 4.4.0
    tzlocal 2.1
    urllib3 1.26.12
    virtualenv 20.16.5
    w3lib 2.0.1
    websocket 0.2.1
    websocket-client 1.4.2
    websockets 10.3
    Werkzeug 2.3.2
    wrapt 1.14.1
    wsproto 1.2.0
    zipp 3.8.1
    zope.event 4.5.0
    zope.interface 5.4.0
    adamhuan@leviathan ~ %


    adamhuan@leviathan ~ % pip list | grep "Flask"
    Flask 2.3.1
    adamhuan@leviathan ~ %

    可以看到,Flask的模块包已经安装成功。



    03

    编写Flask代码


    创建相关文件:

      adamhuan@leviathan ~ % cd flask_project 
      adamhuan@leviathan flask_project %
      adamhuan@leviathan flask_project % mkdir hello_world
      adamhuan@leviathan flask_project % cd hello_world
      adamhuan@leviathan hello_world %
      adamhuan@leviathan hello_world % touch app.py
      adamhuan@leviathan hello_world %
      adamhuan@leviathan hello_world % pwd
      /Users/adamhuan/flask_project/hello_world
      adamhuan@leviathan hello_world %
      adamhuan@leviathan hello_world % ls -ltr
      total 0
      -rw-r--r-- 1 adamhuan staff 0 4 29 22:16 app.py
      adamhuan@leviathan hello_world %


      代码【app.py】具体内容如下:

        adamhuan@leviathan hello_world % vi app.py 
        adamhuan@leviathan hello_world %
        adamhuan@leviathan hello_world % cat app.py
        from flask import Flask


        app = Flask(__name__)




        @app.route('/')
        def hello_world(): # put application's code here
        return 'Hello World!'




        if __name__ == '__main__':
        app.run()


        adamhuan@leviathan hello_world %



        04

        运行Flask


        运行Flask:

          adamhuan@leviathan hello_world % python3 app.py 
          * Serving Flask app 'app'
          * Debug mode: off
          WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
          * Running on http://127.0.0.1:5000
          Press CTRL+C to quit




          05

          访问网站


          访问地址:http://127.0.0.1:5000/

          这样,一个简单的Flask的「Hello World」就写完了。






          END




          温馨提示



          如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。




          文章转载自Nephilim,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

          评论