Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-opus-4-20250514· AnthropicHugging Face LMArena Human Preference 140K

Untitled conversation

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-flash-lite-preview-06-17-thinking’s answer.
  1. User
    import asyncio
    import csv
    import logging
    from pathlib import Path
    
    import flet as ft
    
    from configs.path_config import TAGS_CSV_DIR
    
    logger_local = logging.getLogger(__name__)
    
    async def load_suggestions_from_csv(filepath: Path):
        suggestions = []
        try:
            suggestions = await asyncio.to_thread(_load_csv_sync, filepath)
        except Exception as e:
            logger_local.error(f"Failed to load suggestions: {e}")
        return suggestions
    
    def _load_csv_sync(filepath: Path):
        logger_local.warning("start loading")
        suggestions = []
        with open(filepath, newline='', encoding='cp932') as csvfile:
            reader = csv.reader(csvfile)
            for i, row in enumerate(reader):
                logger_local.debug(f"Row {i}: {row}")
                if len(row) < 1 or not row[0].strip():
                    logger_local.warning(f"Skipping empty or invalid row {i}")
                    continue
    
                value = row[0].strip()
                key = value
                suggestion = ft.AutoCompleteSuggestion(key=key, value=value)
                suggestions.append(suggestion)
        logger_local.warning("finished loading")
        return suggestions
    
    async def main(page: ft.Page):
        logger_local.info("Initializing page and loading suggestions...")
    
        # 初期状態では空のサジェストで AutoComplete を作成
        ac = ft.AutoComplete(
            suggestions=[],
        )
    
        # タグを列挙する TextField を用意(read_only にしておくと良いでしょう)
        textfield_tags = ft.TextField(label="選択されたタグ", value="", read_only=True)
    
        # サジェストが選択されたときの処理
        def on_suggestion_selected(e: ft.ControlEvent):
            # e.selection: ft.AutoCompleteSuggestion
            selected_suggestion = e.selection
            tag = selected_suggestion.value  # 文字列を取得
            if tag:
                # 既存の内容にスペース区切りで追加
                if textfield_tags.value:
                    textfield_tags.value += " " + tag
                else:
                    textfield_tags.value = tag
                textfield_tags.update()
    
                # AutoComplete の入力内容をクリアしてリセット
                ac.value = ""
                ac.update()
    
                logger_local.info(f"タグ '{tag}' が追加されました。")
    
        # on_select イベントにハンドラを割り当て
        ac.on_select = on_suggestion_selected
    
        # ページにウィジェットを追加
        page.add(
            ac,
            ft.Text("タグ名またはエイリアスを入力してください。"),
            textfield_tags,
        )
    
        # CSVからサジェストを非同期でロード
        suggestions = await load_suggestions_from_csv(TAGS_CSV_DIR)
        logger_local.info(f"Total suggestions loaded: {len(suggestions)}")
    
        # ロード完了後、AutoComplete のサジェスト候補を更新
        ac.suggestions = suggestions
        ac.update()
    
    ft.app(target=main)
    

    textfield_tagsへの追加後、autocompleteに入力されたデータが消えない。

  2. Claude Assistant

    このコードを見ると、AutoCompleteの値をクリアする処理は正しく書かれているようですが、問題はon_selectイベントの処理タイミングにある可能性があります。

    以下の修正を試してみてください:

    # サジェストが選択されたときの処理
    def on_suggestion_selected(e: ft.ControlEvent):
        # e.selection: ft.AutoCompleteSuggestion
        selected_suggestion = e.selection
        tag = selected_suggestion.value  # 文字列を取得
        if tag:
            # 既存の内容にスペース区切りで追加
            if textfield_tags.value:
                textfield_tags.value += " " + tag
            else:
                textfield_tags.value = tag
            
            # AutoComplete の入力内容をクリアしてリセット
            ac.value = ""
            
            # 両方のコントロールを同時に更新
            page.update()  # ac.update() と textfield_tags.update() の代わりに
            
            logger_local.info(f"タグ '{tag}' が追加されました。")
    

    もしこれでも解決しない場合は、以下の代替案も試してみてください:

    async def on_suggestion_selected(e: ft.ControlEvent):
        # e.selection: ft.AutoCompleteSuggestion
        selected_suggestion = e.selection
        tag = selected_suggestion.value  # 文字列を取得
        if tag:
            # 既存の内容にスペース区切りで追加
            if textfield_tags.value:
                textfield_tags.value += " " + tag
            else:
                textfield_tags.value = tag
            
            # 少し遅延を入れてからクリア
            await asyncio.sleep(0.1)
            ac.value = ""
            
            page.update()
            
            logger_local.info(f"タグ '{tag}' が追加されました。")
    
    # async関数として定義した場合
    ac.on_select = on_suggestion_selected
    

    また、別のアプローチとして、AutoCompleteをTextFieldの一部として使用することも検討できます:

    # AutoCompleteを含むTextFieldを作成
    ac_textfield = ft.TextField(
        label="タグを入力",
        autocomplete=ac,
    )
    
    # イベントハンドラ内で
    ac_textfield.value = ""  # TextFieldの値をクリア
    ac_textfield.update()
    

    これらの方法のいずれかで、選択後に入力フィールドがクリアされるはずです。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.