File size: 1,747 Bytes
2faeb5f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import flet as ft
class Message:
def __init__(self, user: str, text: str, type: str):
self.user = user
self.text = text
self.type = type
def main(page: ft.Page):
page.title = "دردشة تك"
page.rtl = True
page.theme_mode = ft.ThemeMode.DARK
def on_message(msg: Message):
if msg.type == "chat":
chat_list.controls.append(ft.Text(f"{msg.user}: {msg.text}"))
else:
chat_list.controls.append(ft.Text(msg.text, italic=True, color="grey"))
page.update()
page.pubsub.subscribe(on_message)
chat_list = ft.Column(expand=True, scroll=ft.ScrollMode.ALWAYS)
msg_input = ft.TextField(hint_text="اكتب هنا...", expand=True)
def send_click(e):
if msg_input.value:
page.pubsub.send_all(Message(page.session.get("username"), msg_input.value, "chat"))
msg_input.value = ""
page.update()
name_input = ft.TextField(label="اسمك")
def join_click(e):
if name_input.value:
page.session.set("username", name_input.value)
page.dialog.open = False
page.pubsub.send_all(Message(name_input.value, f"انضم {name_input.value}", "info"))
page.add(ft.Container(content=chat_list, expand=True), ft.Row([msg_input, ft.IconButton(ft.icons.SEND, on_click=send_click)]))
page.update()
page.dialog = ft.AlertDialog(title=ft.Text("دردشة تك"), content=name_input, actions=[ft.ElevatedButton("دخول", on_click=join_click)])
page.dialog.open = True
page.update()
# انتبه لهذه الإعدادات ليعمل على Hugging Face
ft.app(target=main, view=ft.AppView.WEB_BROWSER, host="0.0.0.0", port=7860)
|