Have a look at this demo notebook:
https://github.com/takluyver/rt2-workshop-jupyter/blob/e7fde6565e28adf31a0f9003094db70c3766bd6d/Subprocess%20output.ipynb

It doesn't put output into a widget, but it's using print(), so it should
be easy to combine with a widget.

If you're happy to kill the process by interrupting the kernel from
Jupyter, then it's just a matter of catching KeyboardInterrupt in the
Python code.

Thomas

On 7 March 2018 at 15:03, Randy Heiland <[email protected]> wrote:

> I'd like to be able to display output from a program in an Output widget.
> (And I'd like to be able to kill the program from a widget too). Can
> someone offer advice?
>
> import ipywidgets as widgets
> import subprocess, os
>
> run_output = widgets.Output(layout=widgets.Layout(width='500px',
> height='100px', border='solid'))
> run_output
>
> def run_cb(b):
>     #global run_output
>     print('run sim...')
>     with run_output:
>         print('trying subprocess - does hello output show up?')
>         args = ['hello']
>         run_proc = subprocess.Popen(args)
>         #os.system('hello')
>
> run_button = widgets.Button(
>     description='Run',
>     disabled=False,
>     button_style='success', # 'success', 'info', 'warning', 'danger' or ''
>     tooltip='Run simulation',
> )
> run_button.on_click(run_cb)
> run_button
> widgets.VBox([run_button, run_output])
>
> where hello.c is simply:
>
> #include <stdio.h>
> #include <unistd.h>
> int main(int argc, char **argv) {
>   int idx;
>   for (idx=0; idx<4; idx++) { printf("argc=%d, %d) hello,
> world...\n",argc,idx); fflush(stdout); sleep(1); }
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/jupyter/ffd113b2-d2b9-46e1-99fc-ed17e05423cf%40googlegroups.com
> <https://groups.google.com/d/msgid/jupyter/ffd113b2-d2b9-46e1-99fc-ed17e05423cf%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jupyter/CAOvn4qje_iUFmsmszUa5u7XhatO6Nt--ADtpssVxn6HV%2B2k0Ag%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to