Python Firebase Cloud Firestore Example

Python Firebase Cloud Firestore Example

In this Python Firebase tutorial, we are going to to talk about Python Firebase Cloud Firestore Example. What is Cloud Firestore ? According to Firebase Documentation, Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud Platform

In this Python Firebase article iam going to talk about Cloud Firestore Example in Python Programming Language. so before this we had an article about Python Firebase Admin SDK Integration & Working With Realtime Database, you can check that article in the below link.

1: Python Firebase Admin SDK Integration & Realtime Database

2: Python Firebase Full Course For Beginners

What is Cloud Firestore ?

According to Firebase Documentation, Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud Platform. Like Firebase Realtime Database, it keeps your data in sync across client apps through realtime listeners and offers offline support for mobile and web so you can build responsive apps that work regardless of network latency or Internet connectivity. Cloud Firestore also offers seamless integration with other Firebase and Google Cloud Platform products, including Cloud Functions.

*Create Cloud Firestore Database *

  1. If you haven’t already, create a Firebase project: In the Firebase console, click Add project, then follow the on-screen instructions to create a Firebase project or to add Firebase services to an existing GCP project.
  2. Navigate to the Database section of the Firebase console. You’ll be prompted to select an existing Firebase project. Follow the database creation workflow.
  3. Select a starting mode for your Cloud Firestore Security Rules:

Test mode

Good for getting started with the mobile and web client libraries, but allows anyone to read and overwrite your data. After testing, make sure to review the Secure your data section.

To get started with the web, iOS, or Android SDK, select test mode.

Locked mode

Denies all reads and writes from mobile and web clients. Your authenticated application servers (C#, Go, Java, Node.js, PHP, Python, or Ruby) can still access your database.

*Installation *

OK so after creation of your project in Firebase Console, you need to enable Firebase Cloud Firestore in your project. after that for integration of Firebase SDK you need to install the SDK for the language of your choice. because we are using python, so we do the process for python programming language. you can easily install Firebase Admin SDK with pip like this.

pip install firebase-admin

To authenticate a service account and authorize it to access Firebase services, you must generate a private key file in JSON format.

  1. In the Firebase console, open Settings > Service Accounts.
  2. Click Generate New Private Key, then confirm by clicking Generate Key.
  3. Securely store the JSON file containing the key.

and add that json file to your working directory. Python Firebase Cloud Firestore Example

This is the complete code for Python Firebase Cloud Firestore Example

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore



# initializations 
cred = credentials.Certificate('firebase-sdk.json')
firebase_admin.initialize_app(cred)
db = firestore.client()



#adding first data
doc_ref = db.collection('employee').document('empdoc')

doc_ref.set({

    'name':'Parwiz',
    'lname':'Forogh',
    'age':24


})



#adding second data
doc_ref = db.collection('employee').document('emptwodoc')
doc_ref.set({

    'name':'John',
    'lname':'Doe',
    'email':'[email protected]',
    'age':24


})



#Reading the data
'''

emp_ref = db.collection('employee')
docs = emp_ref.stream()

for doc in docs:
    print('{} => {} '.format(doc.id, doc.to_dict()))


'''

OK now these are the imports that we need to use for this article , basically we have imported firebase_admin_, _credentials and firestore.

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

In here we have created our credentials and we have added our json key file, also we need to initialize our credentials.

cred = credentials.Certificate('firebase-sdk.json')

firebase_admin.initialize_app(cred)

Now we need to create our firestore in here.

cred = credentials.Certificate('firebase-sdk.json')

firebase_admin.initialize_app(cred)

Adding Data

Cloud Firestore stores data in Documents, which are stored in Collections. Cloud Firestore creates collections and documents implicitly the first time you add data to the document. You do not need to explicitly create collections or documents.

Create a new collection and a document using the following example code.

doc_ref = db.collection('employee').document('empdoc')

doc_ref.set({

    'name':'Parwiz',
    'lname':'Forogh',
    'age':24


})

Now we are going to add another data to the document.

doc_ref = db.collection('employee').document('emptwodoc')
doc_ref.set({

    'name':'John',
    'lname':'Doe',
    'email':'[email protected]',
    'age':24


})

Reading Data

To quickly verify that you’ve added data to Cloud Firestore, use the data viewer in the Firebase console.

You can also use the “get” method to retrieve the entire collection.

emp_ref = db.collection('employee')
docs = emp_ref.stream()

for doc in docs:
    print('{} => {} '.format(doc.id, doc.to_dict()))

python firebase cloud

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Deploy Python on Firebase Hosting with Cloud Run - Firecasts

In this Firebase tutorial, we’re going to be using Firebase Hosting and Cloud Run to host a flask server. Find out what Cloud Run is, how you can deploy Python on Firebase Hosting with Docker, and more on this episode with our Firebase Developer Advocate David East.

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

Python Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.

Cloud Migration Strategy

Before dipping that first toe in the cloud migration, think of devising the transition plan. 6Rs Migration Technique has become a staple of cloud migration strategy.