Download aphp.py from Pezh/Login: direct link, hf CLI and curl.
- Browser
- Download file 1.78 kB
-
https://huggingface.co/spaces/Pezh/Login/resolve/main/aphp.py
- Command line
-
hf download hf://spaces/Pezh/Login/aphp.py
-
curl -L -o aphp.py https://huggingface.co/spaces/Pezh/Login/resolve/main/aphp.py
1.78 kB
| import streamlit as st | |
| from streamlit.components.v1 import html | |
| from datetime import datetime, timedelta | |
| # Custom function to display HTML content with Streamlit's components.v1.html() | |
| def local_storage_button(): | |
| button_html = """ | |
| <button onclick="incrementCounter()">Click it</button> | |
| <script> | |
| function getWithExpiry(key) { | |
| const itemStr = localStorage.getItem(key) | |
| if (!itemStr) { | |
| return null | |
| } | |
| const item = JSON.parse(itemStr) | |
| const now = new Date() | |
| if (now.getTime() > item.expiry) { | |
| localStorage.removeItem(key) | |
| return null | |
| } | |
| return item.value | |
| } | |
| function incrementCounter() { | |
| let count = getWithExpiry('click_count') 0; | |
| const expiry = getWithExpiry('expiry_time') new Date().getTime() + (24 * 60 * 60 * 1000); | |
| if (count < 10 && new Date().getTime() < expiry) { | |
| count++; | |
| localStorage.setItem('click_count', JSON.stringify({value: count, expiry})); | |
| localStorage.setItem('expiry_time', JSON.stringify({value: expiry, expiry})); | |
| window.location.reload(); // Reload the page to refresh the count display | |
| } | |
| } | |
| // Set initial count on page load/reload | |
| window.onload = (event) => { | |
| let count = getWithExpiry('click_count') 0; | |
| document.getElementById('counter').innerText = `Button has been clicked ${count} times.`; | |
| }; | |
| </script> | |
| """ | |
| html(button_html) | |
| # Use the custom function to display the button | |
| local_storage_button() | |
| # Display the counter | |
| st.markdown("<p id='counter'></p>", unsafe_allow_html=True) |