123456789101112131415161718192021222324252627282930313233343536373839404142 |
- 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)
- 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__))
- )
-
- output = ''
- while True:
- new = await process.stdout.read(4096)
- if not new:
- break
- output += new.decode()
-
- result.content = f'```\n{output}\n```'
- with ui.dialog() as dialog, ui.card():
- result = ui.markdown()
- ui.button('python3 hello.py', on_click=lambda: run_command('python3 hello.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')
- ui.run(reload=platform.system() != 'Windows')
|