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.
- Create an account in Firebase, go to Firebase Console and create a new Project.
- 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
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
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
Now, when we go back to Firebase Database we will see a new data node getting added into the database.
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.
A Software/Data Engineer
2 年So do we need to close the connection to the realtime database from our code? :((
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
HubSpot Project Manager | CRM
3 年Good read!
Senior Cyber Defence Analyst at Tesserent
3 年Great write up Chandan Singh ??