main.py 924 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env python3
  2. import asyncio
  3. import os.path
  4. import shlex
  5. from nicegui import background_tasks, ui
  6. async def run_command(command: str) -> None:
  7. dialog.open()
  8. result.content = ''
  9. process = await asyncio.create_subprocess_exec(
  10. *shlex.split(command),
  11. stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT,
  12. cwd=os.path.dirname(os.path.abspath(__file__)))
  13. content = ''
  14. while True:
  15. new = await process.stdout.read(4096)
  16. if not new:
  17. break
  18. content += new.decode()
  19. result.content = f'```\n{content}\n```'
  20. with ui.dialog() as dialog, ui.card():
  21. result = ui.markdown()
  22. commands = ['python3 hello.py', 'python3 hello.py NiceGUI', 'python3 slow.py']
  23. with ui.row():
  24. for command in commands:
  25. ui.button(command, on_click=lambda _, c=command: background_tasks.create(run_command(c))).props('no-caps')
  26. ui.run()