Permalink
Browse files

bundle the npm package in the Python package

at install time
  • Loading branch information...
minrk committed May 23, 2018
1 parent c39689a commit 93dabbd890397aaad0d8efb316f7e1f2129e52f3
Showing with 59 additions and 25 deletions.
  1. +6 −0 .gitignore
  2. +8 −0 MANIFEST.in
  3. +1 −4 binder/postBuild
  4. +4 −11 nbstencilaproxy/handlers.py
  5. +4 −0 nbstencilaproxy/package.json
  6. +36 −10 setup.py
@@ -82,6 +82,12 @@ celerybeat-schedule
# dotenv
.env

# general
.DS_Store
.cache
.pytest-cache
nbstencilaproxy/package-lock.json

# virtualenv
.venv
venv/
@@ -0,0 +1,8 @@
prune nbstencilaproxy/node_modules
include nbstencilaproxy/package.json
include *.md
include *.js
include *.json
include *ignore
graft archive
graft binder
@@ -5,20 +5,17 @@ export SRC_DIR="${PWD}"
export STENCILA_DIR="${PWD}"

# enable stencila-proxy extension
python setup.py build_py
pip install -e .
jupyter serverextension enable --sys-prefix --py nbstencilaproxy
jupyter nbextension install --sys-prefix --py nbstencilaproxy
jupyter nbextension enable --sys-prefix --py nbstencilaproxy

# install stencila
npm install

# set environment variables via notebook config
mkdir -p ~/.jupyter
cat >> ~/.jupyter/jupyter_notebook_config.py <<EOF
import os
os.environ["STENCILA_DIR"] = "${STENCILA_DIR}"
os.environ["STENCILA_ARCHIVE_DIR"] = "${SRC_DIR}/archive"
os.environ["STENCILA_ARCHIVE"] = "py-jupyter"
@@ -1,15 +1,14 @@
import os
import pipes
import sys

from tornado import web
from urllib.parse import urlunparse, urlparse

from notebook.utils import url_path_join as ujoin
from notebook.base.handlers import IPythonHandler

from nbserverproxy.handlers import SuperviseAndProxyHandler

here = os.path.dirname(os.path.abspath(__file__))


class AddSlashHandler(IPythonHandler):
"""Handler for adding trailing slash to URLs that need them"""
@@ -22,14 +21,8 @@ def get(self, *args):


def _find_stencila_js(name):
"""Find a stencila.js file, whether it's top-level or in node_modules"""
stencila_dir = os.environ["STENCILA_DIR"]
for sub_path in (name, os.path.join("node_modules", "nbstencilaproxy", name)):
stencila_js = os.path.join(stencila_dir, sub_path)
if os.path.exists(stencila_js):
return stencila_js
else:
raise FileNotFoundError("Could not find {} in {}".format(name, stencila_dir))
"""Find a stencila.js file in the bundled stencila npm package"""
return os.path.join(here, "node_modules", "nbstencilaproxy", name)


# define our proxy handler for proxying the application
@@ -0,0 +1,4 @@
{
"name": "nbstencilaproxy-py",
"version": "0.0.0"
}
@@ -1,17 +1,43 @@
from distutils.command.build_py import build_py
import glob
import os
from subprocess import check_call
import tempfile

import setuptools

here = os.path.dirname(os.path.abspath(__file__))
name = "nbstencilaproxy"
pkg = os.path.join(here, name)


def npm_install():
"""Install nbstencilaproxy js package"""
with tempfile.TemporaryDirectory() as td:
check_call(["npm", "pack", here], cwd=td)
tgz = glob.glob(os.path.join(td, "*.tgz"))[0]
check_call(["npm", "install", "--no-save", tgz], cwd=pkg)


class build_npm_py(build_py):
"""install with npm packages"""

def run(self):
# when installing, install npm package
npm_install()
return super().run()


setuptools.setup(
name="nbstencilaproxy",
version='0.1.0.dev',
url="https://github.com/minrk/nbstencilaproxy",
name=name,
version="0.1.0.dev",
url="https://github.com/minrk/" + name,
author="Min RK, Daniel Nüst, Ryan Lovett",
description="Jupyter extension to proxy Stencila",
packages=setuptools.find_packages(),
keywords=['Jupyter'],
classifiers=['Framework :: Jupyter'],
install_requires=[
'notebook',
'nbserverproxy >= 0.8.3',
],
package_data={'nbstencilaproxy': ['static/*']},
cmdclass={"build_py": build_npm_py},
keywords=["Jupyter"],
classifiers=["Framework :: Jupyter"],
install_requires=["notebook", "nbserverproxy >= 0.8.3"],
package_data={"nbstencilaproxy": ["static/**", "node_modules/**"]},
)

0 comments on commit 93dabbd

Please sign in to comment.