Skip to content

Instantly share code, notes, and snippets.

@rodja
Created May 8, 2023 03:36
Show Gist options
  • Save rodja/2e891556a1a2c2af4ee542e03003ea1a to your computer and use it in GitHub Desktop.
Save rodja/2e891556a1a2c2af4ee542e03003ea1a to your computer and use it in GitHub Desktop.
Coding a Multi-User Chat App With NiceGUI
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()
@Aviral-sam
Copy link

This works great! Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment