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

How to run a Python code through Pyodide/Starboard outside the notebook cells? #60

Open
veraero-tech opened this issue Aug 15, 2021 · 3 comments

Comments

@veraero-tech
Copy link

veraero-tech commented Aug 15, 2021

Great project.
I'd like to make a 'plugin' on top of the starboard notebook. However, for this I'd like to run Python code without interacting and displaying it in the notebook cells. So when e.g. a notebook cell contains python code 'x=1' and has been normally run I'd like to access these variables. I know that in Pyodide this is possible by just running running seperate runPython commands to write and read variables ,e.g.:

let pyodide = await loadPyodide({
          indexURL : "https://cdn.jsdelivr.net/pyodide/v0.18.0/full/"
        });
        console.log(pyodide.runPython("x = 9 + 1"));
        console.log(pyodide.runPython("print(x)"));

I know when using the barebones demo it is hard to do this due to the iFrame restrictions. I'm now running StarBoard Notebook without an iFrame and I can access the variable 'runtime' to control StarBoard (I'm aware of the security risks).
So ideally I would run Python code - to extract data from the notebook cells - outside the notebook cells from javascript.
Either directly through Pyodide or through the Notebook's runtime.

When for example using the Python Pandas library in the notebook cells I can extract in the background the column names of a Pandas Dataframe, and display those in a sidebar.

I've been trying to find how to do this from the starboard-python and starboard-notebook source code.
I guess it can be done through 'runPythonAsync' or 'runPython', but I don't know how to access these function from a js script in the browser. (Sorry I'm strong Python and a bit of an JS/TS noob).

I guess to directly access the Pyodide variable it depends also if Pyodide is running in the maintread or in a web worker. Today I was able to access the 'pyodide' variable in the Chrome Console, but somehow I cannot replicate it. I think the notebook was initialized to run python in the maintread.

I guess if the runPython functionality can be exposed through the 'runtime' it would make it possible to create a lof of other creative plugins/ applications on top of this.

@gzuidhof
Copy link
Owner

Hi @veraero-tech :)

The current version of pyodide, when run in the main thread (it is now actually possible to run pyodide in a webworker too, but the default is main thread), adds a variable on window called pyodide. So you could do this:

Pyodide notebook example

I can't promise that this wont' break in some future version, but I imagine a variant of this will still exist.

I know when using the barebones demo it is hard to do this due to the iFrame restrictions. I'm now running StarBoard Notebook without an iFrame and I can access the variable 'runtime' to control StarBoard (I'm aware of the security risks).

You can definitely do this within an iframe too, you would usually make a plugin that customizes the notebook how you want it. Anyway for version 1 non-iframe mode is fine, also if your website otherwise is stateless anyway there is no risk (i.e. there is no login to abuse).

@veraero-tech
Copy link
Author

Thanks for the swift feedback. :-)
I'm able to run the code from the notebook cells, but I'm not able to access pyodide outside the cells (e.g. from the Chrome Developers console.
Sorry, I'm too in experienced outside my Python backend skills. Time to learn some more JS/TS and write plugins for StarBoard haha.

@gzuidhof
Copy link
Owner

Your notebook runs in an iframe in the browser for sandboxing purposes. That sandbox will have its own environment, so you have to set your browser console to that context. In chrome you do it here:

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