Python Dictionary beginner here,
@app.route('/pokemon/original/<string:name>/', methods=['GET'])
def get_poke(name):
descrip_url = f"https://pokeapi.co/api/v2/pokemon-species/{name.lower()}"
r = requests.get(descrip_url)
json_blob = r.json()
flav_text = extract_descriptive_text(json_blob)
# clean_description = flav_text.replace("\n", " ")
d = {"name": name, "description": flav_text}
ordered_poke_list = OrderedDict(jsonify(d))
return ordered_poke_list
What I want to achieve: display the key 'name'
followed by 'description'
inside a dictionary in the format:
{'name': variable, 'description': variable}
After some research, I've learned that Dictionaries in Python are unordered by nature but can be amended using OrderedDict. I attempted to use this via code above. Error I'm receving:
TypeError: 'Response' object is not iterable
Other attempt: I've also tried using this stackoverflow question's advice and try the dict
method to no avail
d = dict(name=name,description=flav_text)
return jsonify(d)
Error: This returns description first.
Any help would be massively appreciated
Helpful info:
extract_description_text: grabs Pokemon description from the PokeAPI
def extract_descriptive_text(json_blob, language='en', version= 'sword'):
text = ""
for f in json_blob['flavor_text_entries']:
if f['language']['name'] == language and f['version']['name'] == version:
text = f['flavor_text']
return text
Traceback:
Traceback (most recent call last):
File "/Users/swapneel/Library/Python/3.8/lib/python/site-packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/swapneel/Library/Python/3.8/lib/python/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/Users/swapneel/Library/Python/3.8/lib/python/site-packages/flask_restful/__init__.py", line 272, in error_router
return original_handler(e)
File "/Users/swapneel/Library/Python/3.8/lib/python/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/swapneel/Library/Python/3.8/lib/python/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/swapneel/Library/Python/3.8/lib/python/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/Users/swapneel/Library/Python/3.8/lib/python/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/swapneel/Library/Python/3.8/lib/python/site-packages/flask_restful/__init__.py", line 272, in error_router
return original_handler(e)
File "/Users/swapneel/Library/Python/3.8/lib/python/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/swapneel/Library/Python/3.8/lib/python/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/swapneel/Library/Python/3.8/lib/python/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/swapneel/Library/Python/3.8/lib/python/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/swapneel/Desktop/projects/PokemonTranslation/main.py", line 49, in get_poke
ordered_poke_list = OrderedDict(jsonify(d))
TypeError: 'Response' object is not iterable