我正在使用Flask构建带有Python的REST apis。我正在使用jupiter notebook运行以下代码片段:
from flask import Flask
from flask_restful import Resource, Api, reqparse
from flask import render_template
import pandas as pd
import ast
app = Flask(__name__)
api = Api(app)
class Users(Resource):
def get(self):
data = pd.read_csv('users.csv') # read local CSV
data = data.to_dict() # convert dataframe to dict
return {'data': data}, 200 # return data and 200 OK
@app.errorhandler(404)
def not_found(error):
return render_template('error.html'), 404
class Locations(Resource):
pass
api.add_resource(Users, '/users/') # add endpoints
api.add_resource(Locations, '/locations/')
if __name__ == '__main__':
app.run() # run our Flask app我收到以下错误:
Serving Flask app "__main__" (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)
[2020-10-27 23:01:38,207] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "C:\Users\admin\anaconda3\lib\site-packages\werkzeug\routing.py", line 1945, in match
raise NotFound()
werkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\admin\anaconda3\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\admin\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 "C:\Users\admin\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 "C:\Users\admin\anaconda3\lib\site-packages\jinja2\environment.py", line 883, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\Users\admin\anaconda3\lib\site-packages\jinja2\environment.py", line 857, in _load_template
template = self.loader.load(self, name, globals)
File "C:\Users\admin\anaconda3\lib\site-packages\jinja2\loaders.py", line 117, in load
source, filename, uptodate = self.get_source(environment, name)
File "C:\Users\admin\anaconda3\lib\site-packages\flask\templating.py", line 60, in get_source
return self._get_source_fast(environment, template)
File "C:\Users\admin\anaconda3\lib\site-packages\flask\templating.py", line 89, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: error.html
127.0.0.1 - - [27/Oct/2020 23:01:38] "GET / HTTP/1.1" 500 -我只想测试rest api。如上面的错误所示,我需要为此创建模板吗?如何在jupiter notebook中做到这一点?
发布于 2020-10-28 03:07:45
首先,您正在尝试访问未定义的http://127.0.0.1:5000/ API终结点。尝试访问http://127.0.0.1:5000/users/,它应该可以工作。
其次,要定义模板,您需要在与app.py相同的目录中名为templates的目录中创建error.html
https://stackoverflow.com/questions/64560134
复制相似问题