Does pynecone have some function like JavaScript's setInterval()?

Question:

I need a function like
javascript’s setInterval()?

Do you know where it is?

I read the pynecone’s official document.
https://pynecone.app/docs/getting-started/introduction
I still cannot find any function like setInterval() in JS.

I also visit pynecone’s gallery here.
https://pynecone.app/docs/gallery
There is a clock example.
https://clock.pynecone.app

I’m still trying to find out the solution.
I would appreciate it if you would help me.

Asked By: Gap Chen

||

Answers:

The code can let you do something like setInterval()

1. Setup pynecone 0.1.20 by the following command line.

$ pip install pynecone==0.1.20

2. The sample code here and this is the demo video

import pynecone as pc
import asyncio
class State(pc.State):
    counter: int = 0
    is_timer_start: bool = False
    def timer_start(self):
        self.is_timer_start = True
        if self.is_timer_start:
            return self.tick
    def timer_stop(self):
        self.is_timer_start = False
        if self.is_timer_start:
            return self.tick 
    async def tick(self):    
        if self.is_timer_start:
            await asyncio.sleep(1) #time interval is 1 second here because of sleep(1)
            self.counter += 1 # update counter
            # Do what you want to do here for each second
            return self.tick
def index():
    return pc.center(
        pc.vstack(
            pc.heading("Timer", font_size="2em"),
            pc.hstack(
                pc.button("Start", on_click=State.timer_start, color_scheme="blue", border_radius="1em"),
                pc.button("Stop", on_click=State.timer_stop, color_scheme="blue", border_radius="1em"),
            ),
            pc.link(
                State.counter,
                href="#",
                border="0.1em solid",
                padding="0.5em",
                border_radius="0.5em",
                _hover={
                    "color": "rgb(107,99,246)",
                },
            ),
            spacing="1.5em",
            font_size="2em",
        ),
        padding_top="10%",
    )

app = pc.App(state=State)
app.add_page(index, title="Timer")
app.compile()

pynecone==0.1.20 is okay for this code.
pynecone==0.1.21 is not okay (there is some issue).
(Today is 2023/04/04 when I post)
We can wait for the core team to solve it.
So I suggest using pynecone==0.1.20 if you want
to do something like JavaScript’s setInterval()

Updating

The new commit code, Apr 9, 2023, has solved this issue.
The next released code pynecone==0.1.22 will work well.

Answered By: Milo Chen
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.