janpfeifer / gonb

GoNB, a Go Notebook Kernel for Jupyter
https://github.com/janpfeifer/gonb
MIT License
631 stars 35 forks source link

fmt Scan* input prompt #38

Closed quickwritereader closed 1 year ago

quickwritereader commented 1 year ago

It does not give an input prompt and One needs to interrupt as well I'm using Sscan for now.

janpfeifer commented 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:

  1. If your input is small, since it's easy to enter input in the cell, would it work for you to provide the input as a flag ? You can pass flags after the %% special command, so it's convenient for small inputs. See tutorial, it provides an examples.
  2. If you add the special command %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.

quickwritereader commented 1 year ago

@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

quickwritereader commented 1 year ago

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

janpfeifer commented 1 year ago

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.

janpfeifer commented 1 year ago

hi @quickwritereader ,

Some updates:

  1. Apologies, it's been a while and I didn't remember the details (it was in the %help): the %with_inputs (or %with_password) only affects the special bash commands (started with ! or !*), so it's not what you want.
  2. I just coded, in a branch called 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:

image

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

janpfeifer commented 1 year ago

Btw, this is how Jupyter notebook displays the prompt for one to type:

image

janpfeifer commented 1 year ago

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.

quickwritereader commented 1 year ago

thanks a lot. really appreciated

janpfeifer commented 1 year ago

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.