stlite
stlite copied to clipboard
Loop and/or timer may not work
Reported at https://discuss.streamlit.io/t/new-library-stlite-a-port-of-streamlit-to-wasm-powered-by-pyodide/25556/3
The source code copied:
import streamlit as st
import time
st.set_page_config(
page_title='Pomodoro',
layout='centered',
page_icon='🍅'
)
def count_down(ts):
with st.empty():
while True:
mins, secs = divmod(ts, 60)
time_now = '{:02d}:{:02d}'.format(mins, secs)
st.header(f"{time_now}")
time.sleep(1)
ts -= 1
if ts < 0:
break
st.write("Time Up!")
st.balloons()
def main():
st.title("Pomodoro")
time_minutes = st.number_input('Enter the time in minutes ', min_value=0.1, value=25.0)
time_in_seconds = time_minutes * 60
if st.button("START"):
count_down(int(time_in_seconds))
if __name__ == '__main__':
main()
time.sleep() is noop in Pyodide runtime.
https://github.com/pyodide/pyodide/issues/2354
With a support of top-level await since (#514 ), we now can use asyncio.sleep() instead of time.sleep(), so the following code works.
import asyncio
import time
import streamlit as st
st.set_page_config(
page_title='Pomodoro',
layout='centered',
page_icon='🍅'
)
async def count_down(ts):
with st.empty():
while True:
mins, secs = divmod(ts, 60)
time_now = '{:02d}:{:02d}'.format(mins, secs)
st.header(f"{time_now}")
await asyncio.sleep(1)
ts -= 1
if ts < 0:
break
st.write("Time Up!")
st.balloons()
async def main():
st.title("Pomodoro")
time_minutes = st.number_input('Enter the time in minutes ', min_value=0.1, value=25.0)
time_in_seconds = time_minutes * 60
if st.button("START"):
await count_down(int(time_in_seconds))
if __name__ == '__main__':
await main()