Fun with #Firebase and #Python

Recently I’ve been working on building a little robot for my Google Cloud Next talk about Geolocation.  The robot is built around a Raspberry Pi board, and he will be roving around, autonomously figuring his way around whatever space he’s put it, figuring out his latitude and longitude — without using GPS — and then storing the results in Firebase. Being linux-based, I decided to use Python for the Firebase connectivity, and as such have learned a bit about how to use this super simple language and this super simple backend to connect to the firebase backend-as-a-service.

So I’ve written this little tutorial to show you how to get started with Firebase to build something in Python!

Step 1. Create the Project

First off, go to the Firebase console and create a new project. I called mine ‘PythonBlog’

This sets up a project with the various keys that you need to access all the Firebase goodies, from the realtime database to authentication, messaging, notifications and much more. In this post I’ll look at a simple scenario with the realtime database.

Once you’ve done this, go to the realtime database tab on the left hand side, and select ‘Database’. Once you’ve done that, select ‘Rules’ at the top of the right hand area, and change the rules to read as below:

This allows everyone to read and write from your db. Eventually you should change this to lock down who can read and/or write from your db, but for now, just leave it as this to make it easy for you to test.

Next, go to the Data tab, and you’ll see the url of your database listed at the top. Be sure to take note of this as it’s important for later!

Step 2. Set up Python for Firebase

There are a number of different libraries available for Python on Firebase, but I chose to use python-firebase, which I find really easy to use. It’s installed using pip, which if you don’t have it, you can get it here.

To install python-firebase, you just need 2 commands:

Once these are installed, you’re good to start programming with Firebase in Python!

Step 3. Using Python to store data in Firebase

Now that you have Python working for Firebase, it’s really easy to write to and read from the real-time database. Here’s an example where I create a simple JSON item called data, containing latitude and longitude values, and then write that to Firebase:

Note the parameter to FirebaseApplication — this is the url of the database that you took note of in step 1.

You can run this in python with:

If this runs without error, you should then see the data stored in your Firebase realtime database:

As you can see the ‘data’ key is from the fourth line of code where we defined that‘/data’, data), with the key value being automatically generated for you.

Step 4. Using Python to read data from Firebase

In the previous step you saw how to store data in Firebase, let’s now look at how to retrieve data. I’ve added a few latitudes and longitudes to the database, as you can see here:

To read this from the database, it’s simply a matter of doing a get command, like this:

And when I run this app, I’ll see something like this:

So, as you can see, there’s a JSON structure with the key having sub values for latitude and longitude. In the next step you’ll see how to plot these on a map!

Step 5. Plot the values on a map

To plot the values on a map, I used the gmplot library for Python. I really like this one because it’s super simple to use, and it generates HTML containing a Google Map with your required pins, heatmaps etc. Pretty cool stuff.

So, I amended the above Python code like this to incorporate gmplot:

The data that came back from the firebase.get command had a series of items — each being the Firebase-generated key. So, if I iterate through these items, taking the k,v (key and value), then I can generate lists of latitudes and longitudes from the v[‘latitude’] and v[‘longitude’] respectively.

As the gmplot library takes lists like this in the ‘scatter’ method, I can then plot the pins. Note that when you create a map you also have to specify it’s center, so I just take the first item in the list. The ’16’ parameter is the zoom level for the map. To learn more about the maps JavaScript APIs check out the developer documentation.

Do note that the generated map from gmplot might need a bit of tweaking if you’re going to put it online — at least using an API KEY for it to render.

Here’s my one, from the above co-ordinates with the API KEY added: