blockchain
blockchain copied to clipboard
Fix related gist (Step 6: Setting up Flask)
What's wrong
@dvf it would be great if you could fix Step 6: Setting up Flask gist, it affects the workflow at the Learn Blockchains by Building One article. Also there is no way to mention someone at the gist comments, so I created this issue instead.
Expected behavior
full_chain endpoint should return JSON:
{
"chain": [
{
"index": 1,
"previous_hash": 1,
"proof": 100,
"timestamp": 1532563934.4369285,
"transactions": []
}
],
"length": 1
}
Actual behavior
NameError raised while trying to access the full_chain endpoint:
Traceback (most recent call last):
File "~/blockchain/.venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "~/blockchain/.venv/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "~/blockchain/.venv/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "~/blockchain/.venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "~/blockchain/.venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "~/blockchain/.venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "~/blockchain/.venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "~/blockchain/.venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "~/blockchain/.venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "~/blockchain/.venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "~/blockchain/blockchain.py", line 132, in full_chain
return jsonify(response), 200
NameError: name 'jsonify' is not defined
Solution
Fix imports here, replace
from flask import Flask
with
from flask import Flask, jsonify
Thanks for this!