import streamlit as st from datetime import datetime, timedelta import base64 # Function to get the number of remaining clicks for the user def get_remaining_clicks(): user_id = base64.b64encode(st.session_state.user_name.encode()).decode() click_count = st.session_state.get(user_id, {}).get('click_count', 10) # Get the current click count for the user expiry_str = st.session_state.get(user_id, {}).get('expiry', (datetime.utcnow() + timedelta(hours=24)).strftime('%Y-%m-%d %H:%M:%S')) # Get expiry time from session state expiry_time = datetime.strptime(expiry_str, '%Y-%m-%d %H:%M:%S') return click_count, expiry_time # Function to update the click count for the user def update_click_count(): user_id = base64.b64encode(st.session_state.user_name.encode()).decode() click_count, expiry_time = get_remaining_clicks() if datetime.utcnow() <= expiry_time: # Check if within the 24-hour period click_count -= 1 st.session_state[user_id] = {'click_count': click_count, 'expiry': expiry_time.strftime('%Y-%m-%d %H:%M:%S')} else: # Reset the click count if the 24-hour period has passed st.session_state[user_id] = {'click_count': 9, 'expiry': (datetime.utcnow() + timedelta(hours=24)).strftime('%Y-%m-%d %H:%M:%S')} # Main app def main(): st.title("Click Counter App") user_name = st.text_input("Enter your username:") st.session_state.user_name = user_name # Store the username in session state click_count, expiry_time = get_remaining_clicks() if click_count > 0: if st.button("Click"): update_click_count() st.write(f"Clicked! You have {click_count-1} clicks remaining.") else: st.write("You have used up all your clicks for the next 24 hours.") if __name__ == "__main__": main()