Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop and/or timer may not work #34

Closed
whitphx opened this issue May 24, 2022 · 2 comments
Closed

Loop and/or timer may not work #34

whitphx opened this issue May 24, 2022 · 2 comments

Comments

@whitphx
Copy link
Owner

whitphx commented May 24, 2022

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
Copy link
Owner Author

whitphx commented Aug 2, 2022

time.sleep() is noop in Pyodide runtime.
pyodide/pyodide#2354

@whitphx
Copy link
Owner Author

whitphx commented Mar 20, 2023

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 whitphx closed this as completed Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant