RoadTripr: My first hackathon – The Flask App Upgrade (Part 2)

The project is sitting in a git repo, it doesn’t really work fully, and no one really wants to touch it again – but I’m bored and I wanna try out flask.

This article is going to be shorter than I expected – probably didn’t need to do it in parts but hey, you got ’em now…

This post is just going to be a breakdown of the flask app. If you want some exposition go see part one for goodness sake.

First things first is to figure out how to actually make a flask app.

You need to install flask into pip and then create a simple app that launches a flask server. This code can be found everywhere in tutorials online and this isn’t a tutorial so I wont be covering that. Once I had that squared away I needed to tackle this Spotify problem first.

After some research and playing about I found Spotipy. A wrapper for the Spotify API written for Python. Yes the documentation is somewhat out of date but it damn well works and it’s easy. Yes, that’s right using the Spotify API IS EASY!!! This was quite a moment for me, getting over the Spotify hurdle so quickly was wonderful – besides, the raw API has always left with a distaste for CURL.

The first big thing is transferring the data from the form to the final page. In Flask each page is in it’s own subroutine and it turns out it’s easier than it looks to transfer data between them. You must create a secret key in the top of your program, then you can simply use code below to save the url parameters into session variables. These act like global variables, more on that below.

Then it was a job of throwing some data into google maps.

Getting a travel time from a couple tuples that contained co-ordinates was no problem. Getting those co-ordinates, however, slightly harder. When you search for a location on google maps it returns an area denoted by 2 sets of co-ordinates. These form a square which you can simply calculate the centre of and throw that into the travel time calculator.

We then set up a playlist and get the user ID for the logged in user and the playlist ID for later.

You’ll note the used of the phrase session[‘playlistname’], this is where the data about what the user wants to call the playlist lives. When you want to preserve flask variable between pages you cannot use a global variable – you must use a session variable. I highly recommend reading up on these if you don’t know about them and want to get into flask. It might just save a lot of frustration and time.

Then we compile a list of track IDs based off the parameters set in the other page.

This is it. The last step to having a working application. Adding the tracks to the playlist and its one line!

And, you’re done. Add a bit of pow here and pazazz there to the front end and you have a finished app.

I didn’t cover getting oAuth tokens in this as I based the app around the code found here which is fantastic for this. I didn’t use the wrappers oAuth method because frankly it’s terrible and not built for Flask.

So that’s it, the end of RoadTripr. Links below.

The website:

The Git Repo:

One thought on “RoadTripr: My first hackathon – The Flask App Upgrade (Part 2)”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.