Download app.py from Pezh/Login: direct link, hf CLI and curl.
- Browser
- Download file 1.73 kB
-
https://huggingface.co/spaces/Pezh/Login/resolve/main/app.py
- Command line
-
hf download hf://spaces/Pezh/Login/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Pezh/Login/resolve/main/app.py
1.73 kB
| import streamlit as st | |
| from datetime import datetime, timedelta | |
| # Custom function to display HTML content with Streamlit's components.v1.html() | |
| def local_storage_button(): | |
| button_html = f""" | |
| <button onclick="incrementCounter()">Click it</button> | |
| <p id='counter'></p> | |
| <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; | |
| let 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> | |
| """ | |
| st.components.v1.html(button_html, height=100) | |
| # Use the custom function to display the button | |
| local_storage_button() |