Lab 09_Dashboard Using Firebase, Plotly and Flask
Lab 09_Dashboard Using Firebase, Plotly and Flask
▪ The Firebase Admin SDK provides a powerful set of tools for interacting
with Firebase services, including the Realtime Database.
▪ In Python, you can use the Firebase Admin SDK to access and manipulate
data in the Firebase Realtime Database.
▪ To use Admin Database API in Python, we need to install the following.
pip install firebase-admin
Firebase Admin SDK
▪ This will download a JSON file that contains the private key.
▪ For simplicity, rename the file as key.json.
Firebase Admin SDK: Imports
▪ Initialize the Firebase Admin SDK with the provided credentials and
database URL.
firebase_admin.initialize_app(cred,
{'databaseURL':
'https://smart-home-6f8b2-default-rtdb.firebaseio.com/'})
Firebase Admin SDK: Getting Data from the Realtime Database
▪ This code retrieves the current data stored at the office child node.
ref.child('office').get()
▪ This code retrieves the current data stored at the room child node.
ref.child('room').get()
Out: {'fan': 'on', 'led': 'on', 'temp': 25, 'tv': 'on'}
Firebase Admin SDK: Getting Data from the Realtime Database
▪ This code retrieves the current data stored at the tv child node under the
room child node.
ref.child('room').child('tv').get()
Out: 'on'
Out: 'on'
Firebase Admin SDK: Sending Data to the Realtime Database
▪ This code sets the value of the led child node under the office child node
to “on” in the Firebase Realtime Database.
ref.child('office/led').set("on")
Firebase Admin SDK: Sending Data to the Realtime Database
▪ These lines of code add and set the values of the gas_level and oven
child nodes under the kitchen node to “low” and “off ”, respectively.
ref.child('kitchen/gas_level').set("low")
ref.child('kitchen/oven').set("off")
Firebase Admin SDK: Sending Data to the Realtime Database
▪ If the data does not exist, calling set() will add a new node with the data.
▪ If data already exists, calling set() will overwrite it with the new data.
ref.child('temp_sensor').set(25)
ref.child('temp_sensor').set(27)
Firebase Admin SDK: Sending Data to the Realtime Database
▪ This code deletes the temp_sensor node from the Realtime Database.
{
"kitchen": {
"gas_level": "low",
"oven": "off"
},
"office": {
"fan": "off",
"led": "on",
"temp": 28
},
"room": {
"fan": "on",
"led": "on",
"temp": 25,
"tv": "on"
}
}
Firebase Admin SDK: Sending Data to the Realtime Database
▪ The push() function is used to generate a unique key for a new child node
and then set data at that location.
▪ The following will add five child nodes to the temp_sensor ensures
uniqueness of keys.
▪ The push() is used when you want to add data in a way that ensures
uniqueness of keys and preserves order.
Firebase Admin SDK: Getting and Preprocessing Data
Out:
{'-NwSfZyWwTj6enKMsg_J': {'temp': 26, 'timestamp': 1},
'-NwSf_2SHOxEjoh3SV_S': {'temp': 27, 'timestamp': 2},
'-NwSf_7I6WQMgDa57Fje': {'temp': 28, 'timestamp': 3},
'-NwSf_C0rNmz2wSOnXDh': {'temp': 26, 'timestamp': 4},
'-NwSf_GmdxwhQlYD0IZN': {'temp': 25, 'timestamp': 5}}
Firebase Admin SDK: Getting and Preprocessing Data
▪ This code iterates over the items in the temp_sensor dictionary retrieved
from the Firebase Realtime Database.
▪ For each child node under temp_sensor, it extracts the temp and
timestamp values and appends them to separate lists.
timestamp = []
temp = []
In: timestamp
Out: [1, 2, 3, 4, 5]
In: temp
Out: [26, 27, 28, 26, 25]
Plotly Python
▪ Converting the figure to JSON format is useful while rendering the figure
in HTML pages.
fig_json = fig.to_json()
Flask Web Application Framework
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
if __name__ == "__main__":
app.run()
Simple Dashboard Using Plotly and Flask
▪ This function retrieves temperature data from a Firebase and returns the
timestamps and temperature values as lists.
def get_temp_values():
ref = db.reference('/')
temp_sensor = ref.child('temp_sensor').get()
timestamp = []
temp = []
return fig
Simple Dashboard Using Plotly and Flask
from flask import Flask, render_template
# Render the HTML template 'index.html', passing the Plotly figure JSON data
return render_template('index.html', fig_json=fig_json)
<head>
<title>IoT Dashboard</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id="temp_fig"></div>
<script>
var fig_json = {{ fig_json | safe }};
Plotly.newPlot('temp_fig', fig_json.data, fig_json.layout);
</script>
</body>
</html>
Simple Dashboard Using Plotly and Flask