How to write data to Firebase Real Time Database using Python

How to Get Started with Real Time Database in Google Firebase using Python

This post may contain affiliate links. Please read our disclosure for more info.

As many of you might know Google Firebase is a platform developed by Alphabet (previously Google) Inc. for creating mobile and web applications. Originally Firebase was an independent company founded in 2011. In 2014, Alphabet acquired the platform and it is now their flagship offering for cloud based app and website development.

Firebase offers lots of amazing development products such as databases e.g. firestore and real-time databases. In this post, we will deep dive into real time database in google firebase using python.

How to setup a Google Firebase Account

First thing first, before you can start using google’s firebase account, you must have an account setup. The good news is that you can experiment with lots of features and products without paying a dime. It’s completely free.

So, head over to https://console.firebase.google.com/ and create a free account using your gmail id.

Once the account is setup, next login to firebase console and click on New Project

How to load data in firebase using Python

How to save data to Firebase Real-Time Database using Python

Now lets get into interesting stuff. Click on the Add Project button and it will ask you to name your project.

For now lets name your project as – Python-Data-Loader

How to write data to Firebase Real Time Database using Python

On next step, it will ask if you would like to enable Google Analytics for your firebase project. Since we are just writing a non-production application in python, it doesn’t require us to track user activities. So, for now, I will recommend to disable it and click on Create Project.

How to write data to Firebase Real Time Database using Python

Now google firebase console will create the project including real-time database which we need in the background for us. We need to wait for it to finish before continuing.

Congratulations, your first firebase application is setup in google firebase.

How to write data to Firebase Real Time Database using Python

Now click on Continue.

You will be greeted with following screen which provides you access to all the cool products which google firebase expose to starter users like us.

Notice, you are auto assigned Spark Plan which is free account.

How to write data to Firebase Real Time Database using Python

How to Create Real-Time Database in Google Firebase

Now let’s get to the business. Next click on the Real Time Database from left panel and then click on Create Database.

How to write data to Firebase Real Time Database using Python

I will select the location for our Real Time Database as United States (us-central1).

How to write data to Firebase Real Time Database using Python

In next screen, you can choose either Locked Mode or Test Mode.

In Locked Mode, your data will be private and only we will be able to access it since we are the only ones having setting it up. Later I will also show you how to set the access rules so you can yourself read/write to this database.

In Test Mode, your database isn’t private and will be open to all which isn’t safer at all for production applications but may be fine for our learning here.

I will go ahead with Locked Mode for now.

How to write data to Firebase Real Time Database using Python

Click on Enable and your database will be ready for use.

One last thing we need to do before jumping to writing python application.

You might also like:   Ethical Considerations in AI Development: Understanding Bias, Privacy, Transparency, and Governance

How to set access rules for Real Time Database in Google Firebase

While still under newly created database, select Rules tab and add following code.

{
  "rules": {
    ".read": "auth.uid != null",
    ".write": "auth.uid != null"
}

This will ensure that our Python application can read and write to this database.

Python Application to Write Data to Google Firebase

Next we will develop a python application to read and write data to Real Time Database in Google Firebase.

You can choose any IDE that you prefer to develop this code, but I prefer PyCharm IDE which I am going to use here.

Now fire up your PyCharm IDE and click on File -> New Project.

Add project details as follows:

python application to write data to real time database in google firebase

You will need to have Anaconda installed to create virtual environment before above step.

Next install firebase plugin we need from terminal as follows:

pip install firebase_admin

This will install all the dependencies you will need in order to use firebase_admin python package. You should get output similar to this

(python-data-loader) ➜  python-data-loader pip install firebase_admin
Collecting firebase_admin
  Downloading firebase_admin-5.2.0-py3-none-any.whl (115 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 115.5/115.5 kB 2.1 MB/s eta 0:00:00
Collecting google-api-core[grpc]<3.0.0dev,>=1.22.1
  Downloading google_api_core-2.8.2-py3-none-any.whl (114 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 114.6/114.6 kB 3.7 MB/s eta 0:00:00
Collecting google-cloud-storage>=1.37.1
  Downloading google_cloud_storage-2.4.0-py2.py3-none-any.whl (106 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 107.0/107.0 kB 2.8 MB/s eta 0:00:00
Collecting google-api-python-client>=1.7.8
  Downloading google_api_python_client-2.55.0-py2.py3-none-any.whl (8.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.8/8.8 MB 19.0 MB/s eta 0:00:00
Collecting google-cloud-firestore>=2.1.0
  Downloading google_cloud_firestore-2.6.0-py2.py3-none-any.whl (244 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 244.3/244.3 kB 7.8 MB/s eta 0:00:00
Collecting cachecontrol>=0.12.6
  Downloading CacheControl-0.12.11-py2.py3-none-any.whl (21 kB)
Collecting msgpack>=0.5.2
  Downloading msgpack-1.0.4-cp310-cp310-macosx_10_9_x86_64.whl (74 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 74.9/74.9 kB 2.1 MB/s eta 0:00:00
Collecting requests
  Downloading requests-2.28.1-py3-none-any.whl (62 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.8/62.8 kB 2.0 MB/s eta 0:00:00
Collecting protobuf<5.0.0dev,>=3.15.0
  Downloading protobuf-4.21.4-cp37-abi3-macosx_10_9_universal2.whl (484 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 484.1/484.1 kB 10.1 MB/s eta 0:00:00
Collecting google-auth<3.0dev,>=1.25.0
  Downloading google_auth-2.9.1-py2.py3-none-any.whl (167 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 167.8/167.8 kB 5.4 MB/s eta 0:00:00
Collecting googleapis-common-protos<2.0dev,>=1.56.2
  Downloading googleapis_common_protos-1.56.4-py2.py3-none-any.whl (211 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 211.7/211.7 kB 6.0 MB/s eta 0:00:00
Collecting grpcio<2.0dev,>=1.33.2
  Downloading grpcio-1.48.0-cp310-cp310-macosx_10_10_x86_64.whl (4.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.2/4.2 MB 18.0 MB/s eta 0:00:00
Collecting grpcio-status<2.0dev,>=1.33.2
  Downloading grpcio_status-1.48.0-py3-none-any.whl (14 kB)
Collecting httplib2<1dev,>=0.15.0
  Downloading httplib2-0.20.4-py3-none-any.whl (96 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 96.6/96.6 kB 3.4 MB/s eta 0:00:00
Collecting google-auth-httplib2>=0.1.0
  Downloading google_auth_httplib2-0.1.0-py2.py3-none-any.whl (9.3 kB)
Collecting uritemplate<5,>=3.0.1
  Downloading uritemplate-4.1.1-py2.py3-none-any.whl (10 kB)
Collecting protobuf<5.0.0dev,>=3.15.0
  Downloading protobuf-3.20.1-cp310-cp310-macosx_10_9_universal2.whl (962 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 962.3/962.3 kB 13.8 MB/s eta 0:00:00
Collecting google-cloud-core<3.0.0dev,>=1.4.1
  Downloading google_cloud_core-2.3.2-py2.py3-none-any.whl (29 kB)
Collecting proto-plus<2.0.0dev,>=1.20.5
  Downloading proto_plus-1.20.6-py3-none-any.whl (46 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 46.4/46.4 kB 1.4 MB/s eta 0:00:00
Collecting google-resumable-media>=2.3.2
  Downloading google_resumable_media-2.3.3-py2.py3-none-any.whl (76 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 76.9/76.9 kB 2.5 MB/s eta 0:00:00
Collecting pyasn1-modules>=0.2.1
  Downloading pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 155.3/155.3 kB 5.3 MB/s eta 0:00:00
Collecting six>=1.9.0
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting rsa<5,>=3.1.4
  Downloading rsa-4.9-py3-none-any.whl (34 kB)
Collecting cachetools<6.0,>=2.0.0
  Downloading cachetools-5.2.0-py3-none-any.whl (9.3 kB)
Collecting google-crc32c<2.0dev,>=1.0
  Downloading google_crc32c-1.3.0-cp310-cp310-macosx_10_9_x86_64.whl (30 kB)
Collecting pyparsing!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,<4,>=2.4.2
  Downloading pyparsing-3.0.9-py3-none-any.whl (98 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 98.3/98.3 kB 3.0 MB/s eta 0:00:00
Collecting charset-normalizer<3,>=2
  Downloading charset_normalizer-2.1.0-py3-none-any.whl (39 kB)
Requirement already satisfied: certifi>=2017.4.17 in /Users/yottabyte/.conda/envs/python-data-loader/lib/python3.10/site-packages (from requests->cachecontrol>=0.12.6->firebase_admin) (2022.6.15)
Collecting idna<4,>=2.5
  Downloading idna-3.3-py3-none-any.whl (61 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.2/61.2 kB 1.9 MB/s eta 0:00:00
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.11-py2.py3-none-any.whl (139 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 139.9/139.9 kB 4.4 MB/s eta 0:00:00
Collecting pyasn1<0.5.0,>=0.4.6
  Downloading pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 77.1/77.1 kB 2.7 MB/s eta 0:00:00
Installing collected packages: pyasn1, msgpack, urllib3, uritemplate, six, rsa, pyparsing, pyasn1-modules, protobuf, idna, google-crc32c, charset-normalizer, cachetools, requests, proto-plus, httplib2, grpcio, googleapis-common-protos, google-resumable-media, google-auth, grpcio-status, google-auth-httplib2, google-api-core, cachecontrol, google-cloud-core, google-api-python-client, google-cloud-storage, google-cloud-firestore, firebase_admin
Successfully installed cachecontrol-0.12.11 cachetools-5.2.0 charset-normalizer-2.1.0 firebase_admin-5.2.0 google-api-core-2.8.2 google-api-python-client-2.55.0 google-auth-2.9.1 google-auth-httplib2-0.1.0 google-cloud-core-2.3.2 google-cloud-firestore-2.6.0 google-cloud-storage-2.4.0 google-crc32c-1.3.0 google-resumable-media-2.3.3 googleapis-common-protos-1.56.4 grpcio-1.48.0 grpcio-status-1.48.0 httplib2-0.20.4 idna-3.3 msgpack-1.0.4 proto-plus-1.20.6 protobuf-3.20.1 pyasn1-0.4.8 pyasn1-modules-0.2.8 pyparsing-3.0.9 requests-2.28.1 rsa-4.9 six-1.16.0 uritemplate-4.1.1 urllib3-1.26.11
(python-data-loader) ➜  python-data-loader 

If you have reached this point without any issues, it’s great. Now let’s write some python code.

You might also like:   A Comprehensive Guide to Python Libraries for AI Development: PyTorch, Scikit-learn, and NLTK

In PyCharm, create a file main.py if not already generated and add following code:

import firebase_admin
from firebase_admin import credentials, db
import json
from typing import Any


def _authenticate():
    cred = credentials.Certificate("local/cred.json")
    firebase_admin.initialize_app(cred, {
        'databaseURL': '<path/to/db/in/firebase/console/'
    })

You will also need to generate a credential file from google firebase so your python application can connect with real-time database and read/write data.

How to Create Credential file in Google Firebase

On left panel, next to Project Overview click on gear icon and select Project Settings.

How to generate credential json in google firebase

Choose on Python for language and click on Generate new private key button at the bottom.

How to generate credential for real time database in google firebase

This will generate a json file with credentials in it.

Move this file to PyCharm under a new folder local/ and name this file as cred.json

Setup Sample Data for Loading into Real Time Database in Google Firebase

We need some data to be copied over to Real Time Database in Google Firebase. Since this data has to be in json format, you can create a file under data/data.json as

{
	"User-1":
	{
		"Name": "John Peter",
		"Age": 34,
		"Salary": 100000
	},
	"User-2":
	{
		"Name": "Sam Stevenson",
		"Age": 40,
		"Salary": 120000
	}
}

Next will we will write a python class and some functions to write and later read this data. I will also show you how to update and delete records from real time database in google firebase.

How to read data from Real Time database in Google Firebase

In main.py create a static function as follows:

def read_data():
    with open("data/data.json", "r") as f:
        data = json.load(f)

    return data

This function reads json data saved in data/data.json file and returns it to calling function.

Next let’s write a Python class to connect to remote database and save this data.

How to Connect to Real Time Database in Google Firebase using Python

This is actually very simple. Just write this piece of code to create a python class to connect to real time database in google firebase.

class PythonDataLoader:
    def __init__(self):
        _authenticate()
        self.db = db.reference("/Users")

    def save(self):
        data = read_data()
        self.db.set(data)

if __name__ == '__main__':
    pdl = PythonDataLoader()
    pdl.save()

Here db.reference is used to connect to specific document (remember firebase is a document store) in our database and db.set(data) is used to write the data on real time database here. The argument is a json document which we loaded into data variable before.

This is enough to write our data to remote database. Let’s test this by running our program from play button in PyCharm.

This is what you should see loaded up in Google Firebase -> Real Time Database.

how to save data in real time database on google firebase using python

This is great. Isn’t it?

But I want to take it to next step. Let’s add unique keys to each data record because that’s how production data should be stored as.

You might also like:   Benchmarking in Python: Techniques and Best Practices for Performance Evaluation

How to add unique id to data records in Real Time Database on Google Firebase using Python

To add a unique id to each data record in real time database on google firebase, we can use push() provided for python libraries by firebase. It’s pretty simple to implement.

Let’s add a new function as

    def save_with_uid(self):
        data = read_data()
        for key, value in data.items():
            self.db.push().set(value)

One thing to note here is, we will not pass entire data object in set() but rather iterate over individual records to set only the value. The key or unique id will be provided by firebase itself.

When we run this, you should get following output in real time database

how to set unique id to data records in real time database on google firebase using python

Nice.

How to Read Data Stored on Real Time Database in Google Firebase using Python

Reading database is equally easy. We will use db.get() function for this as follows.

    def load_all(self):
        fetched_data = self.db.get()
        return fetched_data

This will load all the data stored in database. Ofcourse, not a good idea for a large database in a production application.

How to Update Data Stored on Real Time Database in Google Firebase using Python

This is another important operation performed by most of the applications. How can we update just one existing record in database?

Well it’s pretty easy to do as well. For this, we will use db.child(key).update({data element}).

Let’s add this function in our python class:

    def update_a_docid(self, doc_id: str, field: str, value: Any):
        all_data = self.load_all()
        try:
            print(f"before: {all_data[doc_id]}")
            self.db.child(doc_id).update({field: value})
        except KeyError:
            print(f"Error: doc_id: {doc_id} not found")

We can invoke this function as

if __name__ == '__main__':
    pdl = PythonDataLoader()
    pdl.update_a_docid("-N8KAHuzLWq3SD8VOgRJ", "Salary", 150000)

This will update the Salary of one of our data record which has key = “-N8KAHuzLWq3SD8VOgRJ” (make sure to change this to value in your own database).

This is great.

Lastly, let’s look at how to delete a record from our real time database.

How to Delete a Record from Real Time Database in Google Firebase using Python

To delete a record, we first need to local that record in the database and then set it’s data value to be null. Firebase will automatically remove that record once value is set to empty.

WANT TO ADVANCE YOUR CAREER?

Enroll in Master Apache SQOOP complete course today for just $20 (a $200 value)

Only limited seats. Don’t miss this opportunity!!!

 

Mastering Apache Sqoop with Hortonworks Sandbox, Hadoo, Hive & MySQL - DataShark.Academy

Get-Started-20---DataShark.Academy

 

Let’s add this function in our python class

    def delete_user(self, name: str):
        all_data = self.load_all()

        for key, value in all_data.items():
            if value["Name"] == name:
                self.db.child(key).set({})

To invoke this, change the main function as

if __name__ == '__main__':
    pdl = PythonDataLoader()
    pdl.delete_user("John Peter")

When you run the program again, user record belonging to “John Peter” will be deleted.

I hope you enjoyed this lesson and it will help you get onboard with Real Time Database in Google Firebase using Python language.


[jetpack-related-posts]

Leave a Reply

Scroll to top