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