gopherdata / gophernotes

The Go kernel for Jupyter notebooks and nteract.
MIT License
3.8k stars 264 forks source link

First stab at implementing shell command. #202

Closed Oshuma closed 4 years ago

Oshuma commented 4 years ago

Issue #196

The only thing left is to steam stdout/stderr to Jupyter. I couldn't quite figure out how to do that, so if anyone could give me some pointers/examples, I can implement it.

cosmos72 commented 4 years ago

Looks good, thanks :)

To stream stdout/stderr to Jupyter, you need four things:

  1. get the stdout/stderr of the command as Go streams: you can use the methods Cmd.StdoutPipe() and Cmd.StderrPipe() of the struct os/exec.Cmd you are already using
  2. modify Kernel.handleExecuteRequest to create the two JupyterStreamWriter immediately - it currently creates them inside goroutines - and pass them to doEval() and further down to your evalShellCommand.
  3. a goroutine reading from Cmd.StdoutPipe() and writing into the first JupyterStreamWriter. You can copy and adapt the code in kernel.go, lines 406-411
  4. another goroutine, reading from Cmd.StderrPipe() and writing into the second JupyterStreamWriter. You can copy and adapt the code in kernel.go, lines 413-417

Note: be careful to wait for the command to exit and for the goroutines to finish before returning from evalShellCommand

Oshuma commented 4 years ago

@cosmos72 Sounds good. I'll implement that as soon as I get a chance and update this PR.

Oshuma commented 4 years ago

@cosmos72 Can you verify/merge when you get a chance?

cosmos72 commented 4 years ago

merged, with some little improvements. Thanks!