MoreScratch
MoreScratch

Reputation: 3083

Query Firebase using python-firebase

How does one query Firebase using Python? I am using python-firebase and can do simple gets (e.g. all entities) but need to query for a subset of entities. Any pointers would be helpful. I have successfully done this using JavaScript BTW.

Upvotes: 6

Views: 6513

Answers (3)

OneCricketeer
OneCricketeer

Reputation: 191820

You can now use Firebase Admin Python API to get at the database

https://firebase.googleblog.com/2017/07/accessing-database-from-python-admin-sdk.html?m=1

from firebase_admin import db

root = db.reference()
x = db.reference('users/{0}'.format(new_user.key)).get()
print 'Name:', x['name']
print 'Since:', x['since']

python-firebase isn't keeping up to date with firebase api change

Latest commit over 2 years ago

Upvotes: 3

Elonelon
Elonelon

Reputation: 78

use Pyrebase, it's simple and easy to understand (in my opinion)

data = db.child("sensor").order_by_child("temp").limit_to_last(10).get()
print(data.val())

result :

Upvotes: 0

Mr. Phantom
Mr. Phantom

Reputation: 517

To request an specific node from firebase you need to specify it in your URL, in the following example I will load a specific node (-KeuhkMH8SQcYJHatRnD) from a main 'news' node:

def load_news():

    try:
        loader = urllib.request.urlopen("https://<YOUR-PROJECT-ID>.firebaseio.com/news/-KeuhkMH8SQcYJHatRnD.json")
    except urllib.error.URLError as e:
        message = json.loads(e.read())
        print(message["error"])
    else:
        print(loader.read())  

Upvotes: 2

Related Questions