444B / streamlit-analytics2

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

[BUG] Cannot store multipage analytics in Firestore: `ValueError: One or more components is not a string or is empty.` #66

Closed miguelbalmeida closed 3 months ago

miguelbalmeida commented 3 months ago

Describe the bug I have a deployed multipage Streamlit app and am trying to store the results of streamlit-analytics2 in Google Firestore, so that they persist when the app is re-deployed. I added this line on one of the pages of the multipage app:

with streamlit_analytics.track(
    firestore_key_file="...",
    firestore_collection_name="..."
):

After adding this line, when accessing the page http://localhost:8501/<PAGE_NAME>?analytics=on, where the analytics should be, I instead get this traceback:

ValueError: One or more components is not a string or is empty.
Traceback:

File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 541, in _run_script
    prep_time = timer() - start_time
File "/Users/<USER_NAME>/Work/<COMPANY_NAME>/<PROJECT_NAME>/pages/<PAGE_NAME>.py", line 14, in <module>
    with streamlit_analytics.track(
File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/contextlib.py", line 142, in __exit__
    next(self.gen)
File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/site-packages/streamlit_analytics2/main.py", line 517, in track
    stop_tracking(
File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/site-packages/streamlit_analytics2/main.py", line 463, in stop_tracking
    firestore.save(counts, firestore_key_file, firestore_collection_name)
File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/site-packages/streamlit_analytics2/firestore.py", line 24, in save
    doc.set(counts)  # creates if doesn't exist
File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/site-packages/google/cloud/firestore_v1/document.py", line 166, in set
    batch, kwargs = self._prep_set(document_data, merge, retry, timeout)
File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/site-packages/google/cloud/firestore_v1/base_document.py", line 217, in _prep_set
    batch.set(self, document_data, merge=merge)
File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/site-packages/google/cloud/firestore_v1/base_batch.py", line 111, in set
    write_pbs = _helpers.pbs_for_set_no_merge(
File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/site-packages/google/cloud/firestore_v1/_helpers.py", line 702, in pbs_for_set_no_merge
    extractor = DocumentExtractor(document_data)
File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/site-packages/google/cloud/firestore_v1/_helpers.py", line 504, in __init__
    for field_path, value in iterator:
File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/site-packages/google/cloud/firestore_v1/_helpers.py", line 451, in extract_fields
    for s_path, s_value in extract_fields(value, field_path):
File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/site-packages/google/cloud/firestore_v1/_helpers.py", line 451, in extract_fields
    for s_path, s_value in extract_fields(value, field_path):
File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/site-packages/google/cloud/firestore_v1/_helpers.py", line 446, in extract_fields
    sub_key = FieldPath(key)
File "/opt/anaconda3/envs/<ENV_NAME>/lib/python3.10/site-packages/google/cloud/firestore_v1/field_path.py", line 272, in __init__
    raise ValueError(error)

If I just use with streamlit_analytics.track(), without using Firestore as a backend, the analytics work correctly.

Expected behavior

I expected the Streamlit app to show analytics as usual, and expected to see some data in the Firestore database.

To Reproduce Steps to reproduce the behavior:

  1. Create a simple multipage app
  2. Add the line with streamlit_analytics.track(firestore_key_file="...", firestore_collection_name="..."): in one of the pages
  3. Access localhost:8501 and browse to the page with the line from step 2
  4. See error

Software Vesions Please fill in the relevant field:

Screenshots

image
miguelbalmeida commented 3 months ago

I just tried adding the with streamlit_analytics.track(...) line on the main Python file (instead of one of the page .py files) and I get the same error.

444B commented 3 months ago

Hey @miguelbalmeida Thanks for raising the issue and providing a detailed bug report I'm going to reproduce this now and I'll follow up with findings or questions within the next hour

444B commented 3 months ago

Intial bug analysis:

Next steps:

@miguelbalmeida do you have a public repo with your pages setup that I could clone to get this exact result? It could also speed things up if I didnt have to set up firestore again

In the meantime I will logically analyze the arguments being passed within streamlit-analytics2

miguelbalmeida commented 3 months ago

Thanks for the quick reply, @444B .

In the meantime, I started tinkering. I added breakpoints in the firestore.py file to understand what was happening, hoping to hack something to prevent the error, and.......... it started working. I haven't managed to reproduce the error again. ¯_(ツ)_/¯

Is it possible that this error only appears when the Firestore counts are at zero? And the error disappears after initialization? I doubt this, because in that case others would have spotted that problem already, but it's the best explanation I could come up with.

I'm sorry I can't help further, but I am now struggling to reproduce the error myself...

444B commented 3 months ago

No worries, I am glad to look into it as well I'm taking the opportunity to document how to set up the connection to firestore and adding it to our wiki Please let me know if you are able to reproduce the issue! Ill keep this open for the next few days