Skip to content

Commit

Permalink
Scaffolding packages
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Nov 9, 2023
1 parent 12e6433 commit e9551ec
Show file tree
Hide file tree
Showing 24 changed files with 410 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"npmClient": "yarn",
"version": "independent",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"useWorkspaces": true
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"name": "JupyterCad contributors"
},
"workspaces": [
"packages/*"
"packages/*",
"python/jupytercad-core"
],
"scripts": {
"build": "lerna run build",
Expand Down
1 change: 1 addition & 0 deletions python/jupytercad-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# JupyterCad python API package
1 change: 1 addition & 0 deletions python/jupytercad-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# JupyterCad standalone application package
1 change: 1 addition & 0 deletions python/jupytercad-core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jupytercad_core/labextension
29 changes: 29 additions & 0 deletions python/jupytercad-core/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2023, JupyterCad contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1 change: 1 addition & 0 deletions python/jupytercad-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# JupyterCad Core package
5 changes: 5 additions & 0 deletions python/jupytercad-core/install.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"packageManager": "python",
"packageName": "jupytercad_core",
"uninstallInstructions": "Use your Python package manager (pip, conda, etc.) to uninstall the package jupytercad_core"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ServerApp": {
"jpserver_extensions": {
"jupytercad_core": true
}
}
}
32 changes: 32 additions & 0 deletions python/jupytercad-core/jupytercad_core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
try:
from ._version import __version__
except ImportError:
# Fallback when using the package in dev mode without installing
# in editable mode with pip. It is highly recommended to install
# the package from a stable release or in editable mode: https://pip.pypa.io/en/stable/topics/local-project-installs/#editable-installs
import warnings

warnings.warn("Importing 'jupytercad_core' outside a proper installation.")
__version__ = "dev"
from .handlers import setup_handlers


def _jupyter_labextension_paths():
return [{"src": "labextension", "dest": "jupytercad-core"}]


def _jupyter_server_extension_points():
return [{"module": "jupytercad_core"}]


def _load_jupyter_server_extension(server_app):
"""Registers the API handler to receive HTTP requests from the frontend extension.
Parameters
----------
server_app: jupyterlab.labapp.LabApp
JupyterLab application instance
"""
setup_handlers(server_app.web_app)
name = "jupytercad_core"
server_app.log.info(f"Registered {name} server extension")
25 changes: 25 additions & 0 deletions python/jupytercad-core/jupytercad_core/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import json

from jupyter_server.base.handlers import APIHandler
from jupyter_server.utils import url_path_join
import tornado


class RouteHandler(APIHandler):
# The following decorator should be present on all verb methods (head, get, post,
# patch, put, delete, options) to ensure only authorized user can request the
# Jupyter server
@tornado.web.authenticated
def get(self):
self.finish(
json.dumps({"data": "This is /jupytercad-core/get-example endpoint!"})
)


def setup_handlers(web_app):
host_pattern = ".*$"

base_url = web_app.settings["base_url"]
route_pattern = url_path_join(base_url, "jupytercad-core", "get-example")
handlers = [(route_pattern, RouteHandler)]
web_app.add_handlers(host_pattern, handlers)
91 changes: 91 additions & 0 deletions python/jupytercad-core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"name": "@jupytercad/jupytercad-core",
"version": "0.1.0",
"description": "JupyterCad core extension",
"keywords": [
"jupyter",
"jupyterlab",
"jupyterlab-extension"
],
"homepage": "https://github.com/jupyter-cad/jupytercad",
"bugs": {
"url": "https://github.com/jupyter-cad/jupytercad/issues"
},
"license": "BSD-3-Clause",
"author": "JupyterCad contributors",
"files": [
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
"style/**/*.{css,js,eot,gif,html,jpg,json,png,svg,woff2,ttf}"
],
"main": "lib/index.js",
"types": "lib/index.d.ts",
"style": "style/index.css",
"repository": {
"type": "git",
"url": "https://github.com/jupyter-cad/jupytercad.git"
},
"scripts": {
"build": "jlpm build:lib && jlpm build:labextension:dev",
"build:prod": "jlpm clean && jlpm build:lib:prod && jlpm build:labextension",
"build:labextension": "jupyter labextension build .",
"build:labextension:dev": "jupyter labextension build --development True .",
"build:lib": "tsc --sourceMap",
"build:lib:prod": "tsc",
"clean": "jlpm clean:lib",
"clean:lib": "rimraf lib tsconfig.tsbuildinfo",
"clean:lintcache": "rimraf .eslintcache .stylelintcache",
"clean:labextension": "rimraf jupytercad_core/labextension jupytercad_core/_version.py",
"clean:all": "jlpm clean:lib && jlpm clean:labextension && jlpm clean:lintcache",
"eslint": "jlpm eslint:check --fix",
"eslint:check": "eslint . --cache --ext .ts,.tsx",
"install:extension": "jlpm build",
"lint": "jlpm stylelint && jlpm prettier && jlpm eslint",
"lint:check": "jlpm stylelint:check && jlpm prettier:check && jlpm eslint:check",
"prettier": "jlpm prettier:base --write --list-different",
"prettier:base": "prettier \"**/*{.ts,.tsx,.js,.jsx,.css,.json,.md}\"",
"prettier:check": "jlpm prettier:base --check",
"stylelint": "jlpm stylelint:check --fix",
"stylelint:check": "stylelint --cache \"style/**/*.css\"",
"watch": "run-p watch:src watch:labextension",
"watch:src": "tsc -w --sourceMap",
"watch:labextension": "jupyter labextension watch ."
},
"dependencies": {
"@jupyterlab/application": "^4.0.0",
"@jupyterlab/coreutils": "^6.0.0",
"@jupyterlab/services": "^7.0.0"
},
"devDependencies": {
"@jupyterlab/builder": "^4.0.0",
"@types/json-schema": "^7.0.11",
"@types/react": "^18.0.26",
"@types/react-addons-linked-state-mixin": "^0.14.22",
"css-loader": "^6.7.1",
"mkdirp": "^1.0.3",
"npm-run-all": "^4.1.5",
"style-loader": "^3.3.1",
"yjs": "^13.5.0"
},
"sideEffects": [
"style/*.css",
"style/index.js"
],
"styleModule": "style/index.js",
"publishConfig": {
"access": "public"
},
"jupyterlab": {
"discovery": {
"server": {
"managers": [
"pip"
],
"base": {
"name": "jupytercad_core"
}
}
},
"extension": true,
"outputDir": "jupytercad_core/labextension"
}
}
79 changes: 79 additions & 0 deletions python/jupytercad-core/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
[build-system]
requires = ["hatchling>=1.5.0", "jupyterlab>=4.0.0,<5", "hatch-nodejs-version>=0.3.2"]
build-backend = "hatchling.build"

[project]
name = "jupytercad_core"
readme = "README.md"
license = { file = "LICENSE" }
requires-python = ">=3.8"
classifiers = [
"Framework :: Jupyter",
"Framework :: Jupyter :: JupyterLab",
"Framework :: Jupyter :: JupyterLab :: 4",
"Framework :: Jupyter :: JupyterLab :: Extensions",
"Framework :: Jupyter :: JupyterLab :: Extensions :: Prebuilt",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
dependencies = [
"jupyter_server>=2.0.1,<3"
]
dynamic = ["version", "description", "authors", "urls", "keywords"]

[tool.hatch.version]
source = "nodejs"

[tool.hatch.metadata.hooks.nodejs]
fields = ["description", "authors", "urls"]

[tool.hatch.build.targets.sdist]
artifacts = ["jupytercad_core/labextension"]
exclude = [".github", "binder"]

[tool.hatch.build.targets.wheel.shared-data]
"jupytercad_core/labextension" = "share/jupyter/labextensions/jupytercad-core"
"install.json" = "share/jupyter/labextensions/jupytercad-core/install.json"
"jupyter-config/server-config" = "etc/jupyter/jupyter_server_config.d"

[tool.hatch.build.hooks.version]
path = "jupytercad_core/_version.py"

[tool.hatch.build.hooks.jupyter-builder]
dependencies = ["hatch-jupyter-builder>=0.5"]
build-function = "hatch_jupyter_builder.npm_builder"
ensured-targets = [
"jupytercad_core/labextension/static/style.js",
"jupytercad_core/labextension/package.json",
]
skip-if-exists = ["jupytercad_core/labextension/static/style.js"]

[tool.hatch.build.hooks.jupyter-builder.build-kwargs]
build_cmd = "build:prod"
npm = ["jlpm"]

[tool.hatch.build.hooks.jupyter-builder.editable-build-kwargs]
build_cmd = "install:extension"
npm = ["jlpm"]
source_dir = "src"
build_dir = "jupytercad_core/labextension"

[tool.jupyter-releaser.options]
version_cmd = "hatch version"

[tool.jupyter-releaser.hooks]
before-build-npm = [
"python -m pip install 'jupyterlab>=4.0.0,<5'",
"jlpm",
"jlpm build:prod"
]
before-build-python = ["jlpm clean:all"]

[tool.check-wheel-contents]
ignore = ["W002"]
1 change: 1 addition & 0 deletions python/jupytercad-core/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__import__("setuptools").setup()
46 changes: 46 additions & 0 deletions python/jupytercad-core/src/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { URLExt } from '@jupyterlab/coreutils';

import { ServerConnection } from '@jupyterlab/services';

/**
* Call the API extension
*
* @param endPoint API REST end point for the extension
* @param init Initial values for the request
* @returns The response body interpreted as JSON
*/
export async function requestAPI<T>(
endPoint = '',
init: RequestInit = {}
): Promise<T> {
// Make request to Jupyter API
const settings = ServerConnection.makeSettings();
const requestUrl = URLExt.join(
settings.baseUrl,
'jupytercad-core', // API Namespace
endPoint
);

let response: Response;
try {
response = await ServerConnection.makeRequest(requestUrl, init, settings);
} catch (error) {
throw new ServerConnection.NetworkError(error as any);
}

let data: any = await response.text();

if (data.length > 0) {
try {
data = JSON.parse(data);
} catch (error) {
console.log('Not a JSON response body.', response);
}
}

if (!response.ok) {
throw new ServerConnection.ResponseError(response, data.message || data);
}

return data;
}
30 changes: 30 additions & 0 deletions python/jupytercad-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';

import { requestAPI } from './handler';

/**
* Initialization data for the jupytercad-core extension.
*/
const plugin: JupyterFrontEndPlugin<void> = {
id: 'jupytercad-core:plugin',
description: 'JupyterCad core extension',
autoStart: true,
activate: (app: JupyterFrontEnd) => {
console.log('JupyterLab extension jupytercad-core is activated!');

requestAPI<any>('get-example')
.then(data => {
console.log(data);
})
.catch(reason => {
console.error(
`The jupytercad_core server extension appears to be missing.\n${reason}`
);
});
}
};

export default plugin;
5 changes: 5 additions & 0 deletions python/jupytercad-core/style/base.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
See the JupyterLab Developer Guide for useful CSS Patterns:
https://jupyterlab.readthedocs.io/en/stable/developer/css.html
*/
1 change: 1 addition & 0 deletions python/jupytercad-core/style/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import url('base.css');
1 change: 1 addition & 0 deletions python/jupytercad-core/style/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './base.css';
8 changes: 8 additions & 0 deletions python/jupytercad-core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfigbase.json",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src"
},
"include": ["src/**/*", "src/schema/*.json", "src/_interface/*.json"]
}
Loading

0 comments on commit e9551ec

Please sign in to comment.