textual icon indicating copy to clipboard operation
textual copied to clipboard

Documentation about adding other event loops

Open qkzk opened this issue 2 years ago • 2 comments

I was trying to view messages from asyncio mqtt updated ASAP with textual and couldn't figure how to add another task in the loop. Then I found this answer which explains how to set it up.

It might seem obvious for experimented async users but I was puzzled.

I think this is a key feature for a basic usage of Textual and it should be added in the documentation.

To be added by @willmcgugan

qkzk avatar Jun 30 '22 17:06 qkzk

@qkzk can you explain a bit here how you got it working?

I'm stuck with the infinite while loop when creating the async task...

class GUI(App):
    async def on_mount(self) -> None:
        task = asyncio.create_task(self.wait_client())

    async def wait_client(self):
        # Wait client
        client, address = client_socket.accept()
        self.logPanel.info("Connected")
        while True:
            msg = client.recv(1024).decode("utf-8")

        	print(msg)


# Socket TCP
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.bind(("localhost", 8800))
client_socket.listen(2)

# Init GUI
app = GUI()
app.run(title="Gateway")

client_socket.close()
drones_socket.close()

maicol07 avatar Aug 01 '22 12:08 maicol07

@maicol07 Sorry I'm on holiday and can't test your code from here. I created the task as you did. What differs is I don't use a socket client.

From what I remember, socket.socket(...).listen is blocking.

I guess you're certain it's not listening indefinitelly (=blocking) on client_socket.listen(2) ?

Since I can't test anything it's a bit hard to be sure but if it's the case, you can find some documentation on asyncio streams, which provides async socket servers.

qkzk avatar Aug 03 '22 13:08 qkzk

https://github.com/Textualize/textual/wiki/Sorry-we-closed-your-issue

willmcgugan avatar Oct 25 '22 09:10 willmcgugan

Did we solve your problem?

Glad we could help!

github-actions[bot] avatar Oct 25 '22 09:10 github-actions[bot]

I'm currently trying to figure this out. Are there any resources I can consult for figuring this out? I know basically nothing about Python AsyncIO. I want to have a constantly running loop that reads a file and outputs the content to a textual window.

Enprogames avatar Mar 12 '23 19:03 Enprogames

I'm currently trying to figure this out. Are there any resources I can consult for figuring this out? I know basically nothing about Python AsyncIO. I want to have a constantly running loop that reads a file and outputs the content to a textual window.

You could check this tutorial

I haven't read this one in particular but they're usually well done.


For your specific problem :

def file_to_string(filepath: str) -> str:
    with open(filepath) as f:
        return f.read()
     
data = file_to_string("text.txt")

will get the content of "text.txt" to data as a string, which you can display as you want in Textualize.

Then it's just about creating an async loop to refresh the content every x seconds.

qkzk avatar Mar 12 '23 20:03 qkzk