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

Feature/SOF-7286 #12

Merged
merged 32 commits into from Mar 21, 2024
Merged

Feature/SOF-7286 #12

merged 32 commits into from Mar 21, 2024

Conversation

VsevolodX
Copy link
Contributor

Adds changes to JupyterLite extension data_bridge that is responsible for exchanging data between JL pyodide environment and JS component/host.

) => {
console.log("JupyterLab extension data-bridge is activated!");

// Send path of the currently opened notebook to the host page when the notebook is opened
notebookTracker.currentChanged.connect((sender, notebookPanel) => {
// Variable to hold the data from the host page, accessible from any notebook and kernel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe `Reusing the "app" variable ..."

);
sessionContext.session?.kernel?.statusChanged.connect((kernel, status) => {
// @ts-ignore
console.debug(status, kernel.id, kernel.dataFromHost);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

// @ts-ignore
app.dataFromHost = JSON.stringify(event.data.payload);
//@ts-ignore
console.debug("Data from host received. app:", app.dataFromHost);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

action: "get-data",
payload: {}
},
"*"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define as a variable MESSAGE_GET_DATA_CONTENT above

console.debug(status, kernel.id, kernel.dataFromHost);
// @ts-ignore
if (status === 'idle' && kernel.dataFromHost !== app.dataFromHost) {
// Custom flag to prevent from loading the same data multiple times
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save previous data inside the current kernel to avoid reloading

window.parent.postMessage(
{
type: "from-iframe-to-host",
data: data,
action: "set-data",
payload: data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above - MESSAGE_SET_DATA_CONTENT

console.error("Current active widget is not a notebook");
}
// @ts-ignore
app.dataFromHost = JSON.stringify(event.data.payload);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON stringify should be executed before we send the message, probably and/or we should do the validation with schema

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, validation happens when we do the entity.toJSON() and send it, then here we use stringify due to how it's going to be consumed by pyodide code execution.

* @param data
*/
const loadData = (kernel: IKernelConnection, data: JSON) => {
const dataFromHostString = JSON.stringify(data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually necessary to escape all the special characteres, so that this can be used directly in the Python code.

package.json Outdated
@@ -3,4 +3,5 @@
"start": "python -m http.server -b localhost -d ./dist",
"build": "python -m pip install -r requirements.txt; cp -rL content content-resolved; jupyter lite build --contents content-resolved --output-dir dist"
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

* @param data string representation of JSON
*/
const loadData = (kernel: IKernelConnection, data: string) => {
const code = `import json\ndata_from_host = json.loads("${data}")`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VsevolodX VsevolodX merged commit 2d1d52a into main Mar 21, 2024
2 checks passed
@VsevolodX VsevolodX deleted the feature/SOF-7286 branch March 21, 2024 04:25
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

Successfully merging this pull request may close these issues.

None yet

2 participants