MCPcopy Create free account
hub / github.com/pywebio/PyWebIO / main

Function main

demos/chat_room.py:36–72  ·  view source on GitHub ↗

PyWebIO chat room You can chat with everyone currently online.

()

Source from the content-addressed store, hash-verified

34
35
36async def main():
37 """PyWebIO chat room
38
39 You can chat with everyone currently online.
40 """
41 global chat_msgs
42
43 put_markdown(t("## PyWebIO chat room\nWelcome to the chat room, you can chat with all the people currently online. You can open this page in multiple tabs of your browser to simulate a multi-user environment. This application uses less than 90 lines of code, the source code is [here](https://github.com/wang0618/PyWebIO/blob/dev/demos/chat_room.py)", "## PyWebIO聊天室\n欢迎来到聊天室,你可以和当前所有在线的人聊天。你可以在浏览器的多个标签页中打开本页面来测试聊天效果。本应用使用不到90行代码实现,源代码[链接](https://github.com/wang0618/PyWebIO/blob/dev/demos/chat_room.py)"))
44
45 put_scrollable(put_scope('msg-box'), height=300, keep_bottom=True)
46 nickname = await input(t("Your nickname", "请输入你的昵称"), required=True, validate=lambda n: t('This name is already been used', '昵称已被使用') if n in online_users or n == '📢' else None)
47
48 online_users.add(nickname)
49 chat_msgs.append(('📢', '`%s` joins the room. %s users currently online' % (nickname, len(online_users))))
50 put_markdown('`📢`: `%s` join the room. %s users currently online' % (nickname, len(online_users)), sanitize=True, scope='msg-box')
51
52 @defer_call
53 def on_close():
54 online_users.remove(nickname)
55 chat_msgs.append(('📢', '`%s` leaves the room. %s users currently online' % (nickname, len(online_users))))
56
57 refresh_task = run_async(refresh_msg(nickname))
58
59 while True:
60 data = await input_group(t('Send message', '发送消息'), [
61 input(name='msg', help_text=t('Message content supports inline Markdown syntax', '消息内容支持行内Markdown语法')),
62 actions(name='cmd', buttons=[t('Send', '发送'), t('Multiline Input', '多行输入'), {'label': t('Exit', '退出'), 'type': 'cancel'}])
63 ], validate=lambda d: ('msg', 'Message content cannot be empty') if d['cmd'] == t('Send', '发送') and not d['msg'] else None)
64 if data is None:
65 break
66 if data['cmd'] == t('Multiline Input', '多行输入'):
67 data['msg'] = '\n' + await textarea('Message content', help_text=t('Message content supports Markdown syntax', '消息内容支持Markdown语法'))
68 put_markdown('`%s`: %s' % (nickname, data['msg']), sanitize=True, scope='msg-box')
69 chat_msgs.append((nickname, data['msg']))
70
71 refresh_task.close()
72 toast("You have left the chat room")
73
74
75if __name__ == '__main__':

Callers

nothing calls this directly

Calls 13

run_asyncFunction · 0.90
put_markdownFunction · 0.85
put_scrollableFunction · 0.85
put_scopeFunction · 0.85
inputFunction · 0.85
refresh_msgFunction · 0.85
input_groupFunction · 0.85
actionsFunction · 0.85
textareaFunction · 0.85
toastFunction · 0.85
appendMethod · 0.80
tFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…