用flask搭建本地web网页不成功

引用了flask库,然后写了段小代码,希望能显示index里面内容,返回结果,通过本地127.0.0.1:5000 并不显示index里面的内容,请问是咋回事?
问题相关代码,请勿粘贴截图

import flask
app = flask.Flask(name)
@app.route("/")
def index():
f=open("index.html","rb")
data=f.read()
f.close()
return data
app.run()

程序运行结果:
C:\Python39\python.exe F:/岁岁平安/server.py

  • Serving Flask app 'server' (lazy loading)
  • Environment: production
    WARNING: This is a development server. Do not use it in a production deployment.
    Use a production WSGI server instead.
  • Debug mode: off
  • Running on http://127.0.0.1:5000 (Press CTRL+C to quit)

浏览器显示内容:
Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

我想要达到的结果

打开浏览器的时候,能显示本地index文件里面的内容,并且能告知出错的原因。

试试下面的代码:


from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
  
    return render_template('index.html')

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

记住,将index.html放在templates文件夹里面,里面在创建一个空的__init__.py文件,这个很重要,不然运行不成功。

img

你有加上斜杠嘛

你可以print 一下看看是否读取到了data

img

img


我用相同的代码试了一下,并没有出现上述问题,可能是你的前一个Flask的程序还在运行而并没有这个url导致