Using Firebase with Python

Krishna Ojha
3 min readApr 22, 2020

--

Firebase + Python

Firebase is one of the most popular NoSQL database out there and Python, one of the most used programming language in variety of fields.

The Steps are simple. I’ll be dividing the steps in two main categories. First would be setting up Firebase and other would be writing the Python Script.

Setting Up Firebase

  1. Open Firebase, log in with your google account and go to console.
Firebase Home Page

2. Click on Add Project. You’ll be redirected to the next page where you need to Name your project.

3. Open your project and click on the </> icon to open your web app settings.

4. Name your app and register it with your Firebase Project.

5. You’ll be provided a code snippet that will contain your app details. Copy the varialble

var firebaseConfig{

api key…

measurementID

};

Web App Code Snippet

You have successfully setup your Firebase, now it time for some Python coding. Before that, incase you don’t have Python on your system, you can download it from here.

Python Script

  1. You’ll need to install pyrebase library before you could proceed, which you could easily install using your terminal with the below command

pip install pyrebase

2. Open any of the IDE you prefer to use, I’ll be using PyCharm instance which you can download from here.

3. Import the pyrebase library

import pyrebase

and you are good to go.

4. Remeber the config variable that we copied from the Firebase, paste it in the python script. We’ll be using it as a dictionary so be sure to add double quotes to the keys. Like this

The firebaseConfig Dictionary

5. Now we need to initialize connection with Firebase

firebase = pyrebase.initialize_app(config)

6. Next step is to initialize reference variable for the Firebase Storage and Realtime Database.

storage = firebase.storage()
database = firebase.database()

So now you can use functions of pyrebase to read and write data from Firebase Cloud Storage, like images, videos etc and you can also read and write data from Realtime Firebase Database.

I’ll be enlisting a few functions of the pyrebase library that can be used for data transfer between your local machine and Firebase using python script. You can also refer my Github Repo where you can find a file consisting of custom functions made using functions from the pyrebase library for ease of use, which you can easily import and use.

Few functions of the pyrebase library

DATABASE

  • set
database.child("DB Object name")
data = {"Key1": "Value1", "Key2": "Value2"}
database.set(data)
  • update
database.child("DB Object Name").update({"Key": "Value"})
  • get
accidents = database.child("DB Object Name").get()
data = accidents.val()

STORAGE

  • put
storage.child("path_on_cloud_storage").put("local_path")
  • download
storage.child("path_on_cloud_storage").download("local_path")
  • get_url
storage.child("images/example.jpg").get_url()

These were some of the basic functions which are used for normal tasks. You can find more of these functions at the Pyrebase repo here.

The possibilities are endless…Go ahead and get your Python project linked to Firebase!

--

--

Krishna Ojha

Web Development is love, Artificial Intelligence my craze and Hardware is my passion. A complete tech enthusiast!