1+ from turtle import Screen
2+
13# sj-firebase-python
24 Implement Firebase Java SDK in Python
35
1113## Usage
1214### Buildozer Android project
1315``` properties
14- android.gradle_dependencies = io.github.simplejnius:sjfirebase:0 .3.0
15- requirements = https://github.com/SimpleJnius/sj-firebase-python/archive/refs/heads/master.zip
16+ android.gradle_dependencies = io.github.simplejnius:sjfirebase:1 .3.2
17+ requirements = sjfirebase
1618```
1719The current version of [ python-for-android] ( https://github.com/kivy/python-for-android )
1820lacks support for incorporating bom dependencies, modifying the classpath, and copying the ` google-service.json ` .
@@ -22,7 +24,7 @@ make adjustments to the specified section using the provided values below:
2224``` properties
2325android.api = 34
2426android.enable_androidx = True
25- android.gradle_dependencies = io.github.simplejnius:sjfirebase:1.0.0 ,
27+ android.gradle_dependencies = io.github.simplejnius:sjfirebase:1.3.2 ,
2628 com.google.firebase:firebase-auth,com.google.firebase:firebase-database,
2729 com.google.firebase:firebase-firestore,com.google.firebase:firebase-storage,
2830 com.google.firebase:firebase-analytics
@@ -39,6 +41,113 @@ pip install sjfirebase
3941# buildozer.spec
4042requirements = sjfirebase
4143```
44+
45+ ## Sample Code Documentation
46+
47+ ** To add, set, update, and delete a document in a collection**
48+ ``` python
49+ from kivy.uix.screenmanager import Screen # noqa
50+ from sjfirebase.tools.mixin import FirestoreMixin
51+
52+
53+ class MyScreen (Screen , FirestoreMixin ):
54+
55+ def on_event1 (self ):
56+ data = dict (name = self .ids.name.text, email = self .ids.email.text)
57+
58+ # creates a new document in "individual" collection and
59+ # passes a success(True/False) and error(None or FirebaseException)
60+ # parameters to your callback
61+ self .add_document(" individuals" , data, lambda success , error : print (success, error))
62+
63+ def on_event2 (self ):
64+ data = dict (name = self .ids.name.text, email = self .ids.email.text)
65+
66+ # creates a new or overwrite document "custom_document_id" in collection
67+ # and passes a success(True/False) and error(None or FirebaseException)
68+ # parameters to your callback
69+ self .set_document(" individual/custom_document_id" , data, lambda success , error : print (success, error))
70+
71+ def on_event3 (self ):
72+ data = dict (name = self .ids.name.text, email = self .ids.email.text)
73+
74+ # creates a new or update document "custom_document_id" in collection
75+ # and passes a success(True/False) and error(None or FirebaseException)
76+ # parameters to your callback
77+ self .set_document(" individual/custom_document_id" , data, lambda success , error : print (success, error), merge = True )
78+
79+ def on_event4 (self ):
80+ data = [" name" , self .ids.name.text, " email" , self .ids.email.text]
81+
82+ # creates a new or overwrite document "custom_document_id" in collection
83+ # and passes a success(True/False) and error(None or FirebaseException)
84+ # parameters to your callback.
85+ # Notice the data is a list, and this list must be an even length and not odd length.
86+ # Every odd values are the key while the even values are the value.
87+ # E.g in the above `data` `name` is the key while `self.ids.name.text` is the value
88+ # same goes with the email. It's this way due to the varargs nature of the Firestore `update` method
89+ # signature
90+ self .update_document(" individual/custom_document_id" , data, lambda success , error : print (success, error))
91+
92+ def on_event5 (self ):
93+ # deletes document "custom_document_id"
94+ self .delete_document(" individual/custom_document_id" , lambda success , error : print (success, error))
95+
96+ # NB: You can instantiate the FirestoreMixing directly
97+ # E.g
98+ f = FirestoreMixin()
99+ f.add_document(... )
100+ f.set_document(... )
101+ # etc..
102+ ```
103+
104+ ** To get-data, paginate-data, and get-realtime-updates**
105+ ``` python
106+ from kivy.uix.screenmanager import Screen # noqa
107+ from sjfirebase.tools.mixin import FirestoreMixin
108+
109+
110+ class MyScreen (Screen , FirestoreMixin ):
111+ def on_event1 (self ):
112+ # gets a dictionary of data from "document_id" if any data and passes
113+ # `success` (True/False) and `data` (dict/errorMessage).
114+ # always check if success is True before accessing items from data,
115+ # or check if data is an instance of dict. Else log the data to see the error message
116+ self .get_document(" individual/document_id" , lambda success , data : print (success, data))
117+
118+ def on_event2 (self ):
119+ # everything @on_event1 plus `source` that states where the data should be fetched.
120+ # either cache, server or default. cache gets from firebase data stored locally
121+ # on the device for offline purposes while server gets from firebase cloud directly.
122+ # default can be either cache or cloud depending on your configuration.
123+ self .get_document(" individual/document_id" , lambda success , data : print (success, data), source = " cache" )
124+
125+ def on_event3 (self ):
126+ # gets all document at "individual" collection and passes success and data argument.
127+ # here, data is a list of dict. before accessing, refer to @on_event1
128+ self .get_collection_of_documents(" individual" , lambda success , data : print (success, data))
129+
130+ def on_event4 (self ):
131+ # refer to @on_event2 for "source" and @on_event3 for functionality description
132+ self .get_collection_of_documents(" individual" , lambda success , data : print (success, data), source = " cache" )
133+
134+ def on_event5 (self ):
135+ # refer to @on_event3 for functionality description. The new thing here is `limit`.
136+ # limit is used to limit the amount of data you fetch from firebase and paginate as you go.
137+ self .get_pagination_of_documents(" individual" , 10 , lambda success , data : print (success, data))
138+
139+ def on_event6 (self ):
140+ # listens for document changes and returns a dict of data and location
141+ # where the data is coming from. `data`(dict/errorMessage), where("Local"/"Server")
142+ self .add_document_snapshot_listener(" individual/document_id" , lambda data , where : print (data, where))
143+
144+ def on_event7 (self ):
145+ # same as @on_event6 but gets a list of document with information on whether they were
146+ # added, removed or modified
147+ self .add_collection_snapshot_listener(" individual" , lambda data , where : print (data, where))
148+ ```
149+
150+
42151### Python API
43152#### ActionCodeSettings
44153``` python
0 commit comments