Open mm4nn opened 4 months ago
You can call self._app.invalidate()
to schedule a repaint.
If you'd like to use asyncio, I also suggest to make the main()
function async, and have async from the start. Use asyncio.run(main())
to start the application, and rewrite the run()
like this:
async def run(self):
await self._app.run_async()
Instead of asyncio.create_task
, use either a TaskGroup
(native asyncio task groups or from the anyio
library), or use self._app.create_background_task(...)
.
I'd also like to point out that it's worth checking out the Textual library which might or might not be better for these things, depending on your needs.
I am currently struggling with asyncio concepts and prompt toolkit. I want to be able to update the UI when I have long running tasks in the background. In a regular UI (non-terminal application) the task would be spun up in a separate thread and when there is data available it could send... say an event or message (depending on the framework) with the data to the UI thread for it to update itself.
Again, I am struggling to try to get anything similar to that pattern with asyncio and prompt toolkit. I included a sample application that has a "ListView." Pressing "i" would run a long-running task that populates the list view. If I remove all of the asyncio stuff and run everything on the main thread, then everything works fine, but when I run it as-is, it only prints the first item. It does nothing after the first
asyncio.sleep()
call.Also, I realize that the UI updates are happening in the "background" here (or at least I think they are not--I don't understand asyncio enough yet to know if it's sharing the main event loop?). Do you have any suggestion on how I can send events or messages to the main thread so all the UI work happens on one thread? (sample code?) Or is that not required for prompt-toolkit?
Any and all help and suggestions would be appreciated.
Just fyi. This is obviously sample code but in my real application the list view would receive updates from http calls.