| id | py_tutorial |
|---|---|
| title | Python Tutorial |
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
Two dependencies are needed for this project, deta and flask:
pip install deta flaskTo configure the app, import the dependencies and instantiate your database.
from flask import Flask, request, jsonify
from deta import Deta
deta = Deta('myProjectKey') # configure your Deta project
db = deta.Base('simpleDB') # access your DB
app = Flask(__name__)For our database we are going to store records of users under a unique key. Users can have three properties:
{
"name": str,
"age": int,
"hometown": str
}We'll expose a function that creates user records to HTTP POST requests on the route /users.
@app.route('/users', methods=["POST"])
def create_user():
name = request.json.get("name")
age = request.json.get("age")
hometown = request.json.get("hometown")
user = db.put({
"name": name,
"age": age,
"hometown": hometown
})
return jsonify(user), 201POST a payload to the endpoint:
{
"name": "Beverly",
"age": 44,
"hometown": "Copernicus City"
}Our server should respond with a status of 201 and a body of:
{
"key": "dl9e6w6859a9",
"name": "Beverly",
"age": 44,
"hometown": "Copernicus City"
}To read records, we can simply use db.get(key).
If we tie a GET request to the /users path with a param giving a user id (i.e. /users/dl9e6w6859a9), we can return a record of the user over HTTP.
@app.route("/users/<key>")
def get_user(key):
user = db.get(key)
return user if user else jsonify({"error": "Not found"}), 404Let's try reading the record we just created.
Make a GET to the path (for example) /users/dl9e6w6859a9.
The server should return the same record:
{
"key": "dl9e6w6859a9",
"name": "Beverly",
"age": 44,
"hometown": "Copernicus City"
}To update records under a given key, we can use db.put(), which will replace the record under a given key.
We can tie a PUT request to the path /users/{id} to update a given user record over HTTP.
@app.route("/users/<key>", methods=["PUT"])
def update_user(key):
user = db.put(request.json, key)
return userWe can update the record by passing a PUT to the path /users/dl9e6w6859a9 with the following payload:
{
"name": "Wesley",
"age": 24,
"hometown": "San Francisco"
}Our server should respond with the new body of:
{
"key": "dl9e6w6859a9",
"name": "Wesley",
"age": 24,
"hometown": "San Francisco"
}To delete records under a given key, we can use Base.delete(key), which will remove the record under a given key.
We can tie a DELETE request to the path /users/{id} to delete a given user record over HTTP.
@app.route("/users/<key>", methods=["DELETE"])
def delete_user(key):
db.delete(key)
return jsonify({"status": "ok"}), 200We can delete the record by passing a DELETE to the path /users/dl9e6w6859a9.
{
"name": "Wesley",
"age": 24,
"hometown": "San Francisco"
}Our server should respond with:
NoneIf you run into any issues, consider reporting them in our Github Discussions.