简体   繁体   中英

Having error while running a simple Python web application using Flask

I have created some machine learning algorithms and want to deploy those models using Flask and Heroku. So I wrote a Basic code using flask in Spyder IDE and getting error. Here is the working directory tree:

在此处输入图像描述

app.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)

index.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)

After going to the above link in Google Chrome I am getting the message given below on Chrome page:

    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

I'm totally new to Flask framework.

It appears that your index.html template may be UTF-16, rather than UTF-8. You might try editing it with a different editor, or making sure the editor you are using is saving as utf-8.

If that doesn't work, feel free to tell us what editor you are using, and we can give more direction.

Remove the space between route and def

@app.route(''/'')

Def home():

And if this does not work then remove the

use_reloader=false
App.run(debug=true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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