Firebase CRUD Operations Using Python

Create a Firebase Project
 
The first step is to create a project in the Firebase console.
 
Note
 
You need to have been logged in with a Google account which can use the Firebase console and follow the below steps to create a Firebase database.
  1. Open the Firebase console.
  2. Click "CREATE NEW PROJECT".
  3. The "Create a project" window opens.
     
    Enter the following information and click "CREATE PROJECT".
    • Project name
       
      Enter python-sample. This is the project name of your app in the Firebase console.
       
    • Country/region
       
      Select your location. Hover on the "?" mark to display the description.
       
      Python
  1. Now, go to Database -> Select Realtime Database
     
    Python 
Then, select the security rule “Start in test mode” so everyone can access it without any credentials.
 
Python
 
Click on the ENABLE button.
 
Now, you will set the following database UI:
 
Python
 
Install Python-Firebase library to access Firebase database using Python. (Firebase library is available here https://github.com/ozgur/python-firebase)
 
You can use the following commands to install python-firebase library,
  • sudo pip install requests
  • sudo pip install python-firebase
Now, we are going to write Python script
 
Insert record in firebase
  1.  from firebase import firebase  
  2. firebase = firebase.FirebaseApplication('https://xxxxx.firebaseio.com/', None)  
  3. data =  { 'Name''Vivek',  
  4.           'RollNo': 1,  
  5.           'Percentage': 76.02  
  6.           }  
  7. result = firebase.post('/python-sample-ed7f7/Students/',data)  
  8. print(result)  
Import library of Firebase in Python script. Then, create database connection using the below command.
  1. firebase = firebase.FirebaseApplication('https://xxxxx.firebaseio.com/', None)  
FirebaseApplication takes two parameters; one is URL of database and second is authentication details for database. But we don’t have any rules so we are passing None.
Post method will insert the record into the database and will return the name of entry; for example,
  1. {'name''-LAgstkF0DT5l0IRucvm'}  
After inserting some more records into the database, you can see them in Firebase console as below.
 
Python
 
Get records from Firebase
Now, we need to read all inserted records using get command.
  1. from firebase import firebase  
  2. firebase = firebase.FirebaseApplication('https://xxxx.firebaseio.com/', None)  
  3. result = firebase.get('/python-sample-ed7f7/Students/''')  
  4. print(result)  
Now, you can see the result in JSON format as below.
  1. {  
  2.     '-LAgstkF0DT5l0IRucvm': {  
  3.         'Percentage': 76.02,  
  4.         'Name''Vivek',  
  5.         'RollNo': 1  
  6.     },  
  7.     '-LAgtLxF8uPedUr0z9gE': {  
  8.         'Percentage': 65,  
  9.         'Name''Akshay',  
  10.         'RollNo': 3  
  11.     },  
  12.     '-LAgt5rGRlPovwNhOsWK': {  
  13.         'Percentage': 71.0,  
  14.         'Name''Amit',  
  15.         'RollNo': 2  
  16.     }  
  17. }  
Update record in firebase
We are going to use put command to update existing entry in firebase database.
  1. from firebase import firebase  
  2. firebase = firebase.FirebaseApplication('https://xxxx.firebaseio.com/', None)  
  3. firebase.put('/python-sample-ed7f7/Students/-LAgstkF0DT5l0IRucvm','Percentage',79)   
  4. print('updated')  
Now you can see updated record in firebase console
 
Python
 
Delete record in firebase
Delete a specific entry from database. This delete method will not return any result of operation.
  1. from firebase import firebase  
  2. firebase = firebase.FirebaseApplication('https://xxxx.firebaseio.com/', None)  
  3. firebase.delete('/python-sample-ed7f7/Students/''-LAgt5rGRlPovwNhOsWK')  
  4. print('deleted')  
You can see that the record is deleted from database.
 
Python


Similar Articles