generated from jupyterlite/demo
-
Notifications
You must be signed in to change notification settings - Fork 0
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-7235: Add extension to bridge data between host and JL #2
Merged
Merged
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
78b6de9
feat: add directory with extensions
VsevolodX 3e05c9e
update: generalize to pass any data
VsevolodX ca2a339
feat: add setup script
VsevolodX 507564d
update: export nvm
VsevolodX 8afe601
chore: optimize repetition
VsevolodX 30a5820
fix: a typo
VsevolodX f331009
update: add extension building
VsevolodX e26a94f
update: install extension
VsevolodX 89cc9c5
chore: get to top level
VsevolodX 3adffef
chore: get to top level
VsevolodX 6150ab8
update: install extenison via requirement.txt
VsevolodX 964346b
update: material -> data
VsevolodX 00dd3a5
update: build extension in GHA
VsevolodX 204ec36
update: build extension in GHA 1
VsevolodX afca55d
update: bump jupyterlab to 4.0.6
VsevolodX d521216
update: add jl develop
VsevolodX 4088531
chore: update gitignore
VsevolodX 7752201
update: setup.sh
VsevolodX 64ca7a5
chore: update gitignore
VsevolodX 506f722
chore: update README
VsevolodX aa869f7
chore: update README
VsevolodX 4ab01cb
chore: revert dist exclusion
VsevolodX fa567e1
Update index.ts
timurbazhirov 61ec2e4
Update index.ts
timurbazhirov dfa0f55
Update README.md
timurbazhirov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,3 +114,6 @@ dmypy.json | |
# jupyterlite | ||
*.doit.db | ||
_output | ||
.idea | ||
.venv | ||
.yarn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
JupyterFrontEnd, | ||
JupyterFrontEndPlugin, | ||
} from "@jupyterlab/application"; | ||
|
||
import { NotebookPanel } from "@jupyterlab/notebook"; | ||
|
||
/** | ||
* Initialization data for the data-bridge extension. | ||
* Similar to https://jupyterlite.readthedocs.io/en/latest/howto/configure/advanced/iframe.html | ||
*/ | ||
const plugin: JupyterFrontEndPlugin<void> = { | ||
id: "data-bridge:plugin", | ||
description: | ||
"Extension to pass JSON data between host page and Jupyter Lite instance", | ||
autoStart: true, | ||
activate: async (app: JupyterFrontEnd) => { | ||
console.log("JupyterLab extension data-bridge is activated!"); | ||
|
||
// @ts-ignore | ||
window.sendDataToHost = (data: any) => { | ||
window.parent.postMessage( | ||
{ | ||
type: "from-iframe-to-host", | ||
data: data, | ||
}, | ||
"*" | ||
); | ||
}; | ||
|
||
// @ts-ignore | ||
window.requestDataFromHost = () => { | ||
window.parent.postMessage( | ||
{ | ||
type: "from-iframe-to-host", | ||
requestData: true, | ||
}, | ||
"*" | ||
); | ||
}; | ||
|
||
// TODO: set type for data | ||
window.addEventListener("message", async (event) => { | ||
if (event.data.type === "from-host-to-iframe") { | ||
let data = event.data.data; | ||
const dataJson = JSON.stringify(data); | ||
const code = ` | ||
import json | ||
data = json.loads('${dataJson}') | ||
`; | ||
// Similar to https://jupyterlab.readthedocs.io/en/stable/api/classes/application.LabShell.html#currentWidget | ||
// https://jupyterlite.readthedocs.io/en/latest/reference/api/ts/interfaces/jupyterlite_application.ISingleWidgetShell.html#currentwidget | ||
const currentWidget = app.shell.currentWidget; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a link to JupyterLab docs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
if (currentWidget instanceof NotebookPanel) { | ||
const notebookPanel = currentWidget; | ||
const kernel = notebookPanel.sessionContext.session?.kernel; | ||
if (kernel) { | ||
kernel.requestExecute({ code: code }); | ||
} else { | ||
console.error("No active kernel found"); | ||
} | ||
} else { | ||
console.error("Current active widget is not a notebook"); | ||
} | ||
} | ||
}); | ||
}, | ||
}; | ||
|
||
export default plugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#!/bin/bash | ||
# This script creates a JupyterLab extension using the cookiecutter template | ||
# and updates the requirements.txt file to make it installable in the current | ||
# JupyterLab environment. | ||
# It assumes that pyenv and nvm are installed and configured correctly. | ||
|
||
PYTHON_VERSION="3.10" | ||
NODE_VERSION="18" | ||
EXTENSION_NAME="data_bridge" | ||
COOKIECUTTER_TEMPLATE_PATH="$HOME/.cookiecutters/extension-cookiecutter-ts" | ||
GITHUB_TEMPLATE_URL="https://github.com/jupyterlab/extension-cookiecutter-ts" | ||
|
||
kind="frontend" | ||
author_name="Mat3ra" | ||
author_email="[email protected]" | ||
labextension_name=$EXTENSION_NAME | ||
python_name=$EXTENSION_NAME | ||
project_short_description="A JupyterLab extension that allows you to send data between notebook and host page" | ||
has_settings=n | ||
has_binder=n | ||
test=n | ||
repository="https://github.com/exabyte-io/jupyter-lite" | ||
|
||
COOKIECUTTER_OPTIONS=( | ||
"$GITHUB_TEMPLATE_URL" | ||
"--no-input" | ||
"kind=$kind" | ||
"author_name=$author_name" | ||
"author_email=$author_email" | ||
"labextension_name=$labextension_name" | ||
"python_name=$python_name" | ||
"project_short_description=$project_short_description" | ||
"has_settings=$has_settings" | ||
"has_binder=$has_binder" | ||
"test=$test" | ||
"repository=$repository" | ||
) | ||
|
||
# Ensure Python and Node.js are installed and switch to the correct versions | ||
if [ ! -d "$HOME/.pyenv/versions/$PYTHON_VERSION" ]; then | ||
pyenv install $PYTHON_VERSION | ||
fi | ||
pyenv local $PYTHON_VERSION | ||
|
||
python -m venv .venv | ||
source .venv/bin/activate | ||
|
||
export NVM_DIR="$HOME/.nvm" | ||
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" | ||
|
||
nvm install $NODE_VERSION | ||
nvm use $NODE_VERSION | ||
|
||
pip install cookiecutter jupyterlab==4 jupyterlite-core | ||
|
||
# Create directory if it doesn't exist | ||
if [ ! -d "extensions/dist" ]; then | ||
mkdir -p extensions/dist | ||
fi | ||
cd extensions/dist | ||
|
||
# Use cookiecutter with the template path if it exists, otherwise use the URL | ||
if [ ! -d "$COOKIECUTTER_TEMPLATE_PATH" ]; then | ||
cookiecutter "${COOKIECUTTER_OPTIONS[@]}" | ||
echo "Created extension using cookiecutter template." | ||
else | ||
# COOKIECUTTER_OPTIONS[0]="$COOKIECUTTER_TEMPLATE_PATH" | ||
cookiecutter "${COOKIECUTTER_OPTIONS[@]}" | ||
echo "Created extension using cached cookiecutter template." | ||
fi | ||
|
||
# Copy the index.ts file if both source and destination directories exist | ||
SRC_FILE="../src/$EXTENSION_NAME/index.ts" | ||
DEST_DIR="./$EXTENSION_NAME/src" | ||
if [ -f "$SRC_FILE" ] && [ -d "$DEST_DIR" ]; then | ||
cp "$SRC_FILE" "$DEST_DIR/index.ts" | ||
else | ||
echo "Source file or destination directory not found. Skipping copy." | ||
fi | ||
|
||
cd $EXTENSION_NAME | ||
pip install -ve . | ||
jupyter labextension develop --overwrite . | ||
|
||
# Install dependencies | ||
jlpm add @jupyterlab/application | ||
jlpm add @jupyterlab/notebook | ||
jlpm add @exabyte-io/code.js | ||
|
||
# Build the extension | ||
jlpm run build | ||
|
||
cd ../../../ | ||
|
||
# add to requirements.txt | ||
LINE="./extensions/dist/$EXTENSION_NAME" | ||
FILE='requirements.txt' | ||
grep -qF -- "$LINE" "$FILE" || echo "$LINE" >> "$FILE" | ||
|
||
# Install extension | ||
[[ ! -z $INSTALL ]] && python -m pip install -r requirements.txt | ||
|
||
# Build JupyterLite | ||
[[ ! -z $BUILD ]] && jupyter lite build --contents content --output-dir dist | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a link to Jupyter Lite docs that we followed here