如何使用 Python 从 Firebase 检索数据

Sah*_*dah 3 python patch get python-requests firebase

我是 Python 新手,我想使用 Python 连接到 Firebase。我可以使用put()and成功添加和修改 Firebase patch(),但找不到从 Firebase 检索数据的方法。

代码:

import serial
import time
import requests
import json

firebase_url = 'https://testing1639.firebaseio.com'

#Connect to Serial Port for communication
ser = serial.Serial('/dev/ttyUSB0', 9600)

#Setup a loop to send temperature values at fixed intervals in seconds
fixed_interval = 2

while 1:
    try:
         #Temperature value obtained from Arduino + LM35 Temp Sensor
         temperature_c = ser.readline()
         #Current time and date
         time_hhmmss = time.strftime('%H:%M:%S')
         date_mmddyyyy = time.strftime('%d/%m/%Y')

         #Current location name
         temperature_location = 'Mumbai-Kandivali' ;

         print temperature_c + ',' + time_hhmmss + ',' + date_mmddyyyy + ',' + temperature_location

         #Insert record
         data = {'date':date_mmddyyyy,'time':time_hhmmss,'value':temperature_c}

         result = requests.post(firebase_url + '/' + temperature_location + '/temperature.json', data=json.dumps(data))

         #Insert record
         print 'Record inserted. Result Code = ' + str(result.status_code) + ',' + result.text
         time.sleep(fixed_interval)

    except IOError:
        print('Error! Something went wrong.')
        time.sleep(fixed_interval)
Run Code Online (Sandbox Code Playgroud)

如何修改它以检索数据?

小智 7

安装

pip install firebase
Run Code Online (Sandbox Code Playgroud)

蟒蛇版

Firebase 是为 python 3 及更高版本编写的,不能与 python 2 一起正常工作。

将 Firebase 添加到您的应用程序

您的 Google 的 Firebase 配置数据可以在Firebase > 设置 > 项目设置滚动到底部> 添加到网络应用程序 > 配置上找到

为了仅用于基于用户的身份验证,我们可以创建以下配置:

pip install firebase
Run Code Online (Sandbox Code Playgroud)

验证

sign_in_with_email_and_password()方法将返回用户数据,包括可用于遵守安全规则的令牌。

每个下列方法接受令牌的用户:get()push()set()update()remove()stream()

from firebase import Firebase

config = {
  "apiKey": "apiKey",
  "authDomain": "projectId.firebaseapp.com",
  "databaseURL": "https://databaseName.firebaseio.com",
  "storageBucket": "projectId.appspot.com"
}

firebase = Firebase(config)
Run Code Online (Sandbox Code Playgroud)

数据库

您可以使用该child()方法构建数据路径。

# Get a reference to the auth service
auth = firebase.auth()

# Log the user in
user = auth.sign_in_with_email_and_password(email, password)

# Get a reference to the database service
db = firebase.database()

# data to save
data = {
    "name": "Joe Tilsed"
}

# Pass the user's idToken to the push method
results = db.child("users").push(data, user['idToken'])
Run Code Online (Sandbox Code Playgroud)

保存数据

要使用唯一的、自动生成的、基于时间戳的密钥保存数据,请使用push()方法。

db = firebase.database()
db.child("users").child("Joe")
Run Code Online (Sandbox Code Playgroud)

要创建自己的密钥,请使用该set()方法。下面示例中的键是“Joe”。

data = {"name": "Joe Tilsed"}
db.child("users").push(data)
Run Code Online (Sandbox Code Playgroud) 更新

要更新现有条目的数据,请使用该update()方法。

data = {"name": "Joe Tilsed"}
db.child("users").child("Joe").set(data)
Run Code Online (Sandbox Code Playgroud) 消除

要删除现有条目的数据,请使用remove()方法。

db.child("users").child("Joe").update({"name": "Joe W Tilsed"})
Run Code Online (Sandbox Code Playgroud)

检索数据

查询返回一个 FirebaseResponse 对象。调用val()这些对象返回查询数据。

users = db.child("users").get()
print(users.val())

>> {"Joe": {"name": "Joe Tilsed"}, "Syd": {"name": "Sydney Cox"}}
Run Code Online (Sandbox Code Playgroud) 钥匙

调用key()返回查询数据的键。

user = db.child("users").get()
print(user.key())

>> users
Run Code Online (Sandbox Code Playgroud) 每个

返回一个对象列表,您可以在每个对象上调用val()key()

all_users = db.child("users").get()
for user in all_users.each():
    print(user.key())

    >> Joe 

    print(user.val())   

    >> {name": "Joe Tilsed"}
Run Code Online (Sandbox Code Playgroud) 得到

要从路径返回数据,只需调用该get()方法。

db.child("users").child("Joe").remove()
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。完整文档:https : //pypi.org/project/firebase/


小智 1

我有这样的东西正在工作

firebase_url = ' https://xyz.firebaseio.com/ '

client_name = '孟买-坎迪瓦利'

类 MainPage(webapp.RequestHandler):

def get(self):
    self.response.headers['Content-Type'] = 'application/json'     
    url = firebase_url + '/' + client_name + '/temperature.json'        
    json_object = json.load(urllib2.urlopen(url))
    json.dump(json_object,  self.response.out ) #this will go to frontend
Run Code Online (Sandbox Code Playgroud)

希望这有帮助...但是我正在努力在 python 中获取“import firebase”,然后执行操作