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.
# tree_app.py
# Python 3.12+ / Flet 0.28.3
from __future__ import annotations
from typing import List, Optional
import flet as ft
from pydantic import BaseModel, Field
# ========== Domain layer ======================================================
class TreeNode(BaseModel):
"""reasoning / response を持ち、再帰的に子ノードを保持できるノード。"""
role: str
reasoning: str
response: str
children: List["TreeNode"] = Field(default_factory=list)
active_index: int = 0
# ----- public api ----------------------------------------------------------
def add_child(self, child: "TreeNode") -> None:
self.children.append(child)
def get_active_child(self) -> Optional["TreeNode"]:
"""active_index を補正しつつ現在アクティブな子ノードを返す。"""
if not self.children:
self.active_index = 0
return None
if self.active_index < 0 or self.active_index >= len(self.children):
self.active_index = 0
return self.children[self.active_index]
class Tree(BaseModel):
root_nodes: List[TreeNode] = Field(default_factory=list)
active_tree_index: int = 0
# ----- public api ----------------------------------------------------------
def add_root(self, root: TreeNode) -> None:
self.root_nodes.append(root)
def get_active_nodes(self) -> List[TreeNode]:
"""
root → active_child → … と辿った際の「アクティブなノード集合」を返す。
"""
if not self.root_nodes:
self.active_tree_index = 0
return []
if (self.active_tree_index < 0
or self.active_tree_index >= len(self.root_nodes)):
self.active_tree_index = 0
path: List[TreeNode] = []
current: Optional[TreeNode] = self.root_nodes[self.active_tree_index]
while current:
path.append(current)
current = current.get_active_child()
return path
# ========== UI layer ==========================================================
class ChatCard(ft.Card):
"""単一の TreeNode ↔ 入力フォーム ↔ 更新ボタン"""
def __init__(
self,
node: TreeNode,
page: ft.Page,
refresh_callback, # 再描画用
*args,
**kwargs,
):
# フィールド
self.node = node
self.page = page
self.refresh_callback = refresh_callback
# --------------- widgets ----------------------------------------------
self.role_text = ft.Text(value=node.role, style=ft.TextThemeStyle.LABEL_SMALL)
self.reasoning_tf = ft.TextField(
value=node.reasoning,
label="Reasoning",
multiline=True,
expand=True,
)
self.response_tf = ft.TextField(
value=node.response,
label="Response",
multiline=True,
expand=True,
)
update_btn = ft.OutlinedButton(text="更新", on_click=self._on_update)
body = ft.Column(
[
self.role_text,
self.reasoning_tf,
self.response_tf,
update_btn,
],
tight=True,
)
super().__init__(content=body, *args, **kwargs)
# --------------- event handler --------------------------------------------
def _on_update(self, _):
"""TextField の内容を TreeNode に反映"""
self.node.reasoning = self.reasoning_tf.value
self.node.response = self.response_tf.value
# UI フィードバック
self.page.snack_bar = ft.SnackBar(
content=ft.Text("ノードを更新しました!"),
open=True,
)
self.page.update()
# 必要なら親を再描画
self.refresh_callback()
class NodeContainer(ft.Container):
"""
TreeNode に対応するコンテナ。
将来的に 1 ノード = 複数 ChatCard となってもここでまとめれば良い。
今回は 1:1 なので単に ChatCard をラップしているだけ。
"""
def __init__(
self,
node: TreeNode,
page: ft.Page,
refresh_callback,
depth: int,
):
chat = ChatCard(node, page, refresh_callback)
super().__init__(
content=chat,
padding=10,
bgcolor=ft.Colors.with_opacity(
0.05 + depth * 0.05, ft.Colors.BLUE_500
), # 深さに応じて色を少しずつ変える
border_radius=8,
expand=True,
)
# ========== Flet main =========================================================
def main(page: ft.Page):
page.title = "TreeNode Active Path Demo"
page.scroll = ft.ScrollMode.AUTO
# -------------- デモ用ツリー ----------------------------------------------
root1 = TreeNode(role="system", reasoning="root 1", response="resp 1")
child_a = TreeNode(role="user", reasoning="A", response="a")
child_b = TreeNode(role="assistant", reasoning="B", response="b", active_index=0)
child_b1 = TreeNode(role="user", reasoning="B1", response="b1")
child_b.children = [child_b1]
root1.children = [child_a, child_b]
root1.active_index = 1
demo_tree = Tree(root_nodes=[root1], active_tree_index=0)
# -------------- 描画関数 --------------------------------------------------
body_column = ft.Column(spacing=12)
def rebuild_body():
"""現在のアクティブパスに基づき UI を再構築"""
body_column.controls.clear()
for depth, node in enumerate(demo_tree.get_active_nodes()):
body_column.controls.append(
NodeContainer(
node=node,
page=page,
refresh_callback=rebuild_body,
depth=depth,
)
)
page.update()
rebuild_body() # 初回描画
page.add(body_column) # ルートに配置
# -----------------------------------------------------------------------------
# flet run tree_app.py
# -----------------------------------------------------------------------------
if __name__ == "__main__":
ft.app(target=main)
Python 3.12-13, Flet 0.28.3. 各ノードが、自身が有効な子ノードのうち1つである場合、左右のボタンで親ノードの有効な子ノードのインデックスを-/+1できるようにして。
以下のコードで、各ノードに左右のボタンを追加して親ノードのactive_indexを変更できるようにします:
# tree_app.py
# Python 3.12+ / Flet 0.28.3
from __future__ import annotations
from typing import List, Optional, Tuple
import flet as ft
from pydantic import BaseModel, Field
# ========== Domain layer ======================================================
class TreeNode(BaseModel):
"""reasoning / response を持ち、再帰的に子ノードを保持できるノード。"""
role: str
reasoning: str
response: str
children: List["TreeNode"] = Field(default_factory=list)
active_index: int = 0
# ----- public api ----------------------------------------------------------
def add_child(self, child: "TreeNode") -> None:
self.children.append(child)
def get_active_child(self) -> Optional["TreeNode"]:
"""active_index を補正しつつ現在アクティブな子ノードを返す。"""
if not self.children:
self.active_index = 0
return None
if self.active_index < 0 or self.active_index >= len(self.children):
self.active_index = 0
return self.children[self.active_index]
class Tree(BaseModel):
root_nodes: List[TreeNode] = Field(default_factory=list)
active_tree_index: int = 0
# ----- public api ----------------------------------------------------------
def add_root(self, root: TreeNode) -> None:
self.root_nodes.append(root)
def get_active_nodes_with_parents(self) -> List[Tuple[TreeNode, Optional[TreeNode]]]:
"""
root → active_child → … と辿った際の「アクティブなノード集合」を
(node, parent) のタプルのリストとして返す。
"""
if not self.root_nodes:
self.active_tree_index = 0
return []
if (self.active_tree_index < 0
or self.active_tree_index >= len(self.root_nodes)):
self.active_tree_index = 0
path: List[Tuple[TreeNode, Optional[TreeNode]]] = []
current: Optional[TreeNode] = self.root_nodes[self.active_tree_index]
parent: Optional[TreeNode] = None
while current:
path.append((current, parent))
parent = current
current = current.get_active_child()
return path
def get_active_nodes(self) -> List[TreeNode]:
"""
root → active_child → … と辿った際の「アクティブなノード集合」を返す。
"""
return [node for node, _ in self.get_active_nodes_with_parents()]
# ========== UI layer ==========================================================
class ChatCard(ft.Card):
"""単一の TreeNode ↔ 入力フォーム ↔ 更新ボタン"""
def __init__(
self,
node: TreeNode,
parent_node: Optional[TreeNode],
page: ft.Page,
refresh_callback, # 再描画用
*args,
**kwargs,
):
# フィールド
self.node = node
self.parent_node = parent_node
self.page = page
self.refresh_callback = refresh_callback
# --------------- widgets ----------------------------------------------
# ヘッダー行: role + ナビゲーションボタン
header_controls = [ft.Text(value=node.role, style=ft.TextThemeStyle.LABEL_SMALL)]
# 親ノードが存在し、複数の子を持つ場合はナビゲーションボタンを表示
if parent_node and len(parent_node.children) > 1:
current_index = parent_node.active_index
total_children = len(parent_node.children)
left_btn = ft.IconButton(
icon=ft.Icons.CHEVRON_LEFT,
on_click=self._on_navigate_left,
disabled=(current_index == 0),
tooltip="前の兄弟ノードへ"
)
right_btn = ft.IconButton(
icon=ft.Icons.CHEVRON_RIGHT,
on_click=self._on_navigate_right,
disabled=(current_index >= total_children - 1),
tooltip="次の兄弟ノードへ"
)
index_text = ft.Text(
f"{current_index + 1}/{total_children}",
style=ft.TextThemeStyle.BODY_SMALL
)
header_controls.extend([
ft.Container(expand=True), # スペーサー
left_btn,
index_text,
right_btn,
])
header_row = ft.Row(header_controls, alignment=ft.MainAxisAlignment.START)
self.reasoning_tf = ft.TextField(
value=node.reasoning,
label="Reasoning",
multiline=True,
expand=True,
)
self.response_tf = ft.TextField(
value=node.response,
label="Response",
multiline=True,
expand=True,
)
update_btn = ft.OutlinedButton(text="更新", on_click=self._on_update)
body = ft.Column(
[
header_row,
self.reasoning_tf,
self.response_tf,
update_btn,
],
tight=True,
)
super().__init__(content=body, *args, **kwargs)
# --------------- event handler --------------------------------------------
def _on_update(self, _):
"""TextField の内容を TreeNode に反映"""
self.node.reasoning = self.reasoning_tf.value
self.node.response = self.response_tf.value
# UI フィードバック
self.page.snack_bar = ft.SnackBar(
content=ft.Text("ノードを更新しました!"),
open=True,
)
self.page.update()
# 必要なら親を再描画
self.refresh_callback()
def _on_navigate_left(self, _):
"""親ノードのactive_indexを減らす"""
if self.parent_node and self.parent_node.active_index > 0:
self.parent_node.active_index -= 1
self.refresh_callback()
def _on_navigate_right(self, _):
"""親ノードのactive_indexを増やす"""
if self.parent_node and self.parent_node.active_index < len(self.parent_node.children) - 1:
self.parent_node.active_index += 1
self.refresh_callback()
class NodeContainer(ft.Container):
"""
TreeNode に対応するコンテナ。
将来的に 1 ノード = 複数 ChatCard となってもここでまとめれば良い。
今回は 1:1 なので単に ChatCard をラップしているだけ。
"""
def __init__(
self,
node: TreeNode,
parent_node: Optional[TreeNode],
page: ft.Page,
refresh_callback,
depth: int,
):
chat = ChatCard(node, parent_node, page, refresh_callback)
super().__init__(
content=chat,
padding=10,
bgcolor=ft.Colors.with_opacity(
0.05 + depth * 0.05, ft.Colors.BLUE_500
), # 深さに応じて色を少しずつ変える
border_radius=8,
expand=True,
)
# ========== Flet main =========================================================
def main(page: ft.Page):
page.title = "TreeNode Active Path Demo"
page.scroll = ft.ScrollMode.AUTO
# -------------- デモ用ツリー ----------------------------------------------
root1 = TreeNode(role="system", reasoning="root 1", response="resp 1")
child_a = TreeNode(role="user", reasoning="A", response="a")
child_b = TreeNode(role="assistant", reasoning="B", response="b", active_index=0)
child_c = TreeNode(role="user", reasoning="C", response="c")
child_b1 = TreeNode(role="user", reasoning="B1", response="b1")
child_b2 = TreeNode(role="assistant", reasoning="B2", response="b2")
child_b.children = [child_b1, child_b2]
root1.children = [child_a, child_b, child_c]
root1.active_index = 1
demo_tree = Tree(root_nodes=[root1], active_tree_index=0)
# -------------- 描画関数 --------------------------------------------------
body_column = ft.Column(spacing=12)
def rebuild_body():
"""現在のアクティブパスに基づき UI を再構築"""
body_column.controls.clear()
for depth, (node, parent) in enumerate(demo_tree.get_active_nodes_with_parents()):
body_column.controls.append(
NodeContainer(
node=node,
parent_node=parent,
page=page,
refresh_callback=rebuild_body,
depth=depth,
)
)
page.update()
rebuild_body() # 初回描画
page.add(body_column) # ルートに配置
# -----------------------------------------------------------------------------
# flet run tree_app.py
# -----------------------------------------------------------------------------
if __name__ == "__main__":
ft.app(target=main)
主な変更点:
get_active_nodes_with_parents()メソッドの追加: 各ノードとその親ノードのタプルを返すメソッドを追加しました。
ChatCardに親ノード参照を追加: 親ノードを受け取り、ナビゲーションボタンを表示できるようにしました。
ナビゲーションボタンの実装:
デモデータの拡張: テスト用により多くの兄弟ノードを追加しました。
これにより、複数の子ノードを持つ親ノードの場合、現在アクティブな子ノードから左右のボタンで兄弟ノード間を移動できるようになります。