Python Firebase Real Time Database Tutorial with Example

Python Firebase Real Time Database Tutorial with Example

Python Firebase Real Time Database Tutorial with Example - in this Python tutorial, we are going to talk about Python Firebase Real Time Database. Firebase is a google product which offers so many useful features: Firebase Real Time Database, Push Notifications, Firebase Authentication, Firebase Cloud Messaging, Firebase Test Lab For Android, Firebase Crash Reporting, Firebase Notification, Firebase App Indexing and many more

Python Firebase Real Time Database – in this Python article iam going to talk about Python Firebase Real Time Database.

So first of all Firebase is platform which allow to build web and mobile applications without server

side programming language. You can store users data on its real-time database which sync

data among users data in no time.

Firebase is a google product which offers so many useful features. Like

_1: Firebase Real Time Database
_2: Push Notifications
3: Firebase Authentication
_4: Firebase Cloud Messaging
_5: Firebase Test Lab For Android
6: Firebase Crash Reporting
_7: Firebase Notification
_8: Firebase App Indexing and many more _

So in this article we are going to use Real Time Database Feature of Firebase.

OK when you want to use Firebase in Python you need to install some packages

pip install requests

pip install python-firebase

So now this is the code for inserting data to Firebase Real Time database

from firebase import firebase


firebase = firebase.FirebaseApplication('URL of Database', None)
data =  { 'Name': 'John Doe',
          'RollNo': 3,
          'Percentage': 70.02
          }
result = firebase.post('/python-example-f6d0b/Students/',data)
print(result)

OK now in this line of code FirebaseApplication takes two parameters; one is URL of database and second is authentication details for database

because for this project we have not set a rule by this reason we have given None in the second parameter.

firebase = firebase.FirebaseApplication('URL of Database', None)

And in this line of code we are going to insert data to our table

result = firebase.post('/python-example-f6d0b/Students/',data)

OK this was for inserting data and now we want to retrieve the data, you can use this code

**Retrieving The Data **
from firebase import firebase



firebase = firebase.FirebaseApplication('URL of database', None)
result = firebase.get('/python-example-f6d0b/Students/', '')
print(result)
Updating The Data
from firebase import firebase

firebase = firebase.FirebaseApplication('Database URL', None)
firebase.put('/python-example-f6d0b/Students/-LjLUhaWGuxNd5gOEmse','Name','Bob')
print('Record Updated')
Deleting The Data
from firebase import firebase

firebase = firebase.FirebaseApplication('URL Of Database', None)
firebase.delete('/python-example-f6d0b/Students/', '-LjLUhaWGuxNd5gOEmse')
print('Record Deleted')

A Basic Guide to Firebase Realtime Database using Python SDK

A Basic Guide to Firebase Realtime Database using Python SDK

A Basic Guide to Firebase Realtime Database using Python SDK. Firebase Realtime Database is a NoSQL cloud-hosted database. The Firebase Admin Python SDK enables server-side (backend) Python developers to integrate Firebase into their services and applications. Data is stored as JSON and synchronized in realtime to every connected client. Using Firebase Admin Python SDK, we can read and write Realtime Database data with full admin privileges, or limited privileges.

Firebase Realtime Database is a NoSQL cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. Using Firebase Admin SDK, we can read and write Realtime Database data with full admin privileges, or limited privileges.

In my recent project, I had the opportunity to work with Firebase Realtime Database. So, I decided to write a basic guide to using Firebase Realtime Database using Python SDK.

Add Firebase to the App

To use the Firebase Admin SDK, we'll need a Firebase project, a service account to communicate with the Firebase service, and a configuration file with your service account's credentials. Follow Add Firebase to your app to get the configuration file, which is a JSON file.

Install Firebase Python SDK

The Firebase Admin Python SDK enables server-side (backend) Python developers to integrate Firebase into their services and applications. To install Firebase Admin Python SDK, simply execute the following command in a terminal:

pip install firebase-admin
Initialize the SDK

Initialize the SDK with this code snippet:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db

# Fetch the service account key JSON file contents
cred = credentials.Certificate('firebase-adminsdk.json')
# Initialize the app with a service account, granting admin privileges
firebase_admin.initialize_app(cred, {
    'databaseURL': 'https://<app_name>.firebaseio.com/'
})
Structure the Database

In Firebase Realtime Database, data is structured as a JSON tree. Unlike a SQL database, there are no tables or records. When we add data to the JSON tree, it becomes a node in the existing JSON structure with an associated key.

Best practice for a data structure is to avoid nesting data and to keep the data structure as flat as possible. Follow Structure Your Database for a detailed guideline.

Save Data

Create a database reference and then pass it an object using set method:

ref = db.reference('/')
ref.set({
        'boxes': 
            {
                'box001': {
                    'color': 'red',
                    'width': 1,
                    'height': 3,
                    'length': 2
                },
                'box002': {
                    'color': 'green',
                    'width': 1,
                    'height': 2,
                    'length': 3
                },
                'box003': {
                    'color': 'yellow',
                    'width': 3,
                    'height': 2,
                    'length': 1
                }
            }
        })

Follow Saving Data for more information.

Update Data

To update a node of a database location without overwriting other child nodes, use the update method as shown below:

ref = db.reference('boxes')
box_ref = ref.child('box001')
box_ref.update({
    'color': 'blue'
})

This will update box001 color to blue.

The Firebase Realtime Database also supports multi-path updates. Using it, we can update color to both box001 and box002 at the same time:

ref = db.reference('boxes')
ref.update({
    'box001/color': 'red',
    'box002/color': 'blue'
})

In Python Admin SDK all write methods are blocking. That is, the write methods do not return until the writes are committed to the database.

Save Lists of Data

If we want to store a list of boxes, then we might want to use a unique key for each new box instead of box001, box002, box003. To solve this, the Firebase clients provide a push() function that generates a unique key for each new child

ref = db.reference('boxes')
ref.push({
    'color': 'purple',
    'width': 7,
    'height': 8,
    'length': 6
})

The unique key is based on a timestamp, so list items will automatically be ordered chronologically. Our database data now looks like this:

{
  "boxes": {
    "-L5_dtlqoO2K8rgwdxJ3": {
      "color": "purple",
      "width": 7,
      "height": 8,
      "length": 6
    }
  }
}

Get the Unique Key Generated by push()

Calling push() will return a reference to the new data path, which you can use to get the key or set data to it. The following code will result in the same data as the above example, but now we'll have access to the unique key that was generated:

ref = db.reference('boxes')
# Generate a reference to a new location and add some data using push()
new_box_ref = ref.push({
    'color': 'purple',
    'width': 7,
    'height': 8,
    'length': 6
})
# Get the unique key generated by push()
box_id = new_box_ref.key
Retrieve Data

The Python Admin SDK currently only supports blocking reads. It cannot be used to add event listeners that receive realtime update notifications. In python, data stored in a Firebase Realtime Database is retrieved by invoking a blocking method on a database reference, which returns the data stored at the reference. Each method call is a onetime operation.

The get() method in Python returns a Python representation of the data directly:

ref = db.reference('boxes')
print(ref.get())
Query Data

Firebase Realtime Database supports some queries to selectively retrieve data based on various factors.

Data Ordering

We can order data in three ways: by child key, by key, or by value. A basic database query starts with one of these ordering functions, each of which is explained below.

Indexing on Values

If we want to use order query, we need to add the values/keys to the .indexOn rule.

{
  "rules": {
    "boxes": {
      ".indexOn": ["color", "width", "height", "length"]
    },
    "weights": {
      ".indexOn": [".value"]
    },
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

Read Index your data on the .indexOn rule for more information.

Order by a Specified Child Key

To get data of all boxes ordered by height, we can do the following:

ref = db.reference('boxes')
snapshot = ref.order_by_child('height').get()
for key, val in snapshot.items():
    print('{0} => {1}'.format(key, val))

Order by Key

We can also order nodes by their keys using the orderByKey() method:

ref = db.reference('boxes')
snapshot = ref.order_by_key().get()
for key, val in snapshot.items():
    print('{0} => {1}'.format(key, val))

Order by Value

We can order nodes by the value of their child keys using the orderByValue() method:

{
  "weights": {
    "person001" : 60,
    "person002" : 65,
    "person003" : 80,
    "person004" : 55,
    "person005" : 72
  }
}
ref = db.reference('weights')
snapshot = ref.order_by_value().get()
for key, val in snapshot.items():
    print('{0} => {1}'.format(key, val))

Limit Queries

The limitToFirst() and limitToLast() queries are used to set a maximum number of children to be retrieved:

ref = db.reference('boxes')
snapshot = ref.order_by_child('weight').limit_to_last(2).get()
for key in snapshot:
    print(key)
ref = db.reference('boxes')
snapshot = ref.order_by_child('height').limit_to_first(2).get()
for key in snapshot:
    print(key)
weights_ref = db.reference('weights')
snapshot = weights_ref.order_by_value().limit_to_last(3).get()
for key, val in snapshot.items():
    print('{0} => {1}'.format(key, val))

Range Queries

Using startAt(), endAt(), and equalTo() allows to choose arbitrary starting and ending points for our queries:

ref = db.reference('boxes')
snapshot = ref.order_by_child('color').start_at('g').get()
for key in snapshot:
    print(key)
ref = db.reference('boxes')
snapshot = ref.order_by_key().end_at('w').get()
for key in snapshot:
    print(key)
ref = db.reference('boxes')
snapshot = ref.order_by_key().start_at('g').end_at(u'n\uf8ff').get()
for key in snapshot:
    print(key)
ref = db.reference('boxes')
snapshot = ref.order_by_child('length').equal_to(3).get()
for key in snapshot:
    print(key)

Visit Data security to know about security rules.
Visit firebase_admin.db module to get information about the module containing functions and classes that facilitate interacting with the Firebase Realtime Database.

Download source code from Github.

Top Python Development Companies | Hire Python Developers

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...

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 you look for the developer in hurry you may forget to take note of review and ratings of the company's aspects, but we at TopDevelopers have done a clear analysis of these top reviewed Python development companies listed here and have picked the best ones for you.

List of Best Python Web Development Companies & Expert Python Programmers.

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Description
Learn Hands-On Python Programming By Creating Projects, GUIs and Graphics

Python is a dynamic modern object -oriented programming language
It is easy to learn and can be used to do a lot of things both big and small
Python is what is referred to as a high level language
Python is used in the industry for things like embedded software, web development, desktop applications, and even mobile apps!
SQL-Lite allows your applications to become even more powerful by storing, retrieving, and filtering through large data sets easily
If you want to learn to code, Python GUIs are the best way to start!

I designed this programming course to be easily understood by absolute beginners and young people. We start with basic Python programming concepts. Reinforce the same by developing Project and GUIs.

Why Python?

The Python coding language integrates well with other platforms – and runs on virtually all modern devices. If you’re new to coding, you can easily learn the basics in this fast and powerful coding environment. If you have experience with other computer languages, you’ll find Python simple and straightforward. This OSI-approved open-source language allows free use and distribution – even commercial distribution.

When and how do I start a career as a Python programmer?

In an independent third party survey, it has been revealed that the Python programming language is currently the most popular language for data scientists worldwide. This claim is substantiated by the Institute of Electrical and Electronic Engineers, which tracks programming languages by popularity. According to them, Python is the second most popular programming language this year for development on the web after Java.

Python Job Profiles
Software Engineer
Research Analyst
Data Analyst
Data Scientist
Software Developer
Python Salary

The median total pay for Python jobs in California, United States is $74,410, for a professional with one year of experience
Below are graphs depicting average Python salary by city
The first chart depicts average salary for a Python professional with one year of experience and the second chart depicts the average salaries by years of experience
Who Uses Python?

This course gives you a solid set of skills in one of today’s top programming languages. Today’s biggest companies (and smartest startups) use Python, including Google, Facebook, Instagram, Amazon, IBM, and NASA. Python is increasingly being used for scientific computations and data analysis
Take this course today and learn the skills you need to rub shoulders with today’s tech industry giants. Have fun, create and control intriguing and interactive Python GUIs, and enjoy a bright future! Best of Luck
Who is the target audience?

Anyone who wants to learn to code
For Complete Programming Beginners
For People New to Python
This course was designed for students with little to no programming experience
People interested in building Projects
Anyone looking to start with Python GUI development
Basic knowledge
Access to a computer
Download Python (FREE)
Should have an interest in programming
Interest in learning Python programming
Install Python 3.6 on your computer
What will you learn
Build Python Graphical User Interfaces(GUI) with Tkinter
Be able to use the in-built Python modules for their own projects
Use programming fundamentals to build a calculator
Use advanced Python concepts to code
Build Your GUI in Python programming
Use programming fundamentals to build a Project
Signup Login & Registration Programs
Quizzes
Assignments
Job Interview Preparation Questions
& Much More