Create an account on Firebase and a project
Once you created an account on firebase, you can create your project.
This is the page of your database with all its children.
To use it with Python, you need to install some modules. With Python, usinf Firebase becomes really easy.
Create an account on firebase
After that create a database. I will add some more information in the future about this.
We will need, then, to install some modules:
- firebase
- python_jwt
- gcloud
- sseclient
- pycriptodome
- request_toolbelt
Quite a lot of module, right? At the end, you should be ready to start.
Put some data in the database
I assume you did create the database. Now let’s connect to it. First we need to install firebase.
Then install python_jwt
and then gcloud
Then sseclient
and pycryptodome
and requests_toolbelt
… we’re done… now it works
Import firebase
First we import firebase
from firebase import firebase
Connect to the database
Then we connect to our database (put your database name instead of your_db_name:
firebase = firebase.FirebaseApplication("https://your_db_name.firebaseio.com/")
Add a record and a value to a child
Let’s make it the most simple that we can
from firebase import firebase # put the name of your database where the ***** are fb = firebase.FirebaseApplication("https://*****.firebaseio.com/") fb.put("books", "Pinocchio", { "author" : "Collodi", "Edition" : 1968} )
The “book” is the child of the database. “Pinocchio” is the name of a record and its values are in a dictionary that has 2 keys “author” and “Edition”. You can also use numbers as the value, or strings, or lists or dictionaries. Take a look at the video below to see how to insert values easily. In the next post we will see how to read the values from the database instead of writing them.
How to retrieve the data
To read, or retrieve, the data that you just stored, you need to use the get function that takes 2 argument, the address of the database (“https://*****.firebaseio.com/”) and the name of the child (“books”):
address = "https://*****.firebaseio.com/"" print(fb.get(address, "books"))
The output will be this:
{'Pinocchio': {'Edition': 1968, 'author': 'Collodi'}}
Delete every data in the database
In case you want to delete all the data (!), you can use this code:
from firebase import firebase # substitute <name_of_your_database> with the actual name, without angular parenthesis fb = firebase.FirebaseApplication("https://<name_of_database>.firebaseio.com/") def delete(): "Deletes everything" fb.delete("/", None) delete()
A quick look to the app
Tkinter to create a GUI for firebase
This little script let us look to the records into firebase. We need to add a way to add the records. Let’s see the code until now:
from firebase import firebase # put the name of your database where the ***** are address = "https://*******.firebaseio.com/" fb = firebase.FirebaseApplication(address) fb.put("books", "Pinocchio", { "author" : "Collodi", "Edition" : 1968} ) import tkinter as tk root = tk.Tk() def entry(text): l = tk.Label(text=text) l.pack() e = tk.Entry(root) e.pack() author = entry("Author") edition = entry("Edition") def seedata(): data2 = data[lbx.get(lbx.curselection())] for k in data2: lbx2.insert(tk.END, k + ": " + str(data2[k])) lbx = tk.Listbox(root) lbx.pack() data = fb.get(address, "books") for k in data: lbx.insert(tk.END, k) lbx2 = tk.Listbox(root) lbx2.pack() lbx.bind("<<ListboxSelect>>", lambda x: seedata()) root.mainloop()
Now we need the code to add the records.
How to get the data in javascript
To get the data in an html page you need some code like this (you nedd the confid data from your firebase project):
<div id="board"></div> <script src="https://www.gstatic.com/firebasejs/4.10.1/firebase.js"></script> <script> var config = { apiKey: "AHfjhdjskhdlgòjkjgkòsjdklfgjlskdjfgkljdf", authDomain: "showrfjkjksdf-546.firebaseapp.com", databaseURL: "https://showrfjkjksdf-546.firebaseio.com/", projectId: "showrfjkjksdf-546", storageBucket: "showrfjkjksdf-546.appspot.com", messagingSenderId: "4546768786" }; firebase.initializeApp(config); let database = firebase.database(); let ref = database.ref().child("my_data_to_read"); // Scrivi da dove vuoi leggere i dati ref.on("value", gotData, errData); function gotData(data){ board.innerHTML = ""; x = data.val(); for (n in x){ board.innerHTML += n + ": " + x[n] + "<br>" } } function errData(err){ console.log("Error"); console.log(err); } </script>
You will get the var config data from your project. Then you will nedd to add the child you want to read the data from in:
let ref = database.ref().child("my_data_to_read"); // Scrivi da dove vuoi leggere i dati
my_data_to_read is the child.
The following code will read the data and show them in the div “board”:
ref.on("value", gotData, errData); // sends the values to the function got data function gotData(data){ board.innerHTML = ""; x = data.val(); for (n in x){ // iterate all the data adding each one in board board.innerHTML += n + ": " + x[n] + "<br>" } }