ublue-forge/anvil/nicegui/_tests/test.py
Stephan Lüscher 85b0b3fdc5
wip - nicegui
2024-05-06 15:16:52 +00:00

88 lines
3.2 KiB
Python

# from nicegui import ui
# @ui.page('/')
# def page_layout():
# ui.label('CONTENT')
# [ui.label(f'Line {i}') for i in range(100)]
# with ui.header(elevated=True).style('background-color: #3874c8').classes('items-center justify-between'):
# ui.button(on_click=lambda: left_drawer.toggle(), icon='menu').props('flat color=white')
# ui.label('HEADER')
# with ui.left_drawer(fixed=False).style('background-color: #ebf1fa').props('bordered') as left_drawer:
# ui.label('LEFT DRAWER')
# with ui.footer().style('background-color: #3874c8'):
# ui.label('FOOTER')
# # ui.link('show page with fancy layout', page_layout)
# ui.run()
# from nicegui import ui
# with ui.header().classes(replace='row items-center') as header:
# ui.button(on_click=lambda: left_drawer.toggle(), icon='menu').props('flat color=white')
# with ui.tabs() as tabs:
# ui.tab('Home')
# ui.tab('Ansible')
# ui.tab('Registry')
# with ui.footer(value=False) as footer:
# ui.label('Footer')
# with ui.left_drawer().classes('bg-blue-100') as left_drawer:
# ui.label('Side menu')
# with ui.page_sticky(position='bottom-right', x_offset=20, y_offset=20):
# ui.button(on_click=footer.toggle, icon='contact_support').props('fab')
# with ui.tab_panels(tabs, value='A').classes('w-full'):
# with ui.tab_panel('A'):
# ui.label('Content of A')
# with ui.tab_panel('B'):
# ui.label('Content of B')
# with ui.tab_panel('C'):
# ui.label('Content of C')
# ui.run()
#!/usr/bin/env python3
import asyncio
import os.path
import platform
import shlex
import sys
from nicegui import ui
async def run_command(command: str) -> None:
"""Run a command in the background and display the output in the pre-created dialog."""
dialog.open()
result.content = ''
command = command.replace('python3', sys.executable) # NOTE replace with machine-independent Python path (#1240)
process = await asyncio.create_subprocess_exec(
*shlex.split(command, posix='win' not in sys.platform.lower()),
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT,
cwd=os.path.dirname(os.path.abspath(__file__))
)
# NOTE we need to read the output in chunks, otherwise the process will block
output = ''
while True:
new = await process.stdout.read(4096)
if not new:
break
output += new.decode()
# NOTE the content of the markdown element is replaced every time we have new output
result.content = f'```\n{output}\n```'
with ui.dialog() as dialog, ui.card():
result = ui.markdown()
ui.button('python3 runner.py', on_click=lambda: run_command('python3 runner.py')).props('no-caps')
ui.button('python3 slow.py', on_click=lambda: run_command('python3 slow.py')).props('no-caps')
with ui.row().classes('items-center'):
ui.button('python3 hello.py "<message>"', on_click=lambda: run_command(f'python3 hello.py "{message.value}"')) \
.props('no-caps')
message = ui.input('message', value='NiceGUI')
# NOTE: On Windows reload must be disabled to make asyncio.create_subprocess_exec work (see https://github.com/zauberzeug/nicegui/issues/486)
ui.run(reload=platform.system() != 'Windows')