reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Are there any events and event triggers (or handlers) that occur when the page is finished loading?

Open heumsi opened this issue 2 years ago β€’ 7 comments

Describe the bug A clear and concise description of what the bug is.

I want to additionally handle other events when the page has finished loading. For example, calling an external API to load data to be used in page (or state).

There doesn't seem to be any events and event triggers related to this in the current documentation.

Any work related to this? Or is there any documentation or code that I should refer to in this regard

To Reproduce Steps to reproduce the behavior:

  • Code/Link to Repo:

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

** Specifics (please complete the following information):**

  • Python Version:
  • Pynecone Version:
  • OS:
  • Browser (Optional):

Additional context Add any other context about the problem here.

heumsi avatar Jan 24 '23 05:01 heumsi

Not yet we do have an issue for this though, we have been prioritizing bug fixes first. https://github.com/pynecone-io/pynecone/issues/142

Alek99 avatar Jan 24 '23 05:01 Alek99

I will add more comments.

Sometimes, variables in State require lazy initialization. (Through method calls, etc.) However, currently, it seems that there is only a way to initialize it as a class variable.

I wonder if it is possible to work on lazy initialization?

heumsi avatar Jan 24 '23 05:01 heumsi

Yes you could initially set the variable in state to some value and have a pc.cond and show a skeleton to start once the variable has been updated show something else as the second item of the cond

Alek99 avatar Jan 24 '23 05:01 Alek99

What to do in this case?

class QueryState(BaseState):
    nodes: List[str] = api.get_nodes()
    is_loaded: bool = False

    def refresh_nodes(self) -> None:
        self.nodes = []
        self.is_loaded = False
        self.nodes = api.get_nodes(self.labels)
        self.is_loaded = True


pc.box(
    pc.foreach(
        QueryState.nodes,
        lambda node: pc.box(
            pc.skeleton(
                pc.text(node),
                is_loaded=QueryState.is_loaded,
            ),
        ),
    ),
)

In the code above, is_loaded is initially False, so only the skeleton is visible.

image

heumsi avatar Jan 24 '23 05:01 heumsi

Should I use computed Vars?

(However, in this case, it is a little burdensome because there is an external API call inside the computed Var.)

heumsi avatar Jan 24 '23 05:01 heumsi

Right I see, yeah the on_load would be ideal here because computed vars will do more requests than necessary. We can try and prioritize this issue to be done first after the bug fixes

Alek99 avatar Jan 24 '23 07:01 Alek99

Okay. I will wait. Thanks!

heumsi avatar Jan 24 '23 08:01 heumsi

@Lendemor Added this yesterday will be in the next release, se here https://github.com/pynecone-io/pynecone/commit/52aaf365c906e2522b079219b8af182b97efb6a3

Alek99 avatar Jan 25 '23 22:01 Alek99