I've been exploring some things with my final project in mind. My final project will take a flask app and hook it up with a react front-end which will be a form. this form will send things to a database using peewee, so it will be a full-stack react app. It will be the first truly full stack app I've ever made so I'm pretty excited about it. It may sound like I've done this before but actually, I'm only vaguely familier with these libraries so it would be a good jumping off point to make a final project like this.
So for the first part of my exploration, I needed to actually make the flask app and routes, and I needed it to store information in a database that I entered into a form. This is not what my final product will look like, but I figure I should start by trying to create a bunch of close approximations and try to stitch them together.
I followed this tutorial, Python Web Apps with Flask by Ezra Zigmond https://www.youtube.com/watch?v=qla-KaMF-2Q
And he walked me through ways to make different kinds of routes in flask. I've done this before and made curl requests from another source but it was good to see other examples. I was super interested in this debugging tool I never knew existed:
if __name__ == '__main__': app.run(debug = True)
and I played around with assigning functions to particular addresses on my webpage:
@app.route('/hello/<name>') def hello(name): return 'hello, %s' % name
essentially I got this form to work in a browser that takes two inputs(see picture 1) posts them, and saves them. And you can keep adding to the posts (see picture 2).
I tried to hook one of the react forms I made for the health department, up to my new flask app (at least, the part not including the html form). I did this by trying to turn my flask app into an api looking at this RESTapi peewee documentation http://docs.peewee-orm.com/projects/flask-peewee/en/latest/rest-api.html. So I tried to register the models in my flask app so that they are "exposed" (for consequent hooking up) importing the RestAPI module into my models.py file.
initialize_db() # register our models so they are exposed via /api/<model>/ api = RestAPI(app) api.register(Post) # configure the urls api.setup()
I debugged for days. I am hitting this error when I try to get a response using curl:
curl http://127.0.0.1:5000/api/post <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>404 Not Found</title> <h1>Not Found</h1> <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
..technically the article says this api is going to turn my information into json so react can read it. But I coulden't get it to work. sigh. stay tuned.
To run this, you need to have pip3 and flask installed, and need to be running python3. you run the flask_app.py file and the two html files need to be in their own templates folder. the "models" file are my..models. the flask_app_test.py file is me just experimenting. signup.db is created by one of the files when you run it..
|Pic1.png||Tue Dec 05 2023 08:56 am||22K|
|Pic2.png||Tue Dec 05 2023 08:56 am||67K|
|flask_app.py||Tue Dec 05 2023 08:56 am||974B|
|flask_app_test.py||Tue Dec 05 2023 08:56 am||754B|
|home.html||Tue Dec 05 2023 08:56 am||426B|
|models.py||Tue Dec 05 2023 08:56 am||1.2K|
|new_post.html||Tue Dec 05 2023 08:56 am||375B|
|signup.db||Tue Dec 05 2023 08:56 am||12K|