繁体   English   中英

使用 Flask 运行简单的 Python web 应用程序时出错

[英]Having error while running a simple Python web application using Flask

我创建了一些机器学习算法,并希望使用 Flask 和 Heroku 部署这些模型。 所以我在 Spyder IDE 中使用 flask 编写了一个基本代码并出现错误。 这是工作目录树:

在此处输入图像描述

应用程序.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')


def home():
    return render_template('index.html')


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

索引.html

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html>

output

runcell(0, 'C:/Users/apoorv/projects/check/app.py')
* Serving Flask app "app" (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: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

转到 Google Chrome 中的上述链接后,我在 Chrome 页面上收到以下消息:

    UnicodeDecodeError
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Traceback (most recent call last)
This is the Copy/Paste friendly version of the traceback. You can also paste this traceback into a gist: 

Traceback (most recent call last):
  File "S:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "S:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "S:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "S:\ProgramData\Anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "S:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "S:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "S:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "S:\ProgramData\Anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "S:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "S:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\apoorv\projects\check\app.py", line 9, in home
    return render_template('index.html')
  File "S:\ProgramData\Anaconda3\lib\site-packages\flask\templating.py", line 138, in render_template
    ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "S:\ProgramData\Anaconda3\lib\site-packages\jinja2\environment.py", line 930, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "S:\ProgramData\Anaconda3\lib\site-packages\jinja2\environment.py", line 883, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "S:\ProgramData\Anaconda3\lib\site-packages\jinja2\environment.py", line 857, in _load_template
    template = self.loader.load(self, name, globals)
  File "S:\ProgramData\Anaconda3\lib\site-packages\jinja2\loaders.py", line 115, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "S:\ProgramData\Anaconda3\lib\site-packages\flask\templating.py", line 60, in get_source
    return self._get_source_fast(environment, template)
  File "S:\ProgramData\Anaconda3\lib\site-packages\flask\templating.py", line 86, in _get_source_fast
    return loader.get_source(environment, template)
  File "S:\ProgramData\Anaconda3\lib\site-packages\jinja2\loaders.py", line 184, in get_source
    contents = f.read().decode(self.encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

dump() shows all variables in the frame
dump(obj) dumps all that's known about the object

我对 Flask 框架完全陌生。

看来您的index.html模板可能是 UTF-16,而不是 UTF-8。 您可以尝试使用其他编辑器对其进行编辑,或者确保您使用的编辑器保存为 utf-8。

如果这不起作用,请随时告诉我们您使用的是什么编辑器,我们可以提供更多指导。

去掉 route 和 def 之间的空格

@app.route(''/'')

Def home():

如果这不起作用,则删除

use_reloader=false
App.run(debug=true)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM