Example of a basic API for a sqllite3 database
###Usage###
The file app.py demonstrates CRUD operations for a table called 'messages' that has two text columns, 'name' and 'comment'.
When the app launches it will check if the database tables already exist, and create them if necessary.
Routes:
Route | Method | Parameters | Result (JSON) |
/ | GET | Returns 'hello world' | |
/messages | GET | Returns all messages | |
/messages | POST | name=foo&comment=bar | Creates a new message with the posted values |
/search | GET | name=foo | Returns all messages where name = 'foo' |
/messages/1 | GET | Gets the message with id=1 | |
/messages/1 | POST | name=foo&comment=bar | Update the message with id=1 |
/messages/1 | DELETE | Delete the message with id=1 |
To deploy a database on the web using this file:
- Update app.py according to the tables you want in your database. Specifically, you should change the
db_init()
function so that it creates one or more table(s) with the rows/datatypes you would like. See here for a list of sqllite3 data types. - Create an account at Python Anywhere. The URL for your app will default to username.pythonanywhere.com.
- At the Python Anywhere welcome screen, click I want to make a web app.
- Click the Web tab.
- Click Add a new web app
- Click Next in the wizard.
- Click Flask.
- Edit the text in the Path box to
/home/[your user name]/app.py
- Click Next. Wait while the app is created. This may take a minute.
- Click the Files tab.
- Click the edit button next to app.py
- Replace the contents of their app.py file with yours, then click Save
- In the Web tab, click Reload
- The app should now be up and running. To test, go to http://yourname.pythonanywhere.com/ and make sure you see "Hello World". Then go to http://yourname.pythonanywhere.com/messages and verify that you see an empty array [] (since no messages have been added yet).
###Demo### http://krushton.pythonanywhere.com