Python Firebase Database Connection

Python Firebase Database Connection – This tutorial I have explained how connect the Python code into Google Firebase SDK database connection. Most of software developers always select Firebase because it’s very convenient when compared to SQL. But we need strong knowledge on Firebase, otherwise it’s very complicate to handle the errors.

Currently you are reading this article so I hope you have good knowledge in the particular domain. If no, once read official documentation for gathering more knowledge.

After reading the steps, you get one idea for how configure Firebase on your back-end service. In the initial stage it’s common for everyone, after that easily working on code. Through Firebase easily develop small scale application. This is major advantage for most of developers, that’s why they are using Google Firebase.

For the large scalability projects, use both of Firebase & MySQL code. NoSQL helps to store unstructured data like product description, customer reviews etc.

Firebase Connection Steps

Okay let’s star to see the database connectivity steps for config python code. Firstly go to on Firebase Console and create new project. After that we specify the URL for initializing database into client side. Then database connectivity code shared on the next section.

import { initializeApp } from 'firebase/app';
import { getDatabase } from "firebase/database";
// TODO: Replace with your app's Firebase project configuration
const firebaseConfig = {
  apiKey: "API_KEY",
  authDomain: "PROJECT_ID.firebaseapp.com",
  // The value of `databaseURL` depends on the location of the database
  databaseURL: "https://DATABASE_NAME.firebaseio.com",
  projectId: "PROJECT_ID",
  storageBucket: "PROJECT_ID.appspot.com",
  messagingSenderId: "SENDER_ID",
  appId: "APP_ID",
  // For Firebase JavaScript SDK v7.20.0 and later, `measurementId` is an optional field
  measurementId: "G-MEASUREMENT_ID",
};
const app = initializeApp(firebaseConfig);
// Get a reference to the database service
const database = getDatabase(app);

Apart from this JSON helps to read the data from server side. We can easily able to store & access from client side. For this JSON is biggest solution to pass the information from client to server. However manually we need implement the code for calling JSON files. Then only the particular function will be executed. Otherwise database service automatically terminated by server.

python firebase database connection
Credits – Fitrebase

Create Project

For example create any sample projects which is store and retrieve data from Firebase database. This is the concept so first download any projects. After that execute this code for configure database connection into our existing project.

Finally after complete the connection we easily able to communicate with back-end services. Authentication is best project because via this we check Login & Registration form.

We have collection of Python project with source code. So here choose any one of concept and connecting the values from client side.

Create Python File

Create new python file for accessing database files. If you make the connection i JSON, then taking action for configure the database. Feel complicate for the manual process ? Don’t worry this is the initial stage so don’t give up your code practice..

import firebase
from firebase import Firebase
from firebase import firebase as f
from stdiomask import getpass 
import sys
config = {
  "apiKey": "AIzaSyBlLq7LECUeC0Et_UZi3KMIYzdTYyeSbNI",
  "authDomain": "information-agreegator.firebaseapp.com",
  "projectId": "information-agreegator",
  "storageBucket": "information-agreegator.appspot.com",
  "messagingSenderId": "149901859275",
  "databaseURL": "https://information-agreegator-default-rtdb.firebaseio.com/",
  "appId": "1:149901859275:web:69de34d55096a98f09be4e",
  "serviceAccount": (r"\Downloads\info-agree-firebase-adminsdk.json")
}

firebase = Firebase(config)
auth = firebase.auth()

#enter the details
name = input("Enter your name : \n")
email = input("Please Enter Your Email Address : \n").strip()
password = getpass("Please Enter Your Password : \n",mask="*")

#create user
auth.create_user_with_email_and_password(email, password)

#user sign in 
user = auth.sign_in_with_email_and_password(email, password)
print("login successful")

#verification mail
auth.send_email_verification(user['idToken'])
print("verification mail send")
#database entry
data={name:email}
fire = f.FirebaseApplication("https://information-agreegator-default-rtdb.firebaseio.com/",None)
result = fire.post('/information-agreegator-default-rtdb/',data)
print(result)

However test mode not working when we deploy this code on server. Suppose if you are plan to publish code on server side, then working real time database and skip test mode feature. It’s only working on local system.

Python Firebase Database Connection

Above code helps to make a database connection with Python. As a result it’s perfect for small based web and mobile apps. For the large scalability projects, just go frameworks such as Laravel, Codeigniter and more techniques.

Similarly facing any issues on this project, just comment below we will try to solve within a days. Because we are always online to sort out end users problems.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.