Setting up your first Web Application

Hey you! How would you like to set up your first web application in a few hours?

The thought of setting up a Web application / Web Service seems to be a taunting task for those who have never done it before or for those who have done it before using complicated python MVC frameworks which take forever to setup . I hope to help aspiring Full-stack developers, Students, Entrepreneurs or those folks who just want to setup a RESTful web-service as quickly as possible using web.py

Before we start, what do you need to know?

  1. A working knowledge of python : be able to write classes and functions in Python and the various data structures in python (mainly Dictionaries, Lists, Tuples and Sets) — 1–3 hours to learn the fundamentals.
  2. An understanding of HTTP requests (GET and POST for this exercise) : the below video covers everything you need to know and more. — 30–45 mins to learn and understand

3. Understanding of what a Database is (and why we need one): If you want to brush up on this, watch the below video — 5–10 mins to learn and understand.

4. Ability to differentiate between Client Facing and Server Side technologies : A simple example, a user logs into www.yourwebsite.com, your browser makes a GET request to the server hosting www.yourwebsite.com and that server returns the Html, Css and Javascript to browser. These components render on the user’s machine and hence fall under Client Facing technology.

Now assume the user wants to publish an review on www.yourwebsite.com and there is a form where he or she can enter a title and a body to the article and press a button when ready to submit. When the user presses submit after filling up the title and body a POST request is made to the server, the Python function that handles the POST request runs and does something. The technologies that run on the server side are known as Server Side technologies.


Overview of the steps

We follow the following steps to reach our goal of building your first web application, which uses Databases for storage:

  1. Get a working web service / web application up: This is what will handle the HTTP requests users, browsers, scripts send and will also contain the logic of each API call within it. This will also contain the Templates that will be used to render the website ( don’t worry it’s easy )
  2. Set up a Database: Every web application / web service needs a Database to be able to store long-term data, i.e data that doesn’t get lost if the web app is restarted (ex: usernames and passwords, etc). We’ll be using Postgres.
  3. Connect the Database to your web service / web application: Finally we need to make the database and web app talk.

Let’s get started!

Step 1: Setting up web.py

Follow the below steps:

  1. Open terminal and run sudo easy_install web.py
  2. Once this is done, follow this tutorial: https://www.youtube.com/watch?v=GfyinI4qJHQ
  3. Good work! Now, set up the logging module. Using Logging over print is a good practice. https://youtu.be/OyZkXsgv5qk
  4. Now you should have a basic working web-service. Let’s test the POST and GET requests using cURL on the terminal. Follow the first few mins of https://youtu.be/WxUVU0b95Oc and then read through this stack-over-flow doc http://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest.
  5. If both POST and GET work, you have the basic web application up and running at http://localhost:8080
  6. In your POST function, use data = json.loads(web.data()). to access the incoming payload from a curl command like: curl -H “Content-Type: application/json” -X GET -d ‘{“name”:”bla”,”age”:100}’ http://localhost:8080/

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Step 2: Setting up a Postgres DB

  1. Follow this video https://www.youtube.com/watch?v=hZHzRnEB6sc
  2. Great now you should have Postgres set up at localhost too!
  3. Create a table in your new DB by running this : CREATE TABLE article ( user_id bigserial primary key, last_name text NOT NULL, first_name text NOT NULL, date_added timestamp default NULL );
  4. Now add some data into it: INSERT INTO users (last_name, first_name,date_added) VALUES (‘D2’, ‘R2’);

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Step 3: Connecting your DB and web.py

  1. This is a fairly simple task. Read through this http://webpy.readthedocs.io/en/latest/db.html and you should be set!

Troubleshooting:

  1. if things do not work, then try double checking the entered credentials
  2. if you get the ‘unable to import psycopg2’ error then run pip install psycopg2
  3. if pip is not installed, like it was on my macbook air, then run sudo easy_install pip

When all the above works, go open a bottle of beer or grab a cake or do whatever you do to celebrate. Congratulations you just built a basic web application. I am proud of you :,)

Go here to compare your code: