Untitled conversation
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
# src/elements/image_card.py
from pathlib import Path
import flet as ft
from utils.thumbnails import get_thumbnail, get_thumbnail_async
async def create_image_card(item_path: Path, on_click: callable) -> ft.Card:
return ft.Card(
content=ft.Container(
content=ft.Stack([
ft.Image(
src=str(await get_thumbnail_async(item_path)),
fit=ft.ImageFit.COVER,
border_radius=8,
left=0, right=0, top=0, bottom=0,
),
ft.Container( # タイトル部分に透明背景をつける
height=28,
bgcolor=ft.Colors.with_opacity(0.6, ft.Colors.BLACK),
border_radius=8,
left=0, right=0, bottom=0,
),
ft.Container(
content=ft.Text(
item_path.name,
size=14,
color=ft.Colors.WHITE,
weight=ft.FontWeight.BOLD,
text_align=ft.TextAlign.CENTER,
overflow=ft.TextOverflow.ELLIPSIS,
),
alignment=ft.alignment.bottom_center,
padding=4,
margin=ft.margin.symmetric(horizontal=10), # 横方向両側に10pxの余白
border_radius=8,
)
]),
on_click=on_click,
),
elevation=2,
shape=ft.RoundedRectangleBorder(radius=8),
)
# src/elements/image_card.py
from pathlib import Path
import flet as ft
from utils.thumbnails import get_thumbnail, get_thumbnail_async
async def create_image_card(item_path: Path, on_click: callable) -> ft.Card:
return ft.Card(
content=ft.Container(
content=ft.Stack([
ft.Image(
src=str(await get_thumbnail_async(item_path)),
fit=ft.ImageFit.COVER,
border_radius=8,
left=0, right=0, top=0, bottom=0,
),
ft.Container( # タイトル部分に透明背景をつける
height=28,
bgcolor=ft.Colors.with_opacity(0.6, ft.Colors.BLACK),
border_radius=8,
left=0, right=0, bottom=0,
),
ft.Container(
content=ft.Text(
item_path.name,
size=14,
color=ft.Colors.WHITE,
weight=ft.FontWeight.BOLD,
text_align=ft.TextAlign.CENTER,
overflow=ft.TextOverflow.ELLIPSIS,
),
alignment=ft.alignment.bottom_center,
padding=4,
margin=ft.margin.symmetric(horizontal=10), # 横方向両側に10pxの余白
border_radius=8,
)
]),
on_click=on_click,
),
elevation=2,
shape=ft.RoundedRectangleBorder(radius=8),
)
Python 3.12, Flet 0.28.3. 現状、カードは作成時にサムネイルを割り当てている。 これを、カードはサムネイルなしに作成して、スレッドプールなどからサムネイルを割り当てる方式にしたい。
サムネイルを後から非同期で割り当てる方式に変更するために、以下のようなアプローチを提案します:
# src/elements/image_card.py
from pathlib import Path
import asyncio
from concurrent.futures import ThreadPoolExecutor
import flet as ft
from utils.thumbnails import get_thumbnail, get_thumbnail_async
async def create_image_card(item_path: Path, on_click: callable) -> ft.Card:
# まず画像なしのカードを作成
image = ft.Image(
fit=ft.ImageFit.COVER,
border_radius=8,
left=0, right=0, top=0, bottom=0,
)
card = ft.Card(
content=ft.Container(
content=ft.Stack([
image,
ft.Container(
height=28,
bgcolor=ft.Colors.with_opacity(0.6, ft.Colors.BLACK),
border_radius=8,
left=0, right=0, bottom=0,
),
ft.Container(
content=ft.Text(
item_path.name,
size=14,
color=ft.Colors.WHITE,
weight=ft.FontWeight.BOLD,
text_align=ft.TextAlign.CENTER,
overflow=ft.TextOverflow.ELLIPSIS,
),
alignment=ft.alignment.bottom_center,
padding=4,
margin=ft.margin.symmetric(horizontal=10),
border_radius=8,
)
]),
on_click=on_click,
),
elevation=2,
shape=ft.RoundedRectangleBorder(radius=8),
)
# サムネイル読み込み用の関数
async def load_thumbnail():
try:
thumbnail = await get_thumbnail_async(item_path)
image.src = str(thumbnail)
await image.update_async()
except Exception as e:
print(f"Error loading thumbnail for {item_path}: {e}")
# サムネイル読み込みを非同期で開始
asyncio.create_task(load_thumbnail())
return card
このアプローチの特徴:
image.update_async()で画像を更新します使用する側では、以下のように複数のカードを効率的に作成できます:
async def create_multiple_cards(paths: list[Path], on_click: callable):
cards = []
for path in paths:
card = await create_image_card(path, on_click)
cards.append(card)
return cards
これにより:
必要に応じて、同時に処理するサムネイル数を制限したい場合は、セマフォを使用することもできます:
# グローバルなセマフォを作成
thumbnail_semaphore = asyncio.Semaphore(5) # 同時に5つまで処理
async def load_thumbnail():
async with thumbnail_semaphore:
try:
thumbnail = await get_thumbnail_async(item_path)
image.src = str(thumbnail)
await image.update_async()
except Exception as e:
print(f"Error loading thumbnail for {item_path}: {e}")
セマフォを使用しない場合、全画像の中から処理が終わった順に表示される。セマフォを使ったらおおむね追加した順になる?
はい、その通りです。
セマフォなしの場合:
セマフォありの場合:
例えば20枚の画像があり、セマフォを5に設定した場合:
ただし、完全な順序保証はありません。例えば最初の5枚の中でも、処理の軽い画像が先に表示される可能性はあります。完全な順序保証が必要な場合は、別のアプローチが必要になります。