Replies: 1 comment
|
The issue is that Use Approach 1: Use import reflex as rx
import asyncio
class State(rx.State):
lines: list[str] = []
async def add_line(self, newline: dict):
# Immediately add the typed line and push to frontend
self.lines.append(newline["lineinput"])
yield
# Wait 10 seconds
await asyncio.sleep(10)
# Add the delayed line and push again
self.lines.append("Added a line!")
yieldThe key insight: every Approach 2: Use If you need the handler to return immediately and have work happen in the background: class State(rx.State):
lines: list[str] = []
def add_line(self, newline: dict):
self.lines.append(newline["lineinput"])
return State.delayed_add
@rx.background
async def delayed_add(self):
await asyncio.sleep(10)
async with self:
self.lines.append("Added a line!")With The core problem with your original code: |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I have a reflex- based application with an auto_scroll component that updates whenever a line of text is entered into an input component. The code for this is below:
What I would like this application to do is to immediately add a line to the auto_scroll component when the line is entered into the input and the "Enter" button is clicked on. Then, after 10 seconds, it should add "Added a line" into the auto_scroll component.
Unfortunately, the current code doesn't work that way. The auto_scroll component does not update until the add_line() method returns. Consequently, I cannot seem to get it to update after the add_another_line() method returns.
I have looked at several applications that update a display when a form is submitted, perform network operations, then update the display with the results of those operations. I know that it is possible to make my application do what I want it to do, I just don't knopw how to do it. Unfortunately, I don't have access to the source code for those applications, so I have no way to see how it is done.
Can someone tell me how to make my application update my auto_scroll component immediately when I enter a new line, then update it again when the "Added a line" text is added 10 seconds later?
Thanks in advance for any suggestions.
All reactions