Skip to content

Instantly share code, notes, and snippets.

@ihabunek
Created January 13, 2024 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihabunek/a92181388472a1945cb705db42aeb2a7 to your computer and use it in GitHub Desktop.
Save ihabunek/a92181388472a1945cb705db42aeb2a7 to your computer and use it in GitHub Desktop.
"""
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