一、创建工程项目
二、配置工程目录
新建的空项目下创建目录。
1、新建app.py文件
2、app.py代码如下:
from flask import Flask, render_templateapp = Flask(__name__)@app.route("/")
def root():"""主页:return: Index.html"""return render_template('Index.html')if __name__ == '__main__':app.run(debug=True, host='127.0.0.1', port='5000')
3、在根目录新建名为static的Python Package,新建完成后删除其下自动生成的__init__.py文件,static文件夹用来存放css、JavaScript、image等静态资源文件
4、在根目录新建名为templates的Python Package,新建完成后删除其下自动生成的__init__.py文件,templates文件夹用来存放HTML文件
在templates文件夹下新建index.html文件,index.html文件的代码如下:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>hello world</h1>
</body>
</html>
三、项目运行
上述目录创建后,运行app.py的main函数
浏览器访问127.0.0.1:5000即可看到index.html中的内容