Welcome to Python Firebase Course for Beginners, in this Python Firebase Course
for Beginners we are going to work with Firebase using Python Programming Language.
especially we are going to work with Firebase Real Time Database, Firebase Authentication
and Firebase Storage. and also we will use pyrebase library for this article, pyrebase is a
simple python wrapper for the Firebase API.
Installation
First of all you need to install pyrebase by using pip command like this.
1 |
pip install pyrebase |
Note: Pyrebase is for Python3 and will not work in Python2.
More Python Firebase Tutorials
1: Python Firebase Admin SDK Integration & Realtime Database
2: Python Firebase Full Course For Beginners
If you are interested in Python Firebase Admin SDK, read the below articles
1: Firebase SDK Integration with Real Time Database
2: Firebase SDK working with Cloud Firestore
3: Python Firebase SDK Working with Authentication
First of all you need to Create New Project in Firebase Console. and after that add pyrebase
to your application. for use with only user based authentication we can create the following
configuration.
1 2 3 4 5 6 7 8 9 10 |
import pyrebase config = { "apiKey": "apiKey", "authDomain": "projectId.firebaseapp.com", "databaseURL": "https://databaseName.firebaseio.com", "storageBucket": "projectId.appspot.com" } firebase = pyrebase.initialize_app(config) |
A Pyrebase app can use multiple Firebase services.
firebase.auth()
– Authentication
firebase.database()
– Database
firebase.storage()
– Storage
1: Firebase Authentication ?
Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI
libraries to authenticate users to your app. It supports authentication using passwords,
phone numbers, popular federated identity providers like Google, Facebook and Twitter,
and more.Firebase Authentication integrates tightly with other Firebase services, and it
leverages industry standards like OAuth 2.0 and OpenID Connect, so it can be easily integrated
with your custom backend.
The sign_in_with_email_and_password()
method will return user data including a
token you can use to adhere to security rules.
you can use this code for creating of users in your firebase console.
Make sure you have the Email/password provider enabled in your Firebase dashboard under
Auth -> Sign In Method.
1 2 3 4 5 6 7 8 9 10 |
#create authetication auth = firebase.auth() #get the valid email and password from the user email = input("Please Enter Your Email : ") password = input("Please Enter Your Password : ") #and authenticate the user user = auth.create_user_with_email_and_password(email, password) print("User Created Successfully") |
Also you can sign in with the created user .
1 2 |
signin = auth.sign_in_with_email_and_password(email, password) print("Sign In Was Successfull") |
Also you can do verification for the email using this code.
1 2 |
auth.send_email_verification(signin['idToken']) print("Email Verification Has Been Sent") |
Sending password reset emails.
1 2 |
auth.send_password_reset_email(email) print("We have sent an email, check your inbox ") |
2: Firebase Real Time Database
The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and
synchronized in realtime to every connected client. When you build cross-platform apps or
web applications , all of your clients share one Realtime Database instance and automatically
receive updates with the newest data.
Note: before this make sure that you have enabled firebase real time database in your
firebase console.
In here we are going to create firebase real time database, and after that we are going to
save the data, to save data with a unique, auto-generated, timestamp-based key, use
the push()
method.
1 2 3 4 5 |
db = firebase.database() data = {"name":"Parwiz Forogh"} db.child("users").push(data) print("Data added to real time database ") |
To create your own keys use the set()
method. The key in the example below is “OwnKey”.
1 |
db.child("users").child("OwnKey").set(data) |
To update data for an existing entry use the update()
method.
1 2 3 4 5 |
db.child("users").child("OwnKey").update({"name":"John Doe"}) print("Data updated successfully ") db.child("users").child("-LzqIcMVMPaQKVLLjK5d").update({"name":"Updated Name"}) print("Data updated successfully ") |
Queries return a PyreResponse object. Calling val()
on these objects returns the query data.
Calling key()
returns the key for the query data.
1 2 |
users = db.child("users").get() print(users.val()) |
Each Returns a list of objects on each of which you can call val()
and key()
.
1 2 3 4 5 |
all_users = db.child("users").get() for users in all_users.each(): print(users.val()) print(users.key()) |
For deleting data for an existing entry use the remove()
method.
1 2 |
db.child("users").child("-LzqIcMVMPaQKVLLjK5d").remove() print("User removed") |
3: Firebase Storage
Cloud Storage for Firebase is a powerful, simple, and cost-effective object storage service built
for Google scale. The Firebase SDKs for Cloud Storage add Google security to file uploads and
downloads for your Firebase apps, regardless of network quality. You can use our SDKs to store
images, audio, video, or other user-generated content. On the server, you can use Google Cloud
Storage, to access the same files.
Note: make sure that you have enabled firebase storage in your firebase console.
First we create the storage object.
1 |
storage = firebase.storage() |
Using child just like with the Database service, you can build paths to your data with the
Storage service.
1 |
storage.child("images/newimage.jpg") |
The put method takes the path to the local file and an optional user token.
1 2 |
storage.child("images/newimage.jpg").put("football.jpg") print("Image Uploaded") |
So the download method takes the path to the saved database file and the name you want
the downloaded file to have.
1 2 |
storage.child("images/newimage.jpg").download("downloaded.jgp") print("Image Downloaded") |
Also you can watch the complete video for this article.
Subscribe and Get Free Video Courses & Articles in your Email