stlite icon indicating copy to clipboard operation
stlite copied to clipboard

Loop and/or timer may not work

Open whitphx opened this issue 3 years ago • 1 comments

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()

whitphx avatar May 24 '22 13:05 whitphx

time.sleep() is noop in Pyodide runtime. https://github.com/pyodide/pyodide/issues/2354

whitphx avatar Aug 02 '22 08:08 whitphx

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()

whitphx avatar Mar 20 '23 07:03 whitphx