How to Build an Events Database Using the ParseHub API, Python & Flask

How to Build an Events app using Python, Flask and the ParseHub API

Now that you have your data and turned Peek.com into an API by using the ParseHub browser extension, it’s time to build our simple events web app. Your web app will communicate with the ParseHub API to get and display all of the events we scraped in the first part of the tutorial. Check out our full ParseHub API documentation here as well.

Install Python and Flask

We will build the events website using Python  and Flask, a web framework, similar to Rails for Ruby.
To get started, make sure your computer runs Python or download it by following this Python guide. If you are using a Mac, versions of MacOS 10.2 - 10.5 come with Python already installed. If you are using an older Mac version, download the latest python release here. For Windows XP users follow this ActivePython guide.

To install Flask, open up your terminal and install it with the following command line:

sudo pip install Flask

Choose a location on your computer where you want your events app to be saved, create a new folder for it and cd into that folder. I named my application “activities”.
 

mkdir activities
cd activities

Make sure you have your API key and project token from the ParseHub Firefox extension, as mentioned above. You can find your API key under your account page. Click the profile symbol at the top of the extension and select “Account”. And get your project token from the “Settings” tab in your www.peek.com project.  

Fetch the data and display it in your events app

Create a file and rename it movies.py in your activities folder.

The script below imports Flask and the homepage() function fetches the data we scraped from www.peek.com. The data is rendered and displayed using the template in activities.html that we will create next.

Most of the function below was copy and pasted from the ParseHub API Reference. Make sure to replace the {PROJECT_TOKEN} and {API_KEY} with the two keys you found above in your project and ParseHub account.
 

# activities.py

from flask import Flask, render_template
import requests
import json

app = Flask(__name__, template_folder='.')

@app.route('/')
def homepage():
 params = {
   'api_key': {API_KEY}',
 }
 r = requests.get(
     'https://www.parsehub.com/api/v2/projects/{PROJECT_TOKEN}/last_ready_run/data',
     params=params)
 return render_template('activities.html', locations=json.loads(r.text)['locations'])

if __name__ == '__main__':
 app.run(debug=True)

Let’s create our movies template and get the app to display all of the cities, the categories for each city and the activities for each category. Create a file and rename it activities.html in your activities folder.
 

<!-- activities.html -->

<html>
    <body>
     <h1>Activities</h1>
     {% for city in locations %}
         <h2>{{city["city"]}}</h2>
         {% for category in city["categories"] %}
            <h3>{{category["category"]}}</h3>
             {% for activity in category["activities"] %}
                 <h4>{{activity["name"]}}</h4>
                 <p>{{activity["price"]}}</p>
             {% endfor %}
         </div>
         {% endfor %}
     {% endfor %}
    </body>
</html>

In your terminal enter:

python activities.py

Now visit http://localhost:5000 in your browser to see the website in action.

Of course you can go ahead and make your app look much nicer with a framework like bootstrap.
web scraping parsehub tutorial
You can follow this Python tutorial to learn how to add caching, run your project on a schedule to get a refresh of data whenever you like, and use webhooks.

ParseHub also has packages available in Node.js, PHP and Python built by the developer community.

If you are trying to build something similar or want to get even more fancy, contact us at any time!

 

Be sure to read the next Application Development article: How To Leverage Ruby's Functional Programming Capability