Untitled conversation
- User
class Section(BaseModel): title: str content: str = "" sub_sections: list['Section'] = Field(default_factory=list) def find(self, title: str) -> Optional['Section']: """ 自分自身または子孫セクションの中から、title が一致する Section を返す。 見つからなければ None。 """ if self.title == title: return self for sub in self.sub_sections: found = sub.find(title) if found: return found return None def to_markdown(self, level: int = 1, auto_format: bool = True) -> str: """ このセクションとサブセクションを再帰的に Markdown 形式で返す。 level は見出しレベル(# の数)を指定。 """ title = self.title if auto_format: if level <= 2: title = title.title() # H1, H2: 各単語の先頭を大文字 else: words = title.lower().split() if words: words[0] = words[0].capitalize() # H3以下: 先頭の単語だけ大文字 title = ' '.join(words) md = [f"{'#' * level} {title}"] if self.content: md.append(f"{self.content}") for sub in self.sub_sections: md.append("\n" + sub.to_markdown(level + 1, auto_format)) return "\n".join(md)Python 3.12+, Flet 0.28.3. この構造をGUIからD&Dで編集できるようなものを作って。
Assistant
Fletを使用したセクション編集GUIの実装
以下は、SectionクラスをGUIで編集できるFletアプリケーションの実装です。ドラッグアンドドロップでセクションを再配置できる機能を備えています。
import flet as ft from typing import Optional, List from pydantic import BaseModel, Field class Section(BaseModel): title: str content: str = "" sub_sections: List['Section'] = Field(default_factory=list) def find(self, title: str) -> Optional['Section']: if self.title == title: return self for sub in self.sub_sections: found = sub.find(title) if found: return found return None def to_markdown(self, level: int = 1, auto_format: bool = True) -> str: title = self.title if auto_format: if level <= 2: title = title.title() else: words = title.lower().split() if words: words[0] = words[0].capitalize() title = ' '.join(words) md = [f"{'#' * level} {title}"] if self.content: md.append(f"{self.content}") for sub in self.sub_sections: md.append("\n" + sub.to_markdown(level + 1, auto_format)) return "\n".join(md) class SectionEditor(ft.UserControl): def __init__(self, section: Section, on_update=None, on_delete=None): super().__init__() self.section = section self.on_update = on_update self.on_delete = on_delete self.expand = True def build(self): self.title_field = ft.TextField( value=self.section.title, label="Title", expand=True, on_change=self.update_title ) self.content_field = ft.TextField( value=self.section.content, label="Content", multiline=True, min_lines=3, expand=True, on_change=self.update_content ) self.sub_sections_list = ft.Column( expand=True, spacing=10, scroll=ft.ScrollMode.AUTO ) self.update_sub_sections() return ft.Column( controls=[ ft.Row( controls=[ self.title_field, ft.IconButton( icon=ft.icons.DELETE, tooltip="Delete section", on_click=self.delete_section ) ], alignment=ft.MainAxisAlignment.SPACE_BETWEEN ), self.content_field, ft.Row( controls=[ ft.Text("Sub-sections:", weight=ft.FontWeight.BOLD), ft.IconButton( icon=ft.icons.ADD, tooltip="Add sub-section", on_click=self.add_sub_section ) ], alignment=ft.MainAxisAlignment.SPACE_BETWEEN ), self.sub_sections_list ], spacing=10, expand=True ) def update_title(self, e): self.section.title = self.title_field.value if self.on_update: self.on_update() def update_content(self, e): self.section.content = self.content_field.value if self.on_update: self.on_update() def update_sub_sections(self): self.sub_sections_list.controls.clear() for sub_section in self.section.sub_sections: editor = SectionEditor( section=sub_section, on_update=self.on_update, on_delete=self.update_sub_sections ) self.sub_sections_list.controls.append( ft.Draggable( content=ft.Container( content=editor, padding=10, border=ft.border.all(1, ft.colors.GREY_400), border_radius=5 ), group="sections", content_when_dragging=ft.Container( width=200, height=50, bgcolor=ft.colors.BLUE_GREY_200, border_radius=5, content=ft.Text(sub_section.title) ), content_feedback=ft.Container( width=200, height=50, bgcolor=ft.colors.BLUE_GREY_300, border_radius=5, content=ft.Text(sub_section.title) ) ) ) self.sub_sections_list.controls.append( ft.DragTarget( group="sections", content=ft.Container( height=50, bgcolor=ft.colors.GREY_200, border_radius=5, content=ft.Text("Drop here to reorder", size=12), alignment=ft.alignment.center ), on_accept=self.on_accept_drag ) ) self.update() def add_sub_section(self, e): new_section = Section(title="New Section") self.section.sub_sections.append(new_section) self.update_sub_sections() if self.on_update: self.on_update() def delete_section(self, e): if self.on_delete: self.on_delete(self) def on_accept_drag(self, e): # ドラッグされたセクションを取得 dragged_section = e.control.content.controls[0].content.content dragged_index = self.sub_sections_list.controls.index(dragged_section) # ドロップ先のインデックスを取得 drop_index = self.sub_sections_list.controls.index(e.control) # セクションを移動 if dragged_index < drop_index: drop_index -= 1 section = self.section.sub_sections.pop(dragged_index) self.section.sub_sections.insert(drop_index, section) self.update_sub_sections() if self.on_update: self.on_update() def main(page: ft.Page): page.title = "Section Editor" page.theme_mode = ft.ThemeMode.LIGHT page.padding = 20 # 初期データ root_section = Section( title="Main Document", content="This is the main content of the document.", sub_sections=[ Section( title="Introduction", content="Introduction content here.", sub_sections=[ Section(title="Background"), Section(title="Purpose") ] ), Section( title="Methods", content="Methods content here.", sub_sections=[ Section(title="Data Collection"), Section(title="Analysis") ] ) ] ) def update_preview(): preview.value = root_section.to_markdown() page.update() editor = SectionEditor( section=root_section, on_update=update_preview ) preview = ft.Text( value=root_section.to_markdown(), expand=True, selectable=True ) page.add( ft.Row( controls=[ ft.Column( controls=[ ft.Text("Section Editor", size=20, weight=ft.FontWeight.BOLD), editor ], expand=True, scroll=ft.ScrollMode.AUTO ), ft.VerticalDivider(), ft.Column( controls=[ ft.Text("Markdown Preview", size=20, weight=ft.FontWeight.BOLD), preview ], expand=True, scroll=ft.ScrollMode.AUTO ) ], expand=True, spacing=20 ) ) ft.app(target=main)機能説明
-
セクション編集:
- タイトルとコンテンツの編集が可能
- サブセクションの追加/削除が可能
-
ドラッグアンドドロップ:
- セクションをドラッグして順序を変更可能
- ドロップゾーンにドロップして再配置
-
リアルタイムプレビュー:
- Markdown形式でプレビュー表示
- 編集内容が即時反映
-
階層構造:
- ネストされたセクションをサポート
- 任意の深さまでサブセクションを作成可能
使用方法
- 左側のエディタでセクションを編集
- セクションをドラッグして順序を変更
- 右側のプレビューでMarkdown形式を確認
- 必要に応じてサブセクションを追加/削除
このアプリケーションは、ドキュメントの構造を視覚的に編集し、Markdown形式で出力するのに適しています。
-
- User
D&Dがうまく機能しない。微調整して。
Assistant
ドラッグアンドドロップの機能を改善するために、コードを修正しました。主な変更点は以下の通りです:
- ドラッグ操作のロジックを改善
- ドロップターゲットの処理を明確化
- セクションの移動処理を正確に実装
以下が修正後のコードです:
import flet as ft from typing import Optional, List from pydantic import BaseModel, Field class Section(BaseModel): title: str content: str = "" sub_sections: List['Section'] = Field(default_factory=list) def find(self, title: str) -> Optional['Section']: if self.title == title: return self for sub in self.sub_sections: found = sub.find(title) if found: return found return None def to_markdown(self, level: int = 1, auto_format: bool = True) -> str: title = self.title if auto_format: if level <= 2: title = title.title() else: words = title.lower().split() if words: words[0] = words[0].capitalize() title = ' '.join(words) md = [f"{'#' * level} {title}"] if self.content: md.append(f"{self.content}") for sub in self.sub_sections: md.append("\n" + sub.to_markdown(level + 1, auto_format)) return "\n".join(md) class SectionEditor(ft.UserControl): def __init__(self, section: Section, parent_editor=None, on_update=None): super().__init__() self.section = section self.parent_editor = parent_editor self.on_update = on_update self.dragged_section = None self.drag_source = None def build(self): self.title_field = ft.TextField( value=self.section.title, label="Title", expand=True, on_change=self.update_title ) self.content_field = ft.TextField( value=self.section.content, label="Content", multiline=True, min_lines=3, expand=True, on_change=self.update_content ) self.sub_sections_list = ft.Column( expand=True, spacing=10, scroll=ft.ScrollMode.AUTO ) self.update_sub_sections() return ft.Column( controls=[ ft.Row( controls=[ self.title_field, ft.IconButton( icon=ft.icons.DELETE, tooltip="Delete section", on_click=self.delete_section ) ], alignment=ft.MainAxisAlignment.SPACE_BETWEEN ), self.content_field, ft.Row( controls=[ ft.Text("Sub-sections:", weight=ft.FontWeight.BOLD), ft.IconButton( icon=ft.icons.ADD, tooltip="Add sub-section", on_click=self.add_sub_section ) ], alignment=ft.MainAxisAlignment.SPACE_BETWEEN ), self.sub_sections_list ], spacing=10, expand=True ) def update_title(self, e): self.section.title = self.title_field.value if self.on_update: self.on_update() def update_content(self, e): self.section.content = self.content_field.value if self.on_update: self.on_update() def update_sub_sections(self): self.sub_sections_list.controls.clear() # ドロップターゲットを追加(最初の要素として) self.sub_sections_list.controls.append( ft.DragTarget( group="sections", content=ft.Container( height=30, bgcolor=ft.colors.GREY_200, border_radius=5, content=ft.Text("Drop here to add before first", size=12), alignment=ft.alignment.center ), on_will_accept=self.on_will_accept, on_accept=self.on_accept_drag, on_leave=self.on_leave, data=0 # インデックスを表すデータ ) ) # 各サブセクションを追加 for i, sub_section in enumerate(self.section.sub_sections): editor = SectionEditor( section=sub_section, parent_editor=self, on_update=self.on_update ) # ドラッグ可能なコンテナ draggable = ft.Draggable( content=ft.Container( content=editor, padding=10, border=ft.border.all(1, ft.colors.GREY_400), border_radius=5, bgcolor=ft.colors.GREY_100 ), group="sections", content_when_dragging=ft.Container( width=200, height=50, bgcolor=ft.colors.BLUE_GREY_200, border_radius=5, content=ft.Text(sub_section.title) ), content_feedback=ft.Container( width=200, height=50, bgcolor=ft.colors.BLUE_GREY_300, border_radius=5, content=ft.Text(sub_section.title) ), data=i # インデックスを表すデータ ) self.sub_sections_list.controls.append(draggable) # 各セクションの間にドロップターゲットを追加 self.sub_sections_list.controls.append( ft.DragTarget( group="sections", content=ft.Container( height=20, bgcolor=ft.colors.GREY_200, border_radius=5, content=ft.Text(f"Drop here to insert after {sub_section.title}", size=12), alignment=ft.alignment.center ), on_will_accept=self.on_will_accept, on_accept=self.on_accept_drag, on_leave=self.on_leave, data=i + 1 # インデックスを表すデータ ) ) self.update() def on_will_accept(self, e): e.control.content.bgcolor = ft.colors.GREY_400 e.control.update() def on_leave(self, e): e.control.content.bgcolor = ft.colors.GREY_200 e.control.update() def on_accept_drag(self, e): # ドロップターゲットの背景色を元に戻す e.control.content.bgcolor = ft.colors.GREY_200 e.control.update() # ドラッグ元のデータを取得 drag_data = e.src.control.data drop_data = e.control.data if drag_data is None or drop_data is None: return # ドラッグ元のセクションを取得 dragged_section = self.section.sub_sections[drag_data] # ドロップ先の親セクションを取得 if self.parent_editor is None: # ルートセクションの場合 target_sections = self.section.sub_sections else: target_sections = self.parent_editor.section.sub_sections # セクションを移動 if drag_data < drop_data: drop_data -= 1 # 元のリストから削除 del self.section.sub_sections[drag_data] # 新しい位置に挿入 target_sections.insert(drop_data, dragged_section) # UIを更新 if self.parent_editor is not None: self.parent_editor.update_sub_sections() else: self.update_sub_sections() if self.on_update: self.on_update() def add_sub_section(self, e): new_section = Section(title="New Section") self.section.sub_sections.append(new_section) self.update_sub_sections() if self.on_update: self.on_update() def delete_section(self, e): if self.parent_editor is not None: # 親から自分を削除 self.parent_editor.section.sub_sections.remove(self.section) self.parent_editor.update_sub_sections() if self.on_update: self.on_update() else: # ルートセクションは削除不可 self.page.show_snack_bar(ft.SnackBar(ft.Text("Cannot delete root section"))) def main(page: ft.Page): page.title = "Section Editor with D&D" page.theme_mode = ft.ThemeMode.LIGHT page.padding = 20 page.window_width = 1000 page.window_height = 800 # 初期データ root_section = Section( title="Main Document", content="This is the main content of the document.", sub_sections=[ Section( title="Introduction", content="Introduction content here.", sub_sections=[ Section(title="Background"), Section(title="Purpose") ] ), Section( title="Methods", content="Methods content here.", sub_sections=[ Section(title="Data Collection"), Section(title="Analysis") ] ), Section( title="Results", content="Results content here." ) ] ) def update_preview(): preview.value = root_section.to_markdown() page.update() editor = SectionEditor( section=root_section, on_update=update_preview ) preview = ft.Text( value=root_section.to_markdown(), expand=True, selectable=True ) page.add( ft.Row( controls=[ ft.Column( controls=[ ft.Text("Section Editor", size=20, weight=ft.FontWeight.BOLD), editor ], expand=2, scroll=ft.ScrollMode.AUTO ), ft.VerticalDivider(), ft.Column( controls=[ ft.Text("Markdown Preview", size=20, weight=ft.FontWeight.BOLD), preview ], expand=1, scroll=ft.ScrollMode.AUTO ) ], expand=True, spacing=20 ) ) ft.app(target=main)主な改善点
-
ドロップターゲットの配置を改善:
- 各セクションの前後にドロップターゲットを配置
- より直感的なドロップ位置を提供
-
ドラッグ操作のデータ処理を改善:
- ドラッグ元とドロップ先のインデックスを正確に追跡
- セクションの移動処理を正確に実装
-
視覚的フィードバックの強化:
- ドロップターゲットにホバーしたときの色変更
- ドラッグ中の視覚的フィードバック
-
親子関係の処理を改善:
- 親セクションへの参照を追加
- セクションの削除処理を改善
-
UIの改善:
- レイアウトの調整
- 視覚的な区別を明確化
この修正により、セクションをドラッグして再配置する操作がより直感的に行えるようになりました。セクションを別のセクションの上にドロップすると、その位置に挿入されます。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.