|
| 1 | +# import logging |
| 2 | + |
| 3 | +import pglet |
| 4 | +from pglet import Button, Dialog, Stack, Text, Textbox |
| 5 | +from pglet.ref import Ref |
| 6 | + |
| 7 | +# logging.basicConfig(level=logging.DEBUG) |
| 8 | + |
| 9 | +pub_sub = {} |
| 10 | + |
| 11 | + |
| 12 | +def broadcast(user, message): |
| 13 | + for session_id, handler in pub_sub.items(): |
| 14 | + handler(user, message) |
| 15 | + |
| 16 | + |
| 17 | +def main(page): |
| 18 | + |
| 19 | + page.padding = 10 |
| 20 | + page.vertical_fill = True |
| 21 | + page.title = "Pglet Chat Example" |
| 22 | + page.bgcolor = "neutralLight" |
| 23 | + page.theme = "light" # "dark" |
| 24 | + |
| 25 | + username_dialog = Ref[Dialog]() |
| 26 | + username = Ref[Textbox]() |
| 27 | + messages = Ref[Stack]() |
| 28 | + message = Ref[Textbox]() |
| 29 | + |
| 30 | + def on_message(user, message): |
| 31 | + if user: |
| 32 | + messages.current.controls.append(Text(f"{user}: {message}")) |
| 33 | + else: |
| 34 | + messages.current.controls.append( |
| 35 | + Text(message, color="#888", size="small", italic=True) |
| 36 | + ) |
| 37 | + page.update() |
| 38 | + |
| 39 | + pub_sub[page.session_id] = on_message |
| 40 | + |
| 41 | + def send_click(e): |
| 42 | + if message.current.value == "": |
| 43 | + return |
| 44 | + broadcast(page.user, message.current.value) |
| 45 | + message.current.value = "" |
| 46 | + page.update() |
| 47 | + |
| 48 | + page.user = page.session_id |
| 49 | + |
| 50 | + def join_click(e): |
| 51 | + if username.current.value == "": |
| 52 | + username.current.error_message = "Name cannot be blank!" |
| 53 | + username.current.update() |
| 54 | + else: |
| 55 | + page.user = username.current.value |
| 56 | + username_dialog.current.open = False |
| 57 | + # user_name.focused = False |
| 58 | + message.current.prefix = f"{page.user}:" |
| 59 | + message.current.focused = True |
| 60 | + page.update() |
| 61 | + broadcast(None, f"{page.user} entered the chat!") |
| 62 | + |
| 63 | + # layout |
| 64 | + page.add( |
| 65 | + Stack( |
| 66 | + height="100%", |
| 67 | + width="100%", |
| 68 | + bgcolor="white", |
| 69 | + padding=10, |
| 70 | + border_radius=5, |
| 71 | + vertical_align="end", |
| 72 | + controls=[Stack(ref=messages, scroll_y=True, auto_scroll=True)], |
| 73 | + ), |
| 74 | + Stack( |
| 75 | + horizontal=True, |
| 76 | + width="100%", |
| 77 | + controls=[ |
| 78 | + Textbox( |
| 79 | + ref=message, |
| 80 | + width="100%", |
| 81 | + multiline=True, |
| 82 | + rows=1, |
| 83 | + auto_adjust_height=True, |
| 84 | + shift_enter=True, |
| 85 | + resizable=False, |
| 86 | + ), |
| 87 | + Button("Send", primary=True, on_click=send_click), |
| 88 | + ], |
| 89 | + on_submit=send_click, |
| 90 | + ), |
| 91 | + Dialog( |
| 92 | + ref=username_dialog, |
| 93 | + open=True, |
| 94 | + blocking=True, |
| 95 | + auto_dismiss=False, |
| 96 | + title="Welcome!", |
| 97 | + controls=[Textbox(ref=username, label="Enter your name", focused=True)], |
| 98 | + footer=[Button(text="Join chat", primary=True, on_click=join_click)], |
| 99 | + ), |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +pglet.app("chat", target=main, web=False) |
0 commit comments