Intermediate
Jul 5, 2024
Updating your Firebase with Python
Introduction
Managing your Firebase database using Python is a powerful and efficient way to handle various backend tasks. While these operations can be performed using Cloud Functions or an Admin tool in FlutterFlow, Python offers a more flexible and straightforward approach for many developers. In this tutorial, we'll guide you through the process of updating your Firebase database withPython.
Prerequisites
Installed Python
Basic Python knowledge
Firebase Admin SDK JSON file
To get a Firebase Admin SDK JSON file:
Go to your Firebase console.
Navigate to Project Settings => ServiceAccounts.
Click on "Generate new private key" and save the file securely.

Get the Firebase Admin SDK json file (private key).
Step-by-step guide
1. Setup
First, we need to install the necessary Python libraries to interact with Firebase.
Next, we import the required modules and initialize Firebase:
2. Updating all documents
Let's say you want to set a field promo_enabled to true for all users. This can be done quickly with Python.
The users_ref.stream() fetches all documents in the "users" collection. For each user, the promo_enabled field is updated to True. The updated_count keeps track of how many documents were updated.
3. Getting some basic statistics
For more complex tasks like generating analytics, Python is an excellent choice. Let's say you want to graph some data quarterly. Of course you could have software like Tableau or Looker, but let's say you don't. This could be a graph you create:

Graph generated by Python from Firebase data.
Now let's say you want to graph the number of posts created over the last three months. Here's some sample code:
Fetches data from the posts collection. Processes each document to count posts created on each date. Uses matplotlib to plot the counts of web and non-web posts over time.
Conclusion
Using Python to manage your Firebase database is ideal for one-time jobs or complex tasks that don't need frequent execution. This approach offers flexibility and simplicity, especially for generating detailed analytics or bulk updating documents.
Note: this can be optimized when dealing with large datasets.