Skip to content

Python code to connect, to a Firebase project and adds a new task to a Firestore database.

License

Notifications You must be signed in to change notification settings

iamrajharshit/OnFirebase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

On Firebase CRUD

CRUD: Create Read Update Delete Using Python and Admin Firebase.

  • At first will create and connect to a firebase database in puthon.
  • Once connected, create a demo database to store values for status and task as keys.

Steps to create and connect to a firebase database in python

Create firebase app

You'll need a Firebase project set up. If you don't have one, create one on the Firebase console.

  • Here, we have created CRUD named web app.
  • Select Web App.
  • Give a Name and ID.
  • Select your hosting Server and Location. create an app on fire base 

Create firestore database

Once app is created,

  • Go to Firestore Database

  • Click on Create database Go to Firestore 

  • Select Start in Production mode

  • Remember, once database is created will change the allow read, write to true.

  • Click on Create.

create DataBase 

Your database will look like this, Once the database is created, DataBase is ready 

  • Go to Rules inside the Fire database.
  • Change the allow read, write to true.
  • Click on Publish.

Change to true 

Get Python config code

We will get the python config code for our app.

  • Select Project Settings under Project Overview. Go to project settings 

Here, Admin SDK configuration snippet is available.

  • Select python.
  • Copy the Python config code to your app.py on your local system. Then, click on Service Account it will redirect to a page. Copy the Admin SDK file to your py app 

Get the Key

Now will get the key, after getting redirected to our Google Cloud, Service Account.

  • Again select service accounts, your account will be displayed.

  • Click on 3 dots and click manage keys. Go to Manage Keys 

  • Click on Add Key.

  • Select JSON as key type.

  • Click on Create.

Get .json key 

A file will be automatically downloaded.

  • Then just copy it to your project directory. Download the key 

Code

Remember to download all the dependencies mentioned in requirements.txt on your python venv.

pip install -r requirements.txt

To learn about python venv, check out the tutorial on python venv.

Now added the dependencies to your app.py, here we have named our app.py as "firebase-authdemo.py".

import firebase_admin
from firebase_admin import firestore
from firebase_admin import credentials

Here, we have renamed the JSON file as serviceAccount. Pass the downloaded key 

Inside the Python config code,

  • Pass the key "serviceAccount.json" and initialize firebase_admin
cred = credentials.Certificate("./serviceAccount.json")
firebase_admin.initialize_app(cred)

Write a simple database code 

Add a database.

Here, we have defined a dictionary named data that contains information about a task.

The dictionary has two key-value pairs:

  • task: The value is "washing," which presumably describes the task itself.
  • status: The value is "ToDO," likely indicating the current state of the task.

Establishes a connection to your Firebase Firestore database.

  • firestore is a module imported from the firebase_admin library, which provides functionality to interact with Firestore from your Python application.
  • The client() method creates a client object that handles communication with the Firestore database.
db = firestore.client()
data={
    'task':'washing',
    'status':'ToDO'
}
doc_ref = db.collection('taskCollection').document()

doc_ref.set(data)
print('Document ID : ',doc_ref.id)
  • db.collection('taskCollection'): This selects the existing collection named "taskCollection" within your Firestore database.
  • .document(): This creates a new, empty document within the chosen collection. A unique document ID is automatically generated by Firestore.
  • .set(data): This method sets the data for the newly created document. The provided data dictionary is used to populate the fields within the document.
  • The doc_ref.id attribute holds the unique identifier assigned to the document by Firestore.

Run the code

Now, we run the firebase-authdemo.py file.

Run the Code 

  • We see Document ID being displayed in the terminal.
  • They are different objects being stored to the database CRUD.

Check on firebase

In Firebase Firestore,taskCollection is a collection within your Firestore database. A collection is a container that groups related documents together. In this case, each document likely represents a task.

Check on FireBase 

  • Firestore: A NoSQL database service offered by Firebase that allows you to store and retrieve data in a flexible and scalable manner.
  • Collection: A group of documents with a similar theme or purpose. Think of it like a folder in a filing cabinet that holds related documents.
  • Document: An individual unit of data within a collection, similar to a file in a folder. It contains fields (like key-value pairs) that represent the data for a specific task.
  • Field: A property or attribute within a document that holds a specific piece of information. In this case, you have two fields:
    • status: A string field that likely indicates the current state of the task (e.g., "ToDO," "In Progress," "Done").
    • task: A string field that describes the actual task itself (e.g., "washing," "groceries," "exercise").

About

Python code to connect, to a Firebase project and adds a new task to a Firestore database.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages