In this Python Firebase article iam going to show you Firebase SDK Integration with Realtime Database.
With the Admin SDK, you can read and write Realtime Database data with full admin privileges, or with finer-grained limited privileges. also Admin SDK lets you interact with Firebase from privileged environments to perform actions like:
You can check the complete video tutorial for this article at the end.
Before this i had an article on Python Firebase using wrapper libraries.
1: Python Firebase Full Course For Beginners
You can check Firebase Admin SDK features that are available for different programming languages in this link.
To use the Firebase Admin SDKs, you’ll need the following:
OK so after creation of your project in Firebase Console, you need to enable Realtime Database in your project.
after that for integration of Firebase SDK you need to install the SDK for the language of your choice. because we are using python, so we do the process for python programming language. you can easily install Firebase Admin SDK with pip like this.
pip install firebase-admin
To authenticate a service account and authorize it to access Firebase services, you must generate a private key file in JSON format.
and add that json file to your working directory.
Before you can access the Firebase Realtime Database from a server using the Firebase Admin SDK, you must authenticate your server with Firebase. When you authenticate a server, rather than sign in with a user account’s credentials as you would in a client app.
When you initialize the Firebase Admin SDK with the credentials for a service account with the Editor role on your Firebase project, that instance has complete read and write access to your project’s Realtime Database.
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
# Fetch the service account key JSON file contents
cred = credentials.Certificate('firebase-sdk.json')
# Initialize the app with a service account, granting admin privileges
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://testpro-72243.firebaseio.com/'
})
So in the above code this is for fetching the service account key json file. and make that you have downloaded the key and added that to your working directory.
cred = credentials.Certificate('firebase-sdk.json')
Because we are working with Realtime Database, so after enabling Reatime Database you need to add the url of the database.
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://url of the realtime database/'
})
Now the setup is completed we need to add some data to our realtime database using this code.
ref = db.reference('/')
ref.set({
'Employee':
{
'emp1': {
'name': 'Parwiz',
'lname': "Forogh",
'age': 24,
},
'emp2': {
'name': 'John',
'lname': 'Doe',
'age': 20,
}
}
})
You can use set for adding data to your realtime database
For updating you need to add this code
ref = db.reference('Employee')
box_ref = ref.child('emp1')
box_ref.update({
'name': 'updated name'
})
If you want to work with multiple path update, you can use this code.
ref = db.reference('Employee')
ref.update({
'emp1/lname': 'updated lname',
'emp2/lname': 'updated lname'
})
There is also another way that you can add data, using this way you can create key for your data, in the first way that we have used for adding data, there is no key for our data.
ref = db.reference('Employee2')
# Generate a reference and add some data using push()
emp_ref = ref.push({
'name': 'Bob',
'lname': "Logan",
'age': 26
})
# Get the unique key generated
emp_key = emp_ref.key
print(emp_key)
You can retrieve the data easily using this code.
ref = db.reference('Employee')
print(ref.get())
This is the complete code for this article
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
# Fetch the service account key JSON file
cred = credentials.Certificate('firebase-sdk.json')
# Initialize the app with a service account
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://Firebase Realtime Database URL/'
})
ref = db.reference('/')
ref.set({
'Employee':
{
'emp1': {
'name': 'Parwiz',
'lname': "Forogh",
'age': 24,
},
'emp2': {
'name': 'John',
'lname': 'Doe',
'age': 20,
}
}
})
'''
#updating data
ref = db.reference('Employee')
box_ref = ref.child('emp1')
box_ref.update({
'name': 'updated name'
})
'''
#working with mullti path update
'''
ref = db.reference('Employee')
ref.update({
'emp1/lname': 'updated lname',
'emp2/lname': 'updated lname'
})
'''
#adding data in second way you can create the key
'''
ref = db.reference('Employee2')
# Generate a reference to a location
emp_ref = ref.push({
'name': 'Bob',
'lname': "Logan",
'age': 26
})
# Get the unique key generated
emp_key = emp_ref.key
print(emp_key)
'''
#retreiving data
'''
ref = db.reference('Employee')
print(ref.get())
'''
#python #firebase #databases