How to build a website application using Python
After you have gone through some Python beginners tutorials, such as this, this and this you may want to begin learning how to create programs that works with various web services.
I will split this article into two parts.
In the first part, we will focus on signing up for an API key, and use it to see what data we can use. After that we will create Python program to display that data on screen.
In the second part, we will take a look on how we use this program to create a full featured web application built in Flask.
The way we will create our web application is by using something that is called an "API" which is short for Application programming interface.
Most Internet companies (like Spotify, Lastfm) today, provides an API to the public that you can use to build your own web application that are powered by its services.
What is an API?
An API is a protocol and its basically a set of programming instructions and standards for accessing a web-based software application or web tool.
There is also something called a wrapper, which you can think of as an API client. They are commonly used to wrap the API into easy to use functions by doing the API calls itself.
Where to start?
First off, you need to think of what for web application you want to build. To get some ideas, you can use http://pythonapi.com/ which lists a whole page of available Python API's.
I'm going to build a web application for a music service, or more specific a service where visitors can search for an artist (or band) and whether that artist (or band) are still on tour.
If that is true, I want to display a text to say:
This artist is still on Tour
and if its false I want to display something user friendly like:
We’re sorry, but we couldn’t find any events. Probably not on tour
If you want to jump head to see what we are going to build, take a look at http://searchconcert.com
After googling around, I found that Songkick.com seems to provide what I need for this. http://www.songkick.com/developer.
"The Songkick api gives you easy access to the biggest live music database in the world: over 3 million upcoming and past concerts and over 100,000 setlists… and growing every day! Easily add concerts to your website or application. "
Perfect! Now, let's Apply for an API key.
An API key basically contains some form of secret token that companies uses in order to identify the origin of the request. There are often some rules which you need to bear in mind before you get started such as rate limit and whether it can be used for commercial or non-commercial.
Make sure to read them.
After you have filled in the form, you should get an API key sent to the given email address.
Nice. We have taken our first step of creating our web application.
By now, you should have:
- Picked a service to use
- API Key.
Interacting with the API
We need to go back to the website to check what for API Requests that we want to use. So let's look it up at http://www.songkick.com/developer
Since I want build a web application where visitors should be able to find if the artist / band they search for is still on Tour, we will use the Artist Search Request.
Most web services (like Songkick.com) often provides some example data on how to their API.
An example for our Artist API request looks like this:
The response from that URL will look like this:
There is also an example of how to use Pagination, but we will skip that for now.
When we want to interact with an API in Python (like accessing web services), we get the responses in a form called JSON.
To interact with JSON, we can use the json or simplejson modules.
JSON (JavaScript Object Notation) is a compact, text based format for computers to exchange data and is once loaded into Python just like a dictionary.
The second module we will need in our code is the Requests module.
Requests allow you to send HTTP/1.1 requests. You can add headers, form data, multipart files, and parameters with simple Python dictionaries, and access the response data in the same way.
Continue reading on: http://superduper.me/how-to-build-a-website-application-using-python/