Skip to main content

Utilities for storing, retrieving, and monitoring Firebase Realtime Database objects in Python.

Project description

PyPI version Build Status Coverage Status

Utilities for storing, retrieving, and monitoring Firebase Realtime Database objects in Python.

This builds on Pyrebase by providing turnkey support for database push updates throughout the Python stack. It works by linking together Pyrebase streams with the venerable blinker package, through a custom data structure. It allows arbitrary Python code to subscribe to notifications whenever data in your Firebase Realtime Database changes.

Q: Can’t I just use Pyrebase

A: Sure, but if you want to be notified in realtime when your data changes, you’ll need to set up thread-based stream handlers, and manage their lifecycles. In addition, the format of events from Firebase can be tricky to parse (do you know the difference between Firebase PUT and PATCH events?)

Firebase Live Data abstracts these concepts into simple blinker signals that are easy to use.

Installing

pip install FirebaseData -e git+https://github.com/heston/Pyrebase.git@a77bd6f6def656b1dcd77d938fac2707f3c4ba61#egg=Pyrebase

Dependencies

Firebase Live Data has a direct dependency on Blinker, and a peer dependency on Pyrebase (see note below). This means that Blinker will be installed automatically, while Pyrebase must be installed separately (hence its inclusion in the pip command above). This is because Pyrebase requires additional configuration that is outside the scope of this document.

A note on Pyrebase maintenance: It seems that Pyrebase is no longer being actively maintained, unfortunately. Please use this author’s fork to get things working:

pip install -e git+https://github.com/heston/Pyrebase.git@a77bd6f6def656b1dcd77d938fac2707f3c4ba61#egg=Pyrebase

Compatibility

Firebase Live Data is tested against Python 3.7, 3.8, 3.9 and 3.10. It is not compatible with Python 2.

Usage

import pyrebase

from firebasedata import LiveData

pyrebase_config = {
    # ...
}

app = pyrebase.initialize_app(pyrebase_config)
live = LiveData(app, '/my_data')

# Get a snapshot of all data at the path, `/my_data`.
#
# This also sets up a persistent push connection to the Firebase Realtime Database
# at that path. Any updates under this path will trigger `blinker` events.
#
# `data` is a local (greedy) cache of the data at the root path (`/my_data`). It behaves
# somewhat like a Python dictionary.
data = live.get_data()
all_data = data.get() #  this also works: data.get('/')
sub_data = data.get('my/sub/path')

The push connection is established lazily, after the first call to get_data.

To get notified if something changes within your LiveData connection, just connect to the signal at that database path.

def my_handler(sender, value=None):
    print(value)


# Note that the root path (`/my_data` in this case) is omitted from the signal name.

live.signal('/some/key').connect(my_handler)

my_handler will be invoked with sender set to the FirebaseData instance, and the value keyword argument set to the value of the key that changed.

You can also set data:

live.set_data('my/sub/path', 'my_value')

blinker events will be dispatched whenever data is set, either locally, like the example above, or via server push events.

Developing

  1. Install the development requirements (preferably into a virtualenv):

    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
  2. Run tests to ensure everything works:

    pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

FirebaseData-0.6.3.tar.gz (7.1 kB view details)

Uploaded Source

File details

Details for the file FirebaseData-0.6.3.tar.gz.

File metadata

  • Download URL: FirebaseData-0.6.3.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.13

File hashes

Hashes for FirebaseData-0.6.3.tar.gz
Algorithm Hash digest
SHA256 df62992723c2cbd65cab0e980165714dac185ee3d981cf92870eb87a24b2f6cd
MD5 9778ad799d5323787845852f3c57f0fd
BLAKE2b-256 f427cc35148461fff66ba4ad2752274568684df54c1e91e53927e628291fbb4c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page