Wednesday, May 13, 2015

Making a simple webapp using flaskr

Most flask examples use a blog as an example. This is probably very similar (and I'm pretty sure 90% was from another tutorial) but I just wanted to show some differences.

First we create a schema, and make a database

schema.sql looks like this.
Before running that you'll need to create the initial db which can be done via the command line in the same folder as your flaskr.py file
sqllite3 flaskr.db < schema.sql

 at the command line type python
>>
from flaskr import init_db
init_db()
This *should* create the first intance of the database.

finally when you're ready, start the following file with
sudo python flaskr.py

So here is the code to run for the simple server(flaskr.py)



So if you want different data stored off, you can change your schema, and the insert/select statement.

If you don't care about storing data, (you just want to perform actions) you can ignore most of the sql related items, and just read the data and perform whatever operation

For example

Now that we have a working app.

If you want to see the app get "data"
go to your browser and type in your ip address (if you did your IP and port as your values) and the data to send for example:
http://192.168.0.1/add?text=yourock&title=thistutorialhopefullyhelps

So now on your python script
request.args.get('title')  should pull out "you rock" and
request.args.get('text') should pull out "this tutorialhopefullyhelps"


Good luck!


This is the site that I had most of my tutorial from:
http://flask.pocoo.org/docs/0.10/tutorial/schema/#tutorial-schema
And the last example some came from here:
http://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask

No comments:

Post a Comment