Introduction
Pyrebase is a simple wrapper for the Firebase API which was written only for Python 3.(Does not Support Py 2)
Preliminaries
- Have an Linux Environment(My Choice)
- Have a Google Account
- Create a Firebase Project in Firebase Console
- Go To Project Settings and Create a Web App and get the Configurations
config = {
"apiKey": "apiKey",
"authDomain": "projectId.firebaseapp.com",
"databaseURL": "https://databaseName.firebaseio.com",
"storageBucket": "projectId.appspot.com"
}
On Your Local Computer,
Make Sure To Create an Virtual Environment to avoid conflicts (Believe Me From Experience : There is a Conflict)
python3 -m venv env
Activate the Virtual Environment
source env/bin/activate
Install the pyrebase
pip3 install pyrebase
and Pyrebase supports Authentication, Database and Storage Services
Authentication -> firebase.auth()
Database -> firebase.database()
Storage -> firebase.storage()
Finally Open Your Code Editor, Let's Code...!
Firebase Initialization:
After Installation of Pyrebase, Initialize your app
import pyrebase
config = {
"apiKey": "YourApiKey",
"authDomain": "YourProjectId.firebaseapp.com",
"databaseURL": "https://YourDatabaseName.firebaseio.com",
"storageBucket": "YourProjectId.appspot.com"
}
firebase = pyrebase.initialize_app(config)
Firebase Authentication:
Firebase provides authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more and it improves industry standards by integrating OAuth 2.0 and OpenID Connect with the custom backend.
-> Create an Authentication
auth = firebase.auth() # Creates Authentication
-> Input From User
user = input("Enter a User") # Input Username
password = input("Enter a Password") # Input Password
-> Authenticate the User
signin = auth.sign_in_with_email_and_password(email, password)
print("Sign In Was Successfull")
-> You can also verify the email using code
auth.send_email_verification(signin['idToken'])
print("Email Verification Sent")
->Send Password Reset Emails
auth.send_password_reset_email(email)
print("Email Sent...Check Your Inbox")
Firebase Real Time Database
The Firebase Realtime Databaseis stored as JSON and synchronized in realtime to every connected client.
-> Intialize Database
db = firebase.database()
# data
data = {
"cloud" : "maestro"
}
->Sending Data to the Database
db.child("users").set(data)
->Update Data in the Database
data2 = {
"cloud_maestro" : "saranmahadev"
}
db.child("users").update(data2)
-> Get the Data from the database
users = db.child("users").get() print(users.val()) # Prints the Values of the JSON Object print(users.key()) #Prints the Keys of the JSON Object
-> To delete the data in the database
db.child("users").child("cloud-maestro").remove()
Firebase Storage
Cloud Storage for Firebase is a powerful, simple, and cost-effective object storage service built for Google scale.
-> Creating the Object
storage = firebase.storage()
-> Building Paths To Your Data
storage.child("img/newimg.jpg")
-> Uploading Images to the Storage
storage.child("img/newimg.jpg").upload("example.jpg")
-> Downloading Images from the Storage
storage.child("img/newimg.jpg").download("example.jpg")
Reference : Codeloop