Created
May 8, 2023 03:36
-
-
Save rodja/2e891556a1a2c2af4ee542e03003ea1a to your computer and use it in GitHub Desktop.
Coding a Multi-User Chat App With NiceGUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from uuid import uuid4 | |
from nicegui import ui | |
messages = [] | |
@ui.refreshable | |
def chat_messages(own_id): | |
for user_id, avatar, text in messages: | |
ui.chat_message(avatar=avatar, text=text, sent=user_id==own_id) | |
@ui.page('/') | |
def index(): | |
def send(): | |
messages.append((user, avatar, text.value)) | |
chat_messages.refresh() | |
text.value = '' | |
user = str(uuid4()) | |
avatar = f'https://robohash.org/{user}?bgset=bg2' | |
with ui.column().classes('w-full items-stretch'): | |
chat_messages(user) | |
with ui.footer().classes('bg-white'): | |
with ui.row().classes('w-full items-center'): | |
with ui.avatar(): | |
ui.image(avatar) | |
text = ui.input(placeholder='message') \ | |
.props('rounded outlined').classes('flex-grow') \ | |
.on('keydown.enter', send) | |
ui.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works great! Thank You