新建Flask项目

 

 

一、开发环境准备 

1. 安装Conda

https://anaconda.org/  

https://www.cnblogs.com/elewei/p/12234888.html

Conda是一个数据科学的软件包,可以用来直接创建虚拟环境,为以后做科学技术时提供方便。而且可以直接管理python版本,升级python比较方便。

 

2. 创建虚拟环境并安装软件包

conda create -n venv

conda install python

pip install flask

 

完成以上操作后, 使用如下命令检查确认版本

conda env list

python --version

pip list

 

3. 创建Flask项目

D:\myapp>tree /f
文件夹 PATH 列表
D:.
│  app.py
│
├─blog
│  │  __init__.py
│
└─instance
        config.py

 

 

# blog/__init__.py

import os

from flask import Flask


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    # a simple page that says hello
    @app.route('/hello')
    def hello():
        return 'Hello, World!'

    return app

 

 

 

# instance/config.py

SECRET_KEY='dev'

 

 

# app.py

from blog import create_app

app = create_app()

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

 

 

 

执行 python app.py 即可启动应用

 

 

 

4. 添加 Git 版本支持

添加 .gitignore 文件

https://github.com/github/gitignore/blob/master/Python.gitignore

 

git init

git add -A

git commit -m 'initialize commit'

 

posted @ 2021-01-08 13:21  elewei  阅读(110)  评论(0编辑  收藏  举报