jrieke / streamlit-analytics

👀 Track & visualize user interactions with your streamlit app
MIT License
264 stars 49 forks source link

Saving to Firestore fails for `None` values #3

Closed jucor closed 2 years ago

jucor commented 3 years ago

Hi! Quite excited at the prospect of using streamlit-analytics, but with streamlit-analytics 0.2.2 I get a bad error when calling

sa.stop_tracking(firestore_key_file="firestore-key.json", firestore_collection_name="counts") 

The following error gets displayed in my app:

TypeError: '<' not supported between instances of 'str' and 'NoneType'
Traceback:
File "/opt/conda/lib/python3.6/site-packages/streamlit/script_runner.py", line 337, in _run_script
    exec(code, module.__dict__)
File "/root/xray/web_demo_streamlit.py", line 244, in <module>
    firestore_collection_name="counts")
File "/opt/conda/lib/python3.6/site-packages/streamlit_analytics/main.py", line 381, in stop_tracking
    firestore.save(counts, firestore_key_file, firestore_collection_name)
File "/opt/conda/lib/python3.6/site-packages/streamlit_analytics/firestore.py", line 24, in save
    doc.set(counts)  # creates if doesn't exist
File "/opt/conda/lib/python3.6/site-packages/google/cloud/firestore_v1/document.py", line 166, in set
    batch, kwargs = self._prep_set(document_data, merge, retry, timeout)
File "/opt/conda/lib/python3.6/site-packages/google/cloud/firestore_v1/base_document.py", line 211, in _prep_set
    batch.set(self, document_data, merge=merge)
File "/opt/conda/lib/python3.6/site-packages/google/cloud/firestore_v1/base_batch.py", line 98, in set
    reference._document_path, document_data
File "/opt/conda/lib/python3.6/site-packages/google/cloud/firestore_v1/_helpers.py", line 697, in pbs_for_set_no_merge
    extractor = DocumentExtractor(document_data)
File "/opt/conda/lib/python3.6/site-packages/google/cloud/firestore_v1/_helpers.py", line 497, in __init__
    for field_path, value in iterator:
File "/opt/conda/lib/python3.6/site-packages/google/cloud/firestore_v1/_helpers.py", line 444, in extract_fields
    for s_path, s_value in extract_fields(value, field_path):
File "/opt/conda/lib/python3.6/site-packages/google/cloud/firestore_v1/_helpers.py", line 444, in extract_fields
    for s_path, s_value in extract_fields(value, field_path):
File "/opt/conda/lib/python3.6/site-packages/google/cloud/firestore_v1/_helpers.py", line 434, in extract_fields
    for key, value in sorted(document_data.items()):

Any idea what I'm missing, please?

jucor commented 3 years ago

I think I found the problem: some of my radio button values were None. When not using Firestore I just get a warning about this object not being serializable to JSON, but when using Firestore I have the crash above.

Fixed it by: 1/ changing my app logic so they are a regular string instead. 2/ deactivating Firestore so I could see the analytics and therefore the "reset" button 3/ reactivating Firestore (note: restarting streamlit instead of "Re-run" would have been as efficient as 2 and 3 :) ).

It could be useful to have a nice warning instead of the error, by recycling the same check done for seralizability.

jrieke commented 2 years ago

Hey! I replaced None values by empty strings now. Hope this should fix it, can't test right now because I don't have a Firestore instance set up. Feel free to reopen if the issue persists!