-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (48 loc) · 2.18 KB
/
main.py
File metadata and controls
71 lines (48 loc) · 2.18 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from pywebio import start_server
from pywebio.input import *
from pywebio.output import *
from pywebio.session import run_async, run_js
import asyncio
chat_msgs = []
online_users = set()
MAX_MESSAGES_COUNT = 100
async def main():
global chat_msgs
put_markdown("## 🧊 Welcome to our chat!\nThis project was made with Python within 100 lines of code!")
msg_box = output()
put_scrollable(msg_box, height=300, keep_bottom=True)
nickname = await input("Enter chat", required=True, placeholder="Nickname", validate=lambda n: "This nickname is already in use!" if n in online_users or n == '📢' else None)
online_users.add(nickname)
chat_msgs.append(('📢', f"`{nickname}` joined chat!"))
msg_box.append(put_markdown(f"`{nickname}` joined chat!"))
refresh_task = run_async(refresh_msg(nickname, msg_box))
while True:
data = await input_group("💭 New message", [
input(placeholder="Text of message", name="msg"),
actions(name="cmd", buttons=["Send", {'label':"Quit chat", 'type':'cancel'}])
], validate=lambda m: ('msg', "Enter the text of message!") if m["cmd"] == "Send" and not m["msg"] else None)
if data is None:
break
msg_box.append(put_markdown(f"`{nickname}`: {data['msg']}"))
chat_msgs.append((nickname, data['msg']))
# exit chat
refresh_task.close()
online_users.remove(nickname)
toast("Вы вышли из чата!")
msg_box.append(put_markdown(f"📢 `{nickname}` quited the chat !"))
chat_msgs.append(('📢', f"`{nickname}` quited the chat!"))
put_buttons(["Reenter"], onclick=lambda btn: run_js('window.location.reload()'))
async def refresh_msg(nickname, msg_box):
global chat_msgs
last_idx = len(chat_msgs)
while True:
await asyncio.sleep(1)
for m in chat_msgs[last_idx:]:
if m[0] != nickname:
msg_box.append(put_markdown(f"`{m[0]}`: {m[1]}"))
# remove expired
if len(chat_msgs) > MAX_MESSAGES_COUNT:
chat_msgs = chat_msgs[len(chat_msgs) // 2:]
last_idx = len(chat_msgs)
if __name__ == "__main__":
start_server(main, debug=True, port=8080, cdn=False)