Created
January 13, 2024 09:16
-
-
Save ihabunek/a92181388472a1945cb705db42aeb2a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Attempt to display an image in Textual using the kitty graphics protocol. | |
This currently does not work, even though render_kitty() itself works and | |
printing the resulting Text will display it in console. | |
""" | |
import asyncio | |
from asyncio.subprocess import PIPE | |
from rich.text import Text | |
from textual.app import App | |
from textual.widgets import Static | |
IMAGE_URL = "https://raw.githubusercontent.com/Textualize/textual/main/imgs/textual.png" | |
async def render_kitty(url: str) -> Text: | |
cmd = ["kitty", "icat", "--unicode-placeholder", url] | |
proc = await asyncio.create_subprocess_exec(*cmd, stdout=PIPE, stderr=PIPE) | |
stdout, stderr = await proc.communicate() | |
if proc.returncode != 0: | |
raise ValueError(stderr.decode()) | |
return Text(stdout.decode()) | |
class ImageApp(App): | |
DEFAULT_CSS = "Static { border: solid white; }" | |
def compose(self): | |
yield Static("Loading...") | |
def on_mount(self): | |
self.run_worker(self.load()) | |
async def load(self): | |
image = await render_kitty(IMAGE_URL) | |
self.query_one(Static).update(image) | |
if __name__ == "__main__": | |
# Attempt to show the image in a textual app (does not work) | |
ImageApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment