{"id":3750,"date":"2019-09-22T14:04:30","date_gmt":"2019-09-22T12:04:30","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=3750"},"modified":"2019-12-04T14:54:58","modified_gmt":"2019-12-04T13:54:58","slug":"firebase-with-pyhon-the-free-google-database","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/firebase-with-pyhon-the-free-google-database\/","title":{"rendered":"Firebase with Pyhon: the free Google database"},"content":{"rendered":"<h2>Create an account on Firebase and a project<\/h2>\n<p>Once you created an account on <a href=\"https:\/\/console.firebase.google.com\/u\/0\/\">firebase<\/a>, you can create your project.<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/002-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3774 aligncenter\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/002-1.png\" alt=\"\" width=\"305\" height=\"237\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/002-1.png 522w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/002-1-320x248.png 320w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/a><\/p>\n<p>This is the page of your database with all its children.<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/001.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3773 aligncenter\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/001.png\" alt=\"\" width=\"427\" height=\"302\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/001.png 794w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/001-320x226.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/001-768x543.png 768w\" sizes=\"auto, (max-width: 427px) 100vw, 427px\" \/><\/a><\/p>\n<p>To use it with <strong>Python<\/strong>, you need to install some modules. With Python, usinf <strong>Firebase<\/strong> becomes really <strong>easy<\/strong>.<\/p>\n<h2>Create an account on firebase<\/h2>\n<p>After that create a database. I will add some more information in the future about this.<\/p>\n<p>We will need, then, to install some modules:<\/p>\n<ul>\n<li>firebase<\/li>\n<li>python_jwt<\/li>\n<li>gcloud<\/li>\n<li>sseclient<\/li>\n<li>pycriptodome<\/li>\n<li>request_toolbelt<\/li>\n<\/ul>\n<p>Quite a lot of module, right? At the end, you should be ready to start.<\/p>\n<h2>Put some data in the database<\/h2>\n<p>I assume you did create the database. Now let&#8217;s connect to it. First we need to install <strong>firebase<\/strong>.<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/Cattura.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3751 aligncenter\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/Cattura.png\" alt=\"\" width=\"547\" height=\"198\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/Cattura.png 793w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/Cattura-320x116.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/Cattura-768x278.png 768w\" sizes=\"auto, (max-width: 547px) 100vw, 547px\" \/><\/a><\/p>\n<p>Then install <strong>python_jwt<\/strong><\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/003.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3754 aligncenter\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/003.png\" alt=\"\" width=\"463\" height=\"185\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/003.png 899w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/003-320x128.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/003-768x308.png 768w\" sizes=\"auto, (max-width: 463px) 100vw, 463px\" \/><\/a><\/p>\n<p>and then <strong>gcloud<\/strong><\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/004.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3755 aligncenter\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/004.png\" alt=\"\" width=\"499\" height=\"179\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/004.png 897w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/004-320x115.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/004-768x276.png 768w\" sizes=\"auto, (max-width: 499px) 100vw, 499px\" \/><\/a><\/p>\n<p>Then <strong>sseclient<\/strong><\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/005.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3756 aligncenter\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/005.png\" alt=\"\" width=\"493\" height=\"177\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/005.png 897w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/005-320x115.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/005-768x276.png 768w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><\/a><\/p>\n<p>and <strong>pycryptodome<\/strong><\/p>\n<p>and <strong>requests_toolbelt<\/strong><\/p>\n<p>&#8230; we&#8217;re done&#8230; now it works<\/p>\n<h2>Import firebase<\/h2>\n<p>First we import firebase<\/p>\n<pre class=\"lang:default decode:true\">from firebase import firebase<\/pre>\n<h2>Connect to the database<\/h2>\n<p>Then we connect to our database (put your database name instead of <strong>your_db_name<\/strong>:<\/p>\n<pre class=\"lang:default decode:true\">firebase = firebase.FirebaseApplication(\"https:\/\/your_db_name.firebaseio.com\/\")<\/pre>\n<h2>Add a record and a value to a child<\/h2>\n<p>Let&#8217;s make it the most simple that we can<\/p>\n<pre class=\"lang:default decode:true\">from firebase import firebase\r\n\r\n# put the name of your database where the ***** are\r\nfb = firebase.FirebaseApplication(\"https:\/\/*****.firebaseio.com\/\")\r\n\r\nfb.put(\"books\",\r\n\t\"Pinocchio\", {\r\n\t\t\"author\" : \"Collodi\",\r\n\t\t\"Edition\" : 1968}\r\n\t   )<\/pre>\n<p>The &#8220;book&#8221; is the <strong>child<\/strong> of the database. &#8220;<strong>Pinocchio<\/strong>&#8221; is the name of a <strong>record<\/strong> and its values are in a <strong>dictionary<\/strong> that has <strong>2 keys<\/strong> &#8220;author&#8221; and &#8220;Edition&#8221;. You can also use <strong>numbers<\/strong> as the value, or <strong>strings<\/strong>, or <strong>lists<\/strong> or <strong>dictionaries<\/strong>. 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.<\/p>\n<h2>How to retrieve the data<\/h2>\n<p>To read, or <strong>retrieve<\/strong>, the data that you just stored, you need to use the get function that takes 2 argument, the address of the database (&#8220;https:\/\/*****.firebaseio.com\/&#8221;) and the name of the child (&#8220;books&#8221;):<\/p>\n<pre class=\"lang:default decode:true\">address = \"https:\/\/*****.firebaseio.com\/\"\"\r\nprint(fb.get(address, \"books\"))<\/pre>\n<p>The output will be this:<\/p>\n<pre class=\"lang:default decode:true \">{'Pinocchio': {'Edition': 1968, 'author': 'Collodi'}}<\/pre>\n<h2>Delete every data in the database<\/h2>\n<p>In case you want to delete all the data (!), you can use this code:<\/p>\n<pre class=\"lang:default decode:true\">from firebase import firebase\r\n\r\n# substitute &lt;name_of_your_database&gt; with the actual name, without angular parenthesis\r\nfb = firebase.FirebaseApplication(\"https:\/\/&lt;name_of_database&gt;.firebaseio.com\/\")\r\n\r\ndef delete():\r\n    \"Deletes everything\"\r\n    fb.delete(\"\/\", None)\r\n\r\ndelete()<\/pre>\n<h2>A quick look to the app<\/h2>\n<p><iframe loading=\"lazy\" title=\"Python and Firebase 1\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/U5_P1NbEZgc?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h2>Tkinter to create a GUI for firebase<\/h2>\n<p>This little script let us look to the records into firebase. We need to add a way to add the records. Let&#8217;s see the code until now:<\/p>\n<pre class=\"lang:default decode:true \">from firebase import firebase\r\n\r\n# put the name of your database where the ***** are\r\naddress = \"https:\/\/*******.firebaseio.com\/\"\r\nfb = firebase.FirebaseApplication(address)\r\n\r\nfb.put(\"books\",\r\n\t\"Pinocchio\", {\r\n\t\t\"author\" : \"Collodi\",\r\n\t\t\"Edition\" : 1968}\r\n\t   )\r\n\r\nimport tkinter as tk\r\n\r\nroot = tk.Tk()\r\n\r\ndef entry(text):\r\n\tl = tk.Label(text=text)\r\n\tl.pack()\r\n\te = tk.Entry(root)\r\n\te.pack()\r\n\r\nauthor = entry(\"Author\")\r\nedition = entry(\"Edition\")\r\n\r\ndef seedata():\r\n\tdata2 = data[lbx.get(lbx.curselection())]\r\n\tfor k in data2:\r\n\t\tlbx2.insert(tk.END, k + \": \" + str(data2[k]))\r\n\r\nlbx = tk.Listbox(root)\r\nlbx.pack()\r\ndata = fb.get(address, \"books\")\r\nfor k in data:\r\n\tlbx.insert(tk.END, k)\r\nlbx2 = tk.Listbox(root)\r\nlbx2.pack()\r\nlbx.bind(\"&lt;&lt;ListboxSelect&gt;&gt;\", lambda x: seedata())\r\n\r\nroot.mainloop()<\/pre>\n<p>The app..<a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/firebaseapp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-3937 aligncenter\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/firebaseapp.png\" alt=\"\" width=\"126\" height=\"440\" \/><\/a><\/p>\n<p>Now we need the code to add the records.<\/p>\n<h2>How to get the data in javascript<\/h2>\n<p>To get the data in an html page you need some code like this (you nedd the confid data from your firebase project):<\/p>\n<pre class=\"lang:default decode:true\">&lt;div id=\"board\"&gt;&lt;\/div&gt;\r\n&lt;script src=\"https:\/\/www.gstatic.com\/firebasejs\/4.10.1\/firebase.js\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\nvar config = {\r\n  apiKey: \"AHfjhdjskhdlg\u00f2jkjgk\u00f2sjdklfgjlskdjfgkljdf\",\r\n  authDomain: \"showrfjkjksdf-546.firebaseapp.com\",\r\n  databaseURL: \"https:\/\/showrfjkjksdf-546.firebaseio.com\/\",\r\n  projectId: \"showrfjkjksdf-546\",\r\n  storageBucket: \"showrfjkjksdf-546.appspot.com\",\r\n  messagingSenderId: \"4546768786\"\r\n};\r\nfirebase.initializeApp(config);\r\nlet database = firebase.database();\r\nlet ref = database.ref().child(\"my_data_to_read\"); \/\/ Scrivi da dove vuoi leggere i dati\r\nref.on(\"value\", gotData, errData);\r\n\r\nfunction gotData(data){\r\n    board.innerHTML = \"\";\r\n    x = data.val();\r\n    for (n in x){\r\n      board.innerHTML += n + \": \" + x[n] + \"&lt;br&gt;\"\r\n    }\r\n}\r\n\r\nfunction errData(err){\r\n    console.log(\"Error\");\r\n    console.log(err);\r\n}\r\n&lt;\/script&gt;<\/pre>\n<p>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:<\/p>\n<pre class=\"lang:default decode:true \">let ref = database.ref().child(\"my_data_to_read\"); \/\/ Scrivi da dove vuoi leggere i dati<\/pre>\n<p>my_data_to_read is the child.<\/p>\n<p>The following code will read the data and show them in the div &#8220;board&#8221;:<\/p>\n<pre class=\"lang:default decode:true \">ref.on(\"value\", gotData, errData); \/\/ sends the values to the function got data\r\n\r\nfunction gotData(data){\r\n    board.innerHTML = \"\";\r\n    x = data.val();\r\n    for (n in x){\r\n      \/\/ iterate all the data adding each one in board\r\n      board.innerHTML += n + \": \" + x[n] + \"&lt;br&gt;\"\r\n    }\r\n}<\/pre>\n<h4>Firebase<\/h4>\r\n<script>\r\nvar title = \"Firebase\";\r\nvar links = [\r\n[\"https:\/\/pythonprogramming.altervista.org\/firebase-with-pyhon-the-free-google-database\/\",\"Firebase\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/3939-2\/\",\"Tkinter and Firebase\"]\r\n\t\t];\t\t \r\n<\/script>\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\r\n\n","protected":false},"excerpt":{"rendered":"Use the free database Firebase with Python in no time\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/firebase-with-pyhon-the-free-google-database\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":3759,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[479,1,571],"tags":[489,292,233,4,602],"class_list":["post-3750","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database","category-examples","category-firebase","tag-data","tag-firebase","tag-javascript","tag-python","tag-retrieve"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3750","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=3750"}],"version-history":[{"count":16,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3750\/revisions"}],"predecessor-version":[{"id":4247,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3750\/revisions\/4247"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/3759"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=3750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=3750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=3750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}