WaterlessStraw
WaterlessStraw

Reputation: 673

Updating a single value in Firebase with python

I am a total newbie when it comes to backend. I am working on a very simple webpage that needs one element to be updated every couple minutes or so. I'd like it to make a request to my Firebase database, get a single integer, and change a number on the webpage to that integer.

Right now I am having trouble updating the Firebase with a simple Python program. Here is what my Firebase looks like every time I run my python script: Click

When I run the script, it adds 6 new random variables with the value I'd like to send to Firebase. Here is what my code looks like so far:

from firebase import firebase
fb = firebase.FirebaseApplication('https://myAssignedDomain.com/', None)
Result = fb.post('test/coffee', {'percentage': 40})

What do I need to do in order to only change one existing value in Firebase rather than create 6 new random variables?

Upvotes: 6

Views: 10897

Answers (2)

ifti
ifti

Reputation: 669

This is how you can update the value of a particular property in firebase python 1.2 package

from firebase import firebase
fb = firebase.FirebaseApplication('https://myAssignedDomain.com/', None)
fb.put('test/asdf',"count",4) #"path","property_Name",property_Value

Upvotes: 5

Mr. Phantom
Mr. Phantom

Reputation: 517

This function will update the 'percentage' value from your node. Just make sure that the node is writable so your script can modify it.

import urllib.request
import urllib.error
import json


def update_entry(new_percentage):

    my_data = dict()
    my_data["percentage"] = new_percentage

    json_data = json.dumps(my_data).encode()
    request = urllib.requests.Request("https://<YOUR-PROJECT-ID>.firebaseio.com/test/coffe.json", data=json_data, method="PATCH")

    try:
        loader = urllib.request.urlopen(request)
    except urllib.error.URLError as e:
        message = json.loads(e.read())
        print(message["error"])
    else:
        print(loader.read())

Upvotes: 0

Related Questions