kluctl/go-embed-python

Run multiple scripts in same environment

Opened this issue · 4 comments

A bit like Jupyter notebooks, I want to run cells of code against the same context

in my case I have a variable in Go, a = 2 and i run this.
Then later I want to do b = a + 2 and run that from Go.
Currently doing this will result in an error a does not exist.
It seems then that all scripts must be run from top to bottom and include everything.
Is there a way to share the context in which previous python has been run?
I thought that the EmbeddedPython object would hold its state but apparently not?
Is this possible?

EmbeddedPython does not hold state and probably will never do so. It's really "just" a helper to embed the Python distribution/runtime itself and then execute it in a convenient way. Everything state related is up to the consumer of this library.

hey @codablock thanks for responding.

Could you suggest a way to approach this?
My thought is to save the output of

	cmd := a.ep.PythonCmd("-c", script)
	cmd.Stdout = &stdoutBuf
	cmd.Stderr = &stderrBuf
	err := cmd.Run()

but that won't result in me having the variables in a python 'world' if i do this, just the outputs.
An alternative is to combine scripts but that would mean everything gets re-run every time.
Is there a way you envisage a consumer managing python state that you could share?

You could run the python cmd without a script and then redirect stdout/stdin in a way so that you can do live coding. It's then the same as running python from your shell and just working with the Python shell. But, this is still far away from what something Jupyter offers you.

I think you mean

	cmd := a.ep.PythonCmd()
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Run()

which i think you are suggesting would keep the session open and then i somehow pipe input python to this with stdinputs or something...
I tried to use the cpython go api bindings to control the session, but that didn't work.
Not sure I quite see what you are saying here, its a shame not to be able to use same session an not quite sure how to do that as a consumer of this.