Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] click on st.toggle then pages_view become 1 #122

Open
bitcometz opened this issue Dec 18, 2024 · 2 comments
Open

[BUG] click on st.toggle then pages_view become 1 #122

bitcometz opened this issue Dec 18, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@bitcometz
Copy link

When I first click the Chinese/English button to switch, the total_pageviews in the JSON file resets to 1. Later, when I click the Chinese/English button again, the total_pageviews increments by 1. How can I prevent the total_pageviews from resetting? Below is my code

# -*- coding: utf-8 -*-
import streamlit as st
import gettext
import streamlit_analytics2 as streamlit_analytics
import warnings
import json

warnings.filterwarnings("ignore")

st.set_page_config(
    page_title="Hello",
    page_icon="👋",
)

analytics_file = "./web_stats.json"
with streamlit_analytics.track(save_to_json=analytics_file):

    with open(analytics_file, "r") as f:
        analytics_data = json.load(f)
        visits = analytics_data['total_pageviews']

    if 'language' not in st.session_state:
        st.session_state.language = 'en'

    on = st.toggle("中文")
    if on:
        st.session_state.language = "zh"
    else:
        st.session_state.language = "en"

    lang = gettext.translation('app', localedir='locales', languages=[st.session_state.language])
    _ = lang.gettext

    st.write(_('# Welcome to BioInz! 👋'))
    st.markdown(
        """
        {bioinz_description}.
        {select_demo}.
        ### {our_focus}
        - DNA
        - RNA
        - Protein
        """.format(
            bioinz_description = _("BioInz is an bioinformatics hubs"),
            select_demo        = _("**👈 Select a demo from the sidebar** to see some examples"),
            our_focus          = _("Our focus"), 
        )
    )

    st.sidebar.write("Select ......")
    st.sidebar.write(f"Page views: {visits}")
@bitcometz bitcometz added the bug Something isn't working label Dec 18, 2024
@444B
Copy link
Owner

444B commented Dec 26, 2024

Hi! I think this is unfortunately a feature of SA2 as it will add +1 to a widget count upon each widget use and the way that streamlit operates is that it runs the script on every run and the first run initializes the counter at 1
I think a few months ago we used to minus 1 from the initial counter to account for that but I recall it getting changed for some reason

Ill run your code in a few mins

@444B
Copy link
Owner

444B commented Dec 26, 2024

Hmmm, struggling to get the code to run as I dont have the translation file but there are some fixes I could suggest:

  1. initialize the number of visit to 0
analytics_file = "./web_stats.json"
visits = 0  # Default value
  1. Check if file exists and has content before reading
if os.path.exists(analytics_file) and os.path.getsize(analytics_file) > 0: ~
  1. Then open the file once it exists:
    with open(analytics_file, "r") as f:
        analytics_data = json.load(f)
        visits = analytics_data.get('total_pageviews', 0)

with streamlit_analytics.track(save_to_json=analytics_file):
# Rest of code

Let me know if that works for you. I had to fight with the code a little as I dont have the translation file but I got it to work for me or as I think you expect it to work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants