Repository navigation
feat(widgets): serve widget library files from Jupyter Server #136
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
Draft
mfranczel
wants to merge
3
commits into
main
Choose a base branch
from
michal/widget-assets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+442
−2
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,128 @@ | ||
| """Jupyter Server extension that serves widget files. | ||
|
|
||
| ``GET /deepnote/widget-assets/<kind>/<path>`` returns a file installed in the | ||
| project's environment. Requests need the Jupyter token. | ||
|
|
||
| Where each kind is looked up: | ||
|
|
||
| - ``labextensions``: ``<module>/<file>`` in an installed JupyterLab extension. | ||
| Scoped modules take two segments, like ``@bokeh/jupyter_bokeh``. | ||
| - ``bokeh``: the ``server/static/js`` folder of the bokeh package. | ||
| - ``panel``: the ``dist`` folder of the panel package. | ||
|
|
||
| Only ``.js``, ``.css`` and ``.json`` files are served. Packages are found | ||
| without importing them, so no user code runs in the server. | ||
| """ | ||
|
|
||
| import importlib.util | ||
| import re | ||
| import sys | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING, Dict, List, Optional | ||
|
|
||
| from jupyter_core.paths import jupyter_path | ||
| from jupyter_server.base.handlers import APIHandler | ||
| from jupyter_server.utils import url_path_join | ||
| from tornado import web | ||
|
|
||
| if TYPE_CHECKING: | ||
| from jupyter_server.serverapp import ServerApp | ||
|
|
||
| CONTENT_TYPES: Dict[str, str] = { | ||
| ".js": "text/javascript; charset=utf-8", | ||
| ".css": "text/css; charset=utf-8", | ||
| ".json": "application/json", | ||
| } | ||
|
|
||
| # One path segment: letters, digits and "_.@-", not starting with a dot | ||
| _PATH_SEGMENT = re.compile(r"[A-Za-z0-9_@-][A-Za-z0-9_.@-]*") | ||
|
|
||
| _PACKAGE_STATIC_DIRS: Dict[str, List[str]] = { | ||
| "bokeh": ["server", "static", "js"], | ||
| "panel": ["dist"], | ||
| } | ||
|
|
||
|
|
||
| def find_widget_asset(kind: str, path: str) -> Optional[Path]: | ||
| """Find the file a widget asset request asks for. | ||
|
|
||
| Args: | ||
| kind: ``labextensions``, ``bokeh`` or ``panel``. | ||
| path: The ``/``-separated path after the kind. | ||
|
|
||
| Returns: | ||
| The file's real path, or None if the path isn't allowed or the file | ||
| is missing. | ||
| """ | ||
| segments = path.split("/") | ||
| if not all(_PATH_SEGMENT.fullmatch(segment) for segment in segments): | ||
| return None | ||
|
|
||
| if kind == "labextensions": | ||
| return _find_labextension_file(segments) | ||
|
|
||
| if kind not in _PACKAGE_STATIC_DIRS: | ||
| return None | ||
| package_dir = _find_package_dir(kind) | ||
| if package_dir is None: | ||
| return None | ||
| return _resolve_inside(package_dir.joinpath(*_PACKAGE_STATIC_DIRS[kind]), segments) | ||
|
|
||
|
|
||
| def _find_labextension_file(segments: List[str]) -> Optional[Path]: | ||
| """Find a file in the first installed copy of a JupyterLab extension.""" | ||
| module_length = 2 if segments[0].startswith("@") else 1 | ||
| module, file_segments = segments[:module_length], segments[module_length:] | ||
| if not file_segments: | ||
| return None | ||
|
|
||
| roots = [Path(root) for root in jupyter_path("labextensions")] | ||
| roots.append(Path(sys.base_prefix, "share", "jupyter", "labextensions")) | ||
| for root in dict.fromkeys(roots): | ||
| module_dir = root.joinpath(*module) | ||
| if module_dir.joinpath("package.json").is_file(): | ||
| return _resolve_inside(module_dir, file_segments) | ||
| return None | ||
|
|
||
|
|
||
| def _find_package_dir(name: str) -> Optional[Path]: | ||
| """Return the folder of an installed package without importing it.""" | ||
| try: | ||
| spec = importlib.util.find_spec(name) | ||
| except (ImportError, ValueError): | ||
| return None | ||
| if spec is None or not spec.submodule_search_locations: | ||
| return None | ||
| return Path(list(spec.submodule_search_locations)[0]) | ||
|
|
||
|
|
||
| def _resolve_inside(root: Path, segments: List[str]) -> Optional[Path]: | ||
| """Return the real path of a servable file inside ``root``, or None.""" | ||
| try: | ||
| real_root = root.resolve(strict=True) | ||
| candidate = real_root.joinpath(*segments).resolve(strict=True) | ||
| except (OSError, RuntimeError): | ||
| return None | ||
| if not candidate.is_relative_to(real_root) or not candidate.is_file(): | ||
| return None | ||
| if candidate.suffix not in CONTENT_TYPES: | ||
| return None | ||
| return candidate | ||
|
|
||
|
|
||
| class WidgetAssetHandler(APIHandler): | ||
| """Serves widget files to requests that carry the Jupyter token.""" | ||
|
|
||
| @web.authenticated | ||
| def get(self, kind: str, path: str) -> None: | ||
| """Send the requested file, or 404 when there isn't one.""" | ||
| asset = find_widget_asset(kind, path) | ||
| if asset is None: | ||
| raise web.HTTPError(404) | ||
| self.finish(asset.read_bytes(), set_content_type=CONTENT_TYPES[asset.suffix]) | ||
|
|
||
|
|
||
| def _load_jupyter_server_extension(serverapp: "ServerApp") -> None: | ||
| """Add the widget asset route when Jupyter Server starts.""" | ||
| route = url_path_join(serverapp.base_url, "deepnote/widget-assets/([a-z]+)/(.+)") | ||
| serverapp.web_app.add_handlers(".*$", [(route, WidgetAssetHandler)]) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.