Google Firebase - steviep42/immport GitHub Wiki

This page contains some basic information for using Google Firebase which is a NoSQL setup. This can be used in at least two modes:

1) Web

The first is directly on the web within the Google Cloud console. You can click the "hamburger" icon and then scroll down to Firebase. From there you can create a database (choose Native mode unless you want to bring in some of the older Cloud Store databases). This will allow you to create a "collection" which contains "documents" which contains "fields". Usually, you would not enter in new documents or add new collections by hand. Some type of batch load would be used to deal with lots of data, but if you are just learning how to work with Google Firebase then experimenting with the web interface is useful.

As an example, here is a screen shot of a trivial "database" in Firebase that tracks users. The way this works is that there is a "collection" of "users" each of which has a "document" name that in turn has a number of "fields". Note that each document need not have the same number of type of fields as another but frequently there are similarities if not identities.

https://github.com/steviep42/immport/blob/master/images/fire1.png

2) From the SDK

You can create new "collections" or access existing ones from the firebase software development kit. This requires installation of the firebase_admin module for Python onto your laptop or desktop environment. I use the conda tool to help manage python on my Apple OSX machine as it doesn't mess with the default version of Python on my laptop. Using conda allows you to maintain any number of virtual environments for varying versions of Python. Some people use the homebrew package manager but I've always run into problems with that. If you are using conda it turns out that the firebase_admin is not packaged into any of the conda channels (not a surprise) so just use pip to install this. Of course, make sure you have already activated an appropriate conda environment.

 $ pip install firebase-admin

Allowing Access To An Existing Project

To use the SDK first requires some activities on the Google Firebase Console to allow access via the SDK installed on your desktop. Here is how to do that. Go to the Firebase Console at https://console.firebase.google.com/ From there you should see your project. If not, you will need to create one. Here is what mine looks like from the console. In this case, I would click "test project" to see more particulars. 1) Click the Projecting Settings icon then 2) click "Service Accounts" and then 3) click "Generate New Private Key".

This will prompt you to save a JSON file containing all relevant information for the project. This is not to be published so stash it in a safe place. You will make reference to this file when you access the project from Python. On my Mac, I put it onto my Downloads folder. In my case, the name of the file is the following though in your case it will be different depending on your project.

test-project-221120-firebase-adminsdk-hvdi9-89d67d6d3e.json

https://github.com/steviep42/immport/blob/master/images/fire3.png

Accessing The Project From Python

Okay, now back on your laptop, in my case an Apple, startup Python, IPython or maybe even a Jupyter notebook. This is just for basic testing so there is no specific need for one over the other. So use the following approach to connect and query the "collection". In this case I'm wanting to find specific information relating to the document named "steve".

 (base) Steves-MacBook-Pro:Downloads esteban$ python
 Python 3.7.1 (default, Dec 14 2018, 13:28:58) 
 [Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import firebase_admin
 >>> from firebase_admin import credentials, firestore
 >>> cred = credentials.Certificate('/Users/esteban/Downloads/test-project-221120-firebase-adminsdk-  hvdi9-89d67d6d3e.json')
 >>> default_app = firebase_admin.initialize_app(cred)
 >>> db = firestore.client()
 >>> doc_ref = db.collection(u'users').document(u'steve')
 >>> doc = doc_ref.get()
 >>> doc
 <google.cloud.firestore_v1.document.DocumentSnapshot object at 0x10bd888d0>
 >>> print(u'Document data: {}'.format(doc.to_dict()))
 Document data: {'first': 'steve', 'last': 'Pitt', 'uname': 'wsp'}

More Query Examples

Check the documentation located at https://cloud.google.com/firestore/docs/query-data/queries for various examples and how to generally use the API. The idea is to treat Firestore as the back end for your application and create queries from an app. Obviously, this app might be developed into something that itself is hosted within Google, such as an app fronted by a web front end, but for developmental purposes the Python -> API -> Firebase approach seems to be the way to go.

>>> docs = users_ref.where(u'first',u'==',u'Mike').stream()
>>> for doc in docs:
...     print(u'{} => {}'.format(doc.id, doc.to_dict()))
... 
mike => {'uname': 'mkwilson', 'first': 'Mike', 'last': 'Wilson'}