A simple python wrapper for the Firebase API.
pip install pyrebase
ref = pyrebase.Firebase('https://yourfirebaseurl.firebaseio.com', 'yourfirebasesecret')
Pyrebase will authenticate as an admin by default, disregarding security rules.
The auth_with_password()
method will return user data including a token you can use to adhere to security rules.
Each of the following methods accepts an optional user token: get()
, push()
, set()
, update()
, remove()
and stream()
.
user = ref.auth_with_password("email", "password")
data = {
"date": "26-10-1985"
}
results = ref.child("users").child("Marty").update(data, user["token"])
You can build paths to your data by using the child()
method.
ref.child("users").child("Marty")
To save data with a unique, auto-generated, timestamp-based key, use the push()
method.
data = {"name": "Marty Mcfly", "date": "05-11-1955"}
ref.child("users").push(data)
To create your own keys use the set()
method. The key in the example below is "Marty".
data = {"Marty": {"name": "Marty Mcfly", "date":"05-11-1955"}}
ref.child("users").set(data)
To update data for an existing entry use the update()
method.
ref.child("users").child("Marty").update({"date": "26-10-1985"})
To delete data for an existing entry use the remove()
method.
ref.child("users").child("Marty").remove()
You can also perform multi-location updates with the update()
method.
data = {
"users/Marty/": {
"date": "26-10-1985"
},
"users/Doc/": {
"date": "26-10-1985",
}
}
ref.update(data)
To perform multi-location writes to new locations we can use the generate_key()
method.
data = {
"users/"+ref.generate_key(): {
"date": "26-10-1985"
},
"users/"+ref.generate_key(): {
"date": "26-10-1985"
}
}
ref.update(data)
Queries return a PyreResponse object. Calling val()
on these objects returns the query data.
users = ref.child("users").get()
print(users.val()) # {"Marty": {"name": "Marty", "date": "26-10-1985"}, "Doc": {"name": "Doc", "date": "01-01-1885"}}
Calling key()
returns the key for the query data.
user = ref.child("users").get()
print(user.key()) # users
Returns a list of objects on each of which you can call val()
and key()
.
all_users = ref.child("users").get()
for user in all_users.each():
print(user.key()) # Marty
print(user.val()) # {name": "Marty", "date": "26-10-1985"}
To return data from a path simply call the get()
method.
all_users = ref.child("users").get()
To return just the keys at a particular path use the shallow()
method.
all_user_ids = ref.child("users").shallow().get()
Note: shallow()
can not be used in conjunction with any complex queries.
You can listen to live changes to your data with the stream()
method.
def stream_handler(posts):
for post in posts:
print(post["event"]) # put
print(post["path"]) # /-K7yGTTEp7O549EzTYtI
print(post["data"]) # {'title': 'Pyrebase', "body": "etc..."}
my_stream = ref.child("posts").stream(stream_handler)
my_stream.close()
Queries can be built by chaining multiple query parameters together.
users_by_name = ref.child("users").order_by_child("name").limit_to_first(3).get()
This query will return the first three users ordered by name.
We begin any complex query with order_by_child()
.
users_by_name = ref.child("users").order_by_child("name").get()
This query will return users ordered by name.
Return data with a specific value.
users_by_score = ref.child("users").order_by_child("score").equal_to(10).get()
This query will return users with a score of 10.
Specify a range in your data.
users_by_score = ref.child("users").order_by_child("score").start_at(3).end_at(10).get()
This query returns users ordered by score and with a score between 3 and 10.
Limits data returned.
users_by_score = ref.child("users").order_by_child("score").limit_to_first(5).get()
This query returns the first five users ordered by score.
ref.create_user("email", "password")
Note: Make sure you have Email & Password Authentication enabled on your firebase dashboard under Login & Auth.
ref.remove_user("email", "password")
ref.change_password("email", "old_password", "new_password")
ref.send_password_reset_email("email")
generate_key()
is an implementation of Firebase's key generation algorithm.
See multi-location updates for a potential use case.
Sometimes we might want to sort our data multiple times. For example, we might want to retrieve all articles written between a certain date then sort those articles based on the number of likes.
Currently the REST API only allows us to sort our data once, so the sort()
method bridges this gap.
articles = ref.child("articles").order_by_child("date").start_at(startDate).end_at(endDate).get()
articles_by_likes = ref.sort(articles, "likes")
Indexing is not enabled for the database reference.