簡體   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