Skip to content

krushton/flask-api-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

flask-api-example

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:

RouteMethodParametersResult (JSON)
/GETReturns 'hello world'
/messagesGETReturns all messages
/messagesPOSTname=foo&comment=barCreates a new message with the posted values
/searchGETname=fooReturns all messages where name = 'foo'
/messages/1GETGets the message with id=1
/messages/1POSTname=foo&comment=barUpdate the message with id=1
/messages/1DELETEDelete the message with id=1

To deploy a database on the web using this file:

  1. 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.
  2. Create an account at Python Anywhere. The URL for your app will default to username.pythonanywhere.com.
  3. At the Python Anywhere welcome screen, click I want to make a web app.
  4. Click the Web tab.
  5. Click Add a new web app
  6. Click Next in the wizard.
  7. Click Flask.
  8. Edit the text in the Path box to /home/[your user name]/app.py
  9. Click Next. Wait while the app is created. This may take a minute.
  10. Click the Files tab.
  11. Click the edit button next to app.py
  12. Replace the contents of their app.py file with yours, then click Save
  13. In the Web tab, click Reload
  14. 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

About

Example of a basic API for a sqllite3 database

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages