Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run multiple scripts in same environment #45

Open
amlwwalker opened this issue Jul 15, 2024 · 4 comments
Open

Run multiple scripts in same environment #45

amlwwalker opened this issue Jul 15, 2024 · 4 comments

Comments

@amlwwalker
Copy link

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?

@codablock
Copy link
Contributor

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.

@amlwwalker
Copy link
Author

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?

@codablock
Copy link
Contributor

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.

@amlwwalker
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants