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.
- Open the Firebase console.
- Click "CREATE NEW PROJECT".
- 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.
- Now, go to Database -> Select Realtime Database
Then, select the security rule “Start in test mode” so everyone can access it without any credentials.
Click on the ENABLE button.
Now, you will set the following database UI:
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
- from firebase import firebase
- firebase = firebase.FirebaseApplication('https://xxxxx.firebaseio.com/', None)
- data = { 'Name': 'Vivek',
- 'RollNo': 1,
- 'Percentage': 76.02
- }
- result = firebase.post('/python-sample-ed7f7/Students/',data)
- print(result)
Import library of Firebase in Python script. Then, create database connection using the below command.
- 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,
- {'name': '-LAgstkF0DT5l0IRucvm'}
After inserting some more records into the database, you can see them in Firebase console as below.
Get records from Firebase
Now, we need to read all inserted records using get command.
- from firebase import firebase
- firebase = firebase.FirebaseApplication('https://xxxx.firebaseio.com/', None)
- result = firebase.get('/python-sample-ed7f7/Students/', '')
- print(result)
Now, you can see the result in JSON format as below.
- {
- '-LAgstkF0DT5l0IRucvm': {
- 'Percentage': 76.02,
- 'Name': 'Vivek',
- 'RollNo': 1
- },
- '-LAgtLxF8uPedUr0z9gE': {
- 'Percentage': 65,
- 'Name': 'Akshay',
- 'RollNo': 3
- },
- '-LAgt5rGRlPovwNhOsWK': {
- 'Percentage': 71.0,
- 'Name': 'Amit',
- 'RollNo': 2
- }
- }
Update record in firebase
We are going to use put command to update existing entry in firebase database.
- from firebase import firebase
- firebase = firebase.FirebaseApplication('https://xxxx.firebaseio.com/', None)
- firebase.put('/python-sample-ed7f7/Students/-LAgstkF0DT5l0IRucvm','Percentage',79)
- print('updated')
Now you can see updated record in firebase console
Delete record in firebase
Delete a specific entry from database. This delete method will not return any result of operation.
- from firebase import firebase
- firebase = firebase.FirebaseApplication('https://xxxx.firebaseio.com/', None)
- firebase.delete('/python-sample-ed7f7/Students/', '-LAgt5rGRlPovwNhOsWK')
- print('deleted')
You can see that the record is deleted from database.