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. first of all you need to install pyrebase by using pip command like this.

pip install pyrebase

pyrebase is for python3 and will not work in python2.

Check Python GUI Development With PyQt5

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:

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.


#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 .


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.


auth.send_email_verification(signin['idToken'])
print("Email Verification Has Been Sent")

Sending password reset emails


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.

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”.

db.child("users").child("OwnKey").set(data)

To update data for an existing entry use the update() method.


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.

users = db.child("users").get()
print(users.val())

Each Returns a list of objects on each of which you can call val() and key().


all_users = db.child("users").get()

for users in all_users.each():
    print(users.val())
    print(users.key())

To delete data for an existing entry use the remove() method.


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.

storage = firebase.storage()

Using child just like with the Database service, you can build paths to your data with the Storage service.

storage.child("images/newimage.jpg")

The put method takes the path to the local file and an optional user token.

storage.child("images/newimage.jpg").put("football.jpg")
print("Image Uploaded")

The download method takes the path to the saved database file and the name you want the downloaded file to have.

storage.child("images/newimage.jpg").download("downloaded.jgp")
print("Image Downloaded")

Thanks for reading .

Originally published by Parwiz at codeloop

#python #programming #firebase

Python Firebase Course For Beginners
1 Likes124.80 GEEK