Last active
May 9, 2022 10:00
-
-
Save CTimmerman/ddfd1746e8469811b94bb6c405485117 to your computer and use it in GitHub Desktop.
Flask CRUD demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
r = requests.get('http://127.0.0.1:5000/ai-quotes') | |
print(r.status_code) | |
print(r.headers['content-type']) | |
print(r.content) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pylint: disable=broad-except, invalid-name, redefined-builtin | |
"""CRUD API demo in Python with Flask. | |
2019-08-29 https://blog.rapidapi.com/how-to-build-an-api-in-python/ simplified by Cees Timmerman. | |
2022-05-09 Polished. | |
""" | |
import random | |
from flask import Flask, request | |
app = Flask(__name__) | |
app.config["PROPAGATE_EXCEPTIONS"] = True # https://stackoverflow.com/a/14200265/819417 | |
ai_quotes = { | |
0: { | |
"author": "Kevin Kelly", | |
"quote": "The business plans of the next 10,000 startups are easy to forecast: " | |
"Take X and add AI.", | |
}, | |
1: { | |
"author": "Stephen Hawking", | |
"quote": "The development of full artificial intelligence could " | |
"spell the end of the human race…. " | |
"It would take off on its own, and re-design " | |
"itself at an ever increasing rate. " | |
"Humans, who are limited by slow biological evolution, " | |
"couldn't compete, and would be superseded.", | |
}, | |
2: { | |
"author": "Claude Shannon", | |
"quote": "I visualize a time when we will be to robots what " | |
"dogs are to humans, " | |
"and I’m rooting for the machines.", | |
}, | |
3: { | |
"author": "Elon Musk", | |
"quote": "The pace of progress in artificial intelligence " | |
"(I’m not referring to narrow AI) " | |
"is incredibly fast. Unless you have direct " | |
"exposure to groups like Deepmind, " | |
"you have no idea how fast—it is growing " | |
"at a pace close to exponential. " | |
"The risk of something seriously dangerous " | |
"happening is in the five-year timeframe." | |
"10 years at most.", | |
}, | |
4: { | |
"author": "Geoffrey Hinton", | |
"quote": "I have always been convinced that the only way " | |
"to get artificial intelligence to work " | |
"is to do the computation in a way similar to the human brain. " | |
"That is the goal I have been pursuing. We are making progress, " | |
"though we still have lots to learn about " | |
"how the brain actually works.", | |
}, | |
5: { | |
"author": "Pedro Domingos", | |
"quote": "People worry that computers will " | |
"get too smart and take over the world, " | |
"but the real problem is that they're too stupid " | |
"and they've already taken over the world.", | |
}, | |
6: { | |
"author": "Alan Turing", | |
"quote": "It seems probable that once the machine thinking " | |
"method had started, it would not take long " | |
"to outstrip our feeble powers… " | |
"They would be able to converse " | |
"with each other to sharpen their wits. " | |
"At some stage therefore, we should " | |
"have to expect the machines to take control.", | |
}, | |
7: { | |
"author": "Ray Kurzweil", | |
"quote": "Artificial intelligence will reach " | |
"human levels by around 2029. " | |
"Follow that out further to, say, 2045, " | |
"we will have multiplied the intelligence, " | |
"the human biological machine intelligence " | |
"of our civilization a billion-fold.", | |
}, | |
8: { | |
"author": "Sebastian Thrun", | |
"quote": "Nobody phrases it this way, but I think " | |
"that artificial intelligence " | |
"is almost a humanities discipline. It's really an attempt " | |
"to understand human intelligence and human cognition.", | |
}, | |
9: { | |
"author": "Andrew Ng", | |
"quote": "We're making this analogy that AI is the new electricity." | |
"Electricity transformed industries: agriculture, " | |
"transportation, communication, manufacturing.", | |
}, | |
} | |
@app.route("/ai-quotes/") | |
@app.route("/ai-quotes/<int:id>") | |
def read(id=None): | |
"""Answers to GET request.""" | |
print(request.method) | |
try: | |
id = int(id or request.args.get("id")) | |
except TypeError: | |
id = random.choice(list(ai_quotes.keys())) | |
try: | |
return ai_quotes[id], 200 | |
except Exception: | |
return "Quote not found", 404 | |
@app.route("/ai-quotes/<int:id>", methods=["POST", "PUT"]) | |
def upsert(id): | |
"""Update or insert.""" | |
try: | |
quote = ai_quotes[id] | |
if request.method == "POST": | |
return "Quote with id {id} already exists".format(id=id), 400 | |
code = 200 | |
except Exception: | |
quote = {} | |
ai_quotes[id] = quote | |
code = 201 | |
quote["author"] = request.form["author"] | |
quote["quote"] = request.form["quote"] | |
return quote, code | |
@app.route("/ai-quotes/<int:id>", methods=["DELETE"]) | |
def delete(id): | |
"""Delete quote specified by id.""" | |
if id in ai_quotes: | |
del ai_quotes[id] | |
return "Quote with id {id} is deleted.".format(id=id), 200 | |
if __name__ == "__main__": | |
app.run(debug=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment