Firebase with Pyhon: the free Google database

Spread the love

Create an account on Firebase and a project

Once you created an account on firebase, you can create your project.

This is the page of your database with all its children.

To use it with Python, you need to install some modules. With Python, usinf Firebase becomes really easy.

Create an account on firebase

After that create a database. I will add some more information in the future about this.

We will need, then, to install some modules:

  • firebase
  • python_jwt
  • gcloud
  • sseclient
  • pycriptodome
  • request_toolbelt

Quite a lot of module, right? At the end, you should be ready to start.

Put some data in the database

I assume you did create the database. Now let’s connect to it. First we need to install firebase.

Then install python_jwt

and then gcloud

Then sseclient

and pycryptodome

and requests_toolbelt

… we’re done… now it works

Import firebase

First we import firebase

Connect to the database

Then we connect to our database (put your database name instead of your_db_name:

Add a record and a value to a child

Let’s make it the most simple that we can

The “book” is the child of the database. “Pinocchio” is the name of a record and its values are in a dictionary that has 2 keys “author” and “Edition”. You can also use numbers as the value, or strings, or lists or dictionaries. Take a look at the video below to see how to insert values easily. In the next post we will see how to read the values from the database instead of writing them.

How to retrieve the data

To read, or retrieve, the data that you just stored, you need to use the get function that takes 2 argument, the address of the database (“https://*****”) and the name of the child (“books”):

The output will be this:

Delete every data in the database

In case you want to delete all the data (!), you can use this code:

A quick look to the app

Tkinter to create a GUI for firebase

This little script let us look to the records into firebase. We need to add a way to add the records. Let’s see the code until now:

The app..

Now we need the code to add the records.

How to get the data in javascript

To get the data in an html page you need some code like this (you nedd the confid data from your firebase project):

You will get the var config data from your project. Then you will nedd to add the child you want to read the data from in:

my_data_to_read is the child.

The following code will read the data and show them in the div “board”: