added dialog classmethod

This commit is contained in:
2025-09-23 04:24:12 +02:00
parent 01d9dc9fa2
commit baaa2d4349
5 changed files with 185 additions and 2 deletions

84
example_async_dialog.py Normal file
View File

@@ -0,0 +1,84 @@
#!/usr/bin/env python3
"""Example demonstrating the AsyncElement.as_dialog() method"""
from nicegui import ui
from src.niceguiex.async_elements import AsyncElement
class ConfirmDialog(AsyncElement[ui.column]):
"""A confirmation dialog that can be awaited for a result"""
async def build(self, message: str, title: str = "Confirm"):
"""Build the dialog content"""
with self._element:
ui.label(title).classes('text-h6')
ui.label(message)
ui.space()
with ui.row().classes('full-width justify-end'):
ui.button('Cancel', on_click=lambda: self._dialog.submit(False))
ui.button('OK', on_click=lambda: self._dialog.submit(True)).props('color=primary')
class FormDialog(AsyncElement[ui.column]):
"""A form dialog that collects user input"""
async def build(self, title: str = "Enter Information"):
"""Build the form dialog"""
# Create input fields
name_input = ui.input('Name', placeholder='Enter your name')
email_input = ui.input('Email', placeholder='Enter your email')
ui.space()
with ui.row().classes('full-width justify-end'):
ui.button('Cancel', on_click=lambda: self._dialog.submit(None))
ui.button('Submit', on_click=lambda: self._dialog.submit({
'name': name_input.value,
'email': email_input.value
})).props('color=primary')
@ui.page('/')
async def main():
ui.label('AsyncElement Dialog Examples').classes('text-h4')
ui.separator()
async def show_confirm():
result = await ConfirmDialog.as_dialog(
message="Are you sure you want to proceed?",
title="Confirmation Required"
)
ui.notify(f'Confirmation result: {result}', type='positive' if result else 'negative')
async def show_form():
result = await FormDialog.as_dialog(title="User Registration")
if result:
ui.notify(f'Form submitted: {result}', type='positive')
else:
ui.notify('Form cancelled', type='warning')
with ui.row():
ui.button('Show Confirmation Dialog', on_click=show_confirm)
ui.button('Show Form Dialog', on_click=show_form)
# Example with inline async element
async def show_inline_dialog():
class QuickDialog(AsyncElement[ui.column]):
async def build(self):
ui.label('Quick Question').classes('text-h6')
ui.label('Do you like this feature?')
with ui.row():
ui.button('Yes!', on_click=lambda: self._dialog.submit('yes'))
ui.button('No', on_click=lambda: self._dialog.submit('no'))
ui.button('Maybe', on_click=lambda: self._dialog.submit('maybe'))
result = await QuickDialog.as_dialog()
ui.notify(f'Your answer: {result}')
ui.button('Show Quick Dialog', on_click=show_inline_dialog)
if __name__ in {"__main__", "__mp_main__"}:
ui.run(title='Async Dialog Example', port=8085)