Closed quickwritereader closed 1 year ago
Indeed, inputting data through the terminal is a bit awkward now -- it's not possible (not in an easy way that I know) for the kernel (GoNB) to guess that your program wants to read some input. We can ask Jupyter for an input box in the browser, but if we do request an input box from Jupyter the user has to click on it, which is annoying.
So instead, 2 alternatives:
%%
special command, so it's convenient for small inputs. See tutorial, it provides an examples.%with_inputs
to your cell, it will ask Jupyter for an input box: which is annoying if you don't read from it ... but if you do, it may work for you.Let me know if this helps.
If not, let me know, I could add a function in gonbui
library to request for an input box. It's a long standing TODO.
@janpfeifer could you show a small example for %with_inputs.
It is not so important. I am using Gonb to learn and to teach golang mixed with my notes. So I faced this issue while teaching Scan and it's sisters. Just I opened an issue as I know that it works in python.
So You could label it as an extra , or even You can close the issue
but I see that having gonb input prompt where it writes to named pipe(linux) and compiled go stdin tied to that named pipe could be good option. But I think for my case I just needed presupplied stdin content. I was not able to use %with_inputs or %% option for it
Oh, I'm sorry to hear that, I never actually made use of "%with_input" except when I created it at the very first version -- sadly it's hard to test with jupyter (at least I don't know how). Let me take a look at it. I'll come up with something by tomorrow.
hi @quickwritereader ,
Some updates:
%help
): the %with_inputs
(or %with_password
) only affects the special bash commands (started with !
or !*
), so it's not what you want.input
, a new version that supports the gonbui.RequestInput(prompt string, password bool)
function. Whenever you call it, it will prompt the user in the Jupyter notebook for a text field (with the given prompt
, and optionally hidden content if password
is true). Whatever the user types is piped into the cells os.Stdin
, meaning it can be read with a simple fmt.Scanf
.Would (2) above work for you ?
Here is an example of how it would look like:
import (
"fmt"
"github.com/janpfeifer/gonb/gonbui"
)
%%
gonbui.RequestInput("Tell me a number: ", false)
var x int
_, err := fmt.Scan(&x)
if err != nil { panic(err) }
fmt.Printf("The number you typed was %d\n", x)
gonbui.RequestInput("Tell me a secret: ", true)
var secret string
_, err = fmt.Scan(&secret)
if err != nil { panic(err) }
fmt.Printf("Shh! Your secret was %q\n", secret)
See image here:
You can also test it by checkout (cloning locally) the input
branch.
In any case, if it works for you, I'll create a new release later today with it.
cheers
Btw, this is how Jupyter notebook displays the prompt for one to type:
Yes, I realize the need for the extra call to gonbui.RequestInput
is not ideal for the teaching material -- since they don't need to do this in the command line ...
But anyway, it's a good functionality to have around -- like in Python.
thanks a lot. really appreciated
I updated the tutorial with an entry about inputs, and released v0.7.4 with the updates. Hopefully it will work.
Closing the issue, but let me know if you need anything else for teaching @quickwritereader -- education is very dear topic to me, and high on my priority list :)
ps.: Btw, if you also like machine learning, check out github.com/gomlx/gomlx, it's a full featured ML framework for Go, using XLA (same used in TensorFlow and Jax, so similar speeds), co-developed with GoNB.
It does not give an input prompt and One needs to interrupt as well I'm using Sscan for now.