Using Firebase Realtime DB with Python
Photo by https://unsplash.com/@florianolv

Using Firebase Realtime DB with Python

What is Firebase?

Firebase is a Popular Cloud-hosted NoSQL Database developed by Google. This is known as BaaS (Backend as a Service). The Database can be integrated with Web, iOS, Android, Unity platforms. Firebase offers two types of Database services, one is Realtime DB which is the original database since the inception and now we also have Cloud Firestone which has more rich features and highly scalable.

Python is a general-purpose dynamically typed programming language that is used across AI, Machine Learning, Web Apps, Data Science etc.

My Development Tool of Choice is Visual Studio code which I will be using to code and demonstrate this example. Here I will be first creating the Firebase DB and then using python will connect to it.

  1. Create an account in Firebase, go to Firebase Console and create a new Project.
  2. The next step is to create Realtime Database, while creating the database select any data centre of your choice (US, Belgium, Singapore). I have also imported a Sample JSON into the Firebase Database so that we can do a get data Call from our python code to view the results. While creating the Database I had selected Test Mode hence it will bypass authentication while inserting or extracting data from Python. Here is how the database should look like
No alt text provided for this image

3. Now, will write the python code using the Firebase helper library. https://github.com/ozgur/python-firebase. We have helper libraries for other programming languages documented here.

4. Create a Directory in your local system and install the below python packages. I would recommend using a virtual env and then install these packages into it.

pip install requests
pip install python-firebase

5. In the Python Directory create two files one for GetData and the other for posting the data back in firebase.

In the getdata.py copy this below code and replace the URL with your Firebase Database URL

from firebase import firebase
fb_app = firebase.FirebaseApplication('https://YOUR_DATABASE_URL.asia-southeast1.firebasedatabase.app/', None)
result = fb_app.get('/users', None)
print(result)

Now when this code is executed you should be getting the output like this

No alt text provided for this image

For Inserting the data into Firebase, we will use below code

from firebase import firebase
fb_app = firebase.FirebaseApplication(
    'https://YOUR_DATABASE.asia-southeast1.firebasedatabase.app/', None)
new_user_data = {'Name': 'CHARLIE',
        'DOB': '27 May 1980'
        }


result = fb_app.post('/users', new_user_data, {'print': 'pretty'})
print(result)

And when this code is executed the output should look like this

No alt text provided for this image

Now, when we go back to Firebase Database we will see a new data node getting added into the database.

No alt text provided for this image

Hope this gives a basic understanding of how Cloud-Hosted Firebase Database can be used with our general programming languages like C#, Python, Java, Node.js etc.

Firebase Official Documentation

?

Follow me on Tumblr Medium

Consider updating your content. Something 2 years old is ancient.

回复
Qu?c Nguy?n ?ình

A Software/Data Engineer

2 年

So do we need to close the connection to the realtime database from our code? :((

回复
Bilal Imran

AI Developer | Full Stack Developer | Node JS | Project Manager | Python | Next JS | Server Management | WordPress Theme & Plugin

3 年

Sir was stuck in a project. Love your article. It really helped me to get going. Chandan Singh

Vinni John

HubSpot Project Manager | CRM

3 年

Good read!

Sahil Deshpande

Senior Cyber Defence Analyst at Tesserent

3 年

Great write up Chandan Singh ??

要查看或添加评论,请登录

Chandan Singh的更多文章

  • JavaScript's Evolution: Shaping the Future of Web Development

    JavaScript's Evolution: Shaping the Future of Web Development

    JavaScript, the ubiquitous language of the web, has come a long way since its inception. It's no longer just the tool…

  • Getting Started with Angular.js

    Getting Started with Angular.js

    If you’re looking to get started with Angular.js, you’ve come to the right place.

    2 条评论
  • What is Log4j vulnerability?

    What is Log4j vulnerability?

    The Log4j Vulnerability was first reported by Alibaba Cloud Security Team late in November 2021 and then was alerted to…

  • Blockchain Architecture

    Blockchain Architecture

    What is Blockchain? Blockchain is a decentralized, Immutable block of Digital ledger wherein each block contains the…

    6 条评论

社区洞察

其他会员也浏览了