Skip to content

Commit a01ba98

Browse files
authored
Update chat tutorial (#5879)
* Update chat examples for session and dialog API changes Refactored chat_2.py and chat_3.py to use the updated session and dialog APIs. Replaced deprecated session_id and session.get/set with session.id and session.store.get/set. Updated dialog handling to use show_dialog and pop_dialog methods, and fixed color constant naming. * Update session access to use store attribute Replaces direct session get/set calls with session.store.get/set for user_name in chat example. Ensures compatibility with updated session API. * Refactor Message class and fix color/border usage Refactored the Message class to use a dataclass for cleaner code. Updated color constants to use the correct Flet naming (e.g., BLACK_45 instead of BLACK45) and replaced ft.border.all with ft.Border.all for border creation. * Remove unnecessary page.update() calls in chat examples Eliminated redundant page.update() calls from chat_1.py, chat_2.py, chat_3.py, and main.py, as updates are now handled automatically in event handlers. Updated chat.md documentation to reflect this change and improved code snippets and explanations for clarity.
1 parent c721137 commit a01ba98

File tree

5 files changed

+89
-140
lines changed

5 files changed

+89
-140
lines changed

sdk/python/examples/tutorials/chat/chat_1.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ def main(page: ft.Page):
88
def send_click(e):
99
chat.controls.append(ft.Text(new_message.value))
1010
new_message.value = ""
11-
page.update()
1211

1312
page.add(
1413
chat,

sdk/python/examples/tutorials/chat/chat_2.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ def on_message(message: Message):
2020
page.pubsub.subscribe(on_message)
2121

2222
def send_click(e):
23-
page.pubsub.send_all(Message(user=page.session_id, text=new_message.value))
23+
page.pubsub.send_all(Message(user=page.session.id, text=new_message.value))
2424
new_message.value = ""
25-
page.update()
2625

2726
page.add(chat, ft.Row([new_message, ft.Button("Send", on_click=send_click)]))
2827

sdk/python/examples/tutorials/chat/chat_3.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def on_message(message: Message):
1919
chat.controls.append(ft.Text(f"{message.user}: {message.text}"))
2020
elif message.message_type == "login_message":
2121
chat.controls.append(
22-
ft.Text(message.text, italic=True, color=ft.Colors.BLACK45, size=12)
22+
ft.Text(message.text, italic=True, color=ft.Colors.BLACK_45, size=12)
2323
)
2424
page.update()
2525

@@ -28,39 +28,39 @@ def on_message(message: Message):
2828
def send_click(e):
2929
page.pubsub.send_all(
3030
Message(
31-
user=page.session.get("user_name"),
31+
user=page.session.store.get("user_name"),
3232
text=new_message.value,
3333
message_type="chat_message",
3434
)
3535
)
3636
new_message.value = ""
37-
page.update()
3837

3938
user_name = ft.TextField(label="Enter your name")
4039

4140
def join_click(e):
4241
if not user_name.value:
4342
user_name.error_text = "Name cannot be blank!"
44-
user_name.update()
4543
else:
46-
page.session.set("user_name", user_name.value)
47-
page.dialog.open = False
44+
page.session.store.set("user_name", user_name.value)
45+
# page.dialog.open = False
46+
page.pop_dialog()
4847
page.pubsub.send_all(
4948
Message(
5049
user=user_name.value,
5150
text=f"{user_name.value} has joined the chat.",
5251
message_type="login_message",
5352
)
5453
)
55-
page.update()
5654

57-
page.dialog = ft.AlertDialog(
58-
open=True,
59-
modal=True,
60-
title=ft.Text("Welcome!"),
61-
content=ft.Column([user_name], tight=True),
62-
actions=[ft.Button(content="Join chat", on_click=join_click)],
63-
actions_alignment=ft.MainAxisAlignment.END,
55+
page.show_dialog(
56+
ft.AlertDialog(
57+
open=True,
58+
modal=True,
59+
title=ft.Text("Welcome!"),
60+
content=ft.Column([user_name], tight=True),
61+
actions=[ft.Button(content="Join chat", on_click=join_click)],
62+
actions_alignment=ft.MainAxisAlignment.END,
63+
)
6464
)
6565

6666
page.add(chat, ft.Row([new_message, ft.Button("Send", on_click=send_click)]))

sdk/python/examples/tutorials/chat/main.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1+
from dataclasses import dataclass
2+
13
import flet as ft
24

35

6+
@dataclass
47
class Message: # noqa: B903
5-
def __init__(self, user_name: str, text: str, message_type: str):
6-
self.user_name = user_name
7-
self.text = text
8-
self.message_type = message_type
8+
user_name: str
9+
text: str
10+
message_type: str
911

1012

13+
@ft.control
1114
class ChatMessage(ft.Row):
1215
def __init__(self, message: Message):
1316
super().__init__()
17+
self.message = message
1418
self.vertical_alignment = ft.CrossAxisAlignment.START
1519
self.controls = [
1620
ft.CircleAvatar(
17-
content=ft.Text(self.get_initials(message.user_name)),
21+
content=ft.Text(self.get_initials(self.message.user_name)),
1822
color=ft.Colors.WHITE,
19-
bgcolor=self.get_avatar_color(message.user_name),
23+
bgcolor=self.get_avatar_color(self.message.user_name),
2024
),
2125
ft.Column(
2226
tight=True,
2327
spacing=5,
2428
controls=[
25-
ft.Text(message.user_name, weight=ft.FontWeight.BOLD),
26-
ft.Text(message.text, selectable=True),
29+
ft.Text(self.message.user_name, weight=ft.FontWeight.BOLD),
30+
ft.Text(self.message.text, selectable=True),
2731
],
2832
),
2933
]
@@ -62,7 +66,7 @@ def join_chat_click(e):
6266
join_user_name.error_text = "Name cannot be blank!"
6367
join_user_name.update()
6468
else:
65-
page.session.set("user_name", join_user_name.value)
69+
page.session.store.set("user_name", join_user_name.value)
6670
welcome_dlg.open = False
6771
new_message.prefix = ft.Text(f"{join_user_name.value}: ")
6872
page.pubsub.send_all(
@@ -72,26 +76,24 @@ def join_chat_click(e):
7276
message_type="login_message",
7377
)
7478
)
75-
page.update()
7679

7780
async def send_message_click(e):
7881
if new_message.value != "":
7982
page.pubsub.send_all(
8083
Message(
81-
page.session.get("user_name"),
84+
page.session.store.get("user_name"),
8285
new_message.value,
8386
message_type="chat_message",
8487
)
8588
)
8689
new_message.value = ""
8790
await new_message.focus()
88-
page.update()
8991

9092
def on_message(message: Message):
9193
if message.message_type == "chat_message":
9294
m = ChatMessage(message)
9395
elif message.message_type == "login_message":
94-
m = ft.Text(message.text, italic=True, color=ft.Colors.BLACK45, size=12)
96+
m = ft.Text(message.text, italic=True, color=ft.Colors.BLACK_45, size=12)
9597
chat.controls.append(m)
9698
page.update()
9799

@@ -137,7 +139,7 @@ def on_message(message: Message):
137139
page.add(
138140
ft.Container(
139141
content=chat,
140-
border=ft.border.all(1, ft.Colors.OUTLINE),
142+
border=ft.Border.all(1, ft.Colors.OUTLINE),
141143
border_radius=5,
142144
padding=10,
143145
expand=True,

0 commit comments

Comments
 (0)