Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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 to use pynecone==0.1.20 if you want
to do something like JavaScript’s setInterval()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading