Creating a simple health app #315
-
As soon as I saw the announcements for FastHTML, I was thinking this could be the thing I can use to create a little health app I always wanted to make for myself. The simplest version would be a form where I enter my weight, click submit and this would be stored in a sqlite table with a timestamp. I've managed to create the form, more or less, but I'm not sure how to "post" it to the table. So this is my code for now: app = FastHTML() app, rt, healthdata, HealthData = fast_app('/Volumes/backup/sqlite/tinyhealth.db', @app.get("/") I've tried creating a def post that would store it, but I don't really get what I'm doing. I'm getting Method Not Allowed left and right, or otherwise an Internal Server Error. Can anyone help me? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Okay I think I've got it. I actually managed to create a post method to save a row with data in the sqlite database. So what did I do to get it working? I've added a post function and learned that you need to put the weight (and bmi) in as arguments. The route is "/" because we're not going anywhere. We're only going to insert data.
You can add a return to this function that shows data has actually been passed through: The insert statement works like this: The created_at is my timestamp. Now when I save this, my form looks like this: After submitting the data, I can look up the sqlite table in DBeaver and it actually has added rows. I've created a repo for this code, so everyone can look at it. |
Beta Was this translation helpful? Give feedback.
Okay I think I've got it. I actually managed to create a post method to save a row with data in the sqlite database.
So what did I do to get it working?
The home function is largely unchanged (except for the added BMI input).
I've added a post function and learned that you need to put the weight (and bmi) in as arguments. The route is "/" because we're not going anywhere. We're only going to insert data.
@app.route("/", methods=['post'])
def post(weight:float, bmi:float):
You can add a return to this function that shows data has actually been passed through:
return f"Entered into the database: weight={weight}, bmi={bmi}"
The insert statement works like this:
healthdata.insert(HealthData(…