{"id":3939,"date":"2019-10-08T21:35:24","date_gmt":"2019-10-08T19:35:24","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=3939"},"modified":"2019-10-20T09:05:17","modified_gmt":"2019-10-20T07:05:17","slug":"3939-2","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/3939-2\/","title":{"rendered":"Tkinter and Firebase (and javascript) to get data"},"content":{"rendered":"<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<p>&nbsp;<\/p>\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":"An app to retrieve the data from firebase database with Python and tkinter and also some javascript code to retrieve data with an html page.\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/3939-2\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":3940,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[370,331,49],"tags":[239,292,600,233,588,601],"class_list":["post-3939","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-html","category-javascript","category-tkinter","tag-database","tag-firebase","tag-get-data","tag-javascript","tag-retrieve-data","tag-show-data"],"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\/3939","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=3939"}],"version-history":[{"count":5,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3939\/revisions"}],"predecessor-version":[{"id":3993,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3939\/revisions\/3993"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/3940"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=3939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=3939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=3939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}