Skip to content

Commit

Permalink
explanations for the script executor example
Browse files Browse the repository at this point in the history
  • Loading branch information
rodja committed Mar 7, 2023
1 parent 64d9e7d commit fbc878a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions examples/script_executor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@


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 = ''
process = await asyncio.create_subprocess_exec(
*shlex.split(command),
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT,
cwd=os.path.dirname(os.path.abspath(__file__)))
content = ''
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
content += new.decode()
result.content = f'```\n{content}\n```'
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()
Expand Down

0 comments on commit fbc878a

Please sign in to comment.