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

Support for accessing Python scope #67

Open
lalop opened this issue Jan 16, 2023 · 6 comments
Open

Support for accessing Python scope #67

lalop opened this issue Jan 16, 2023 · 6 comments
Assignees
Labels
enhancement New feature or request question Further information is requested

Comments

@lalop
Copy link

lalop commented Jan 16, 2023

According to pyodide runPython can return a value https://pyodide.org/en/stable/usage/quickstart.html?highlight=expression#running-python-code
I was looking to the code and I see that you wrap the run call into an exec method which is part of the problem here.
Right now I don't really understand why this wrapper is here and I assume that's because I'm pretty new to the python engine.
In an other hand I don't see how to get anything else then stdout and/or stderr with this hook.

I'll be happy to contribute if you think that could be a great enhancement to get the result of code execution and/or share some variables like globals for exemple.

@lalop lalop changed the title runPython coulds return a value runPython can return a value Jan 16, 2023
@elilambnz
Copy link
Owner

Hi, are you asking why the runPython function doesn't return a value? If so I'm happy to explain this further.

We're always looking to improve the API to make it more useful, so suggestions are certainly welcome.

Currently, there isn't support for directly accessing the Python scope (i.e. globals) from JavaScript, or vice versa. Ideally, I'd like to support this behaviour but due to using web workers to run the Pyodide instances, this is tricky to get working.

@elilambnz elilambnz self-assigned this Jan 16, 2023
@elilambnz elilambnz added the question Further information is requested label Jan 16, 2023
@lalop
Copy link
Author

lalop commented Jan 16, 2023

My first question is why are you using python's exec function instead of a direct execution.
Then I would like to use this hook to run a python script that computes a value and use this value in javascript. If you have some idea on the way to add this I can contribute in that way :)

@elilambnz
Copy link
Owner

To answer your first question, every execution is wrapped in some additional Python code to improve the default error handling behaviour.

Getting the value of variables directly from Python (other than from stdout) is currently unsupported, so I'm unable to provide any examples of this just yet. We're looking into supporting access to globals, so once I have more to share I'll update this issue with further information.

@lalop
Copy link
Author

lalop commented Jan 16, 2023

ok, thank you for your answer

@elilambnz elilambnz changed the title runPython can return a value Support for accessing Python scope Jul 23, 2023
@elilambnz elilambnz added the enhancement New feature or request label Jul 23, 2023
@dukecyto
Copy link

Hi,

Very exciting project!
I had initial success interoperating a React app with NumPy/SciPy for scientific computing.
For now, the numerical array data in TS is passed to Python by embedding it in the Python script string, and the result from Python is passed back to TS through stdout.
A smoother interoperation is crucial, and I am very much interested in the progress of this issue.

It seems that you have a PR on accessing the Python global scope hanging around for a while #71
What is holding back from merging this?

Thanks!

@c0mm4nd
Copy link

c0mm4nd commented Jul 4, 2024

I have forked and added the feature of getting value from globals

image

To implement this, I removed the author-improved error handling, so it is just for anyone who needs to get variable values from globals. Besides, some advanced vars like functions in Python cannot be transmitted from the JS worker to the React Webpage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants