Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
jupyter_ext_default_page: Added reminding information when there isn'…
Browse files Browse the repository at this point in the history
…t nni kicked off
  • Loading branch information
Johnsonms authored and zhaofu li committed Sep 17, 2021
2 parents 20fde68 + c5c3db2 commit 59f50ca
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 3 deletions.
33 changes: 32 additions & 1 deletion nni/tools/jupyter_extension/management.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import json
from pathlib import Path
import shutil
import os

from jupyter_core.paths import jupyter_config_dir, jupyter_data_dir

import nni_node

def _get_jupyter_lab_version():
try:
import jupyterlab
return jupyterlab.__version__
except ImportError:
return '3.x'

jupyter_lab_major_version = _get_jupyter_lab_version().split('.')[0]

_backend_config_file = Path(jupyter_config_dir(), 'jupyter_server_config.d', 'nni.json')
_backend_config_content = {
'ServerApp': {
Expand All @@ -14,6 +24,14 @@
}
}
}
_v2_backend_config_file = Path(jupyter_config_dir(), 'jupyter_notebook_config.d', 'nni.json')
_v2_backend_config_content = {
"NotebookApp": {
"nbserver_extensions": {
"nni.tools.jupyter_extension": True
}
}
}

_frontend_src = Path(nni_node.__path__[0], 'jupyter-extension')
_frontend_dst = Path(jupyter_data_dir(), 'labextensions', 'nni-jupyter-extension')
Expand All @@ -23,8 +41,21 @@ def install():
_backend_config_file.write_text(json.dumps(_backend_config_content))

_frontend_dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(_frontend_src, _frontend_dst)

if jupyter_lab_major_version == '2':
_v2_backend_config_file.parent.mkdir(parents=True, exist_ok=True)
_v2_backend_config_file.write_text(json.dumps(_v2_backend_config_content))

if (_frontend_src.is_symlink()):
linkto = os.path.realpath(_frontend_src)
os.symlink(linkto, _frontend_dst)
else:
shutil.copytree(_frontend_src, _frontend_dst)
else:
shutil.copytree(_frontend_src, _frontend_dst)

def uninstall():
_backend_config_file.unlink()
if jupyter_lab_major_version == '2':
_v2_backend_config_file.unlink()
shutil.rmtree(_frontend_dst)
36 changes: 36 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
$ NNI_RELEASE=2.0 python setup.py build_ts
$ NNI_RELEASE=2.0 python setup.py bdist_wheel -p manylinux1_x86_64
for jupyterlab 2.x package:
$ JUPYTER_LAB_VERSION=2.3.1 NNI_RELEASE=2.0 python setup.py build_ts
$ JUPYTER_LAB_VERSION=2.3.1 NNI_RELEASE=2.0 python setup.py bdist_wheel -p manylinux1_x86_64
Where "2.0" is version string and "manylinux1_x86_64" is platform.
The platform may also be "macosx_10_9_x86_64" or "win_amd64".
Expand All @@ -65,6 +69,26 @@

release = os.environ.get('NNI_RELEASE')

def _get_jupyter_lab_version():
try:
import jupyterlab
return jupyterlab.__version__
except ImportError:
return '3.x'

jupyter_lab_major_version = _get_jupyter_lab_version().split('.')[0]

def check_jupyter_lab_version():
environ_version = os.environ.get('JUPYTER_LAB_VERSION')

jupyter_lab_version = _get_jupyter_lab_version()

if environ_version:
if jupyter_lab_version.split('.')[0] != environ_version.split('.')[0]:
sys.exit(f'ERROR: To build a jupyter lab extension, run "JUPYTER_LAB_VERSION={jupyter_lab_version}", current: {environ_version} ')
elif jupyter_lab_version.split('.')[0] != '3':
sys.exit(f'ERROR: To build a jupyter lab extension, run "JUPYTER_LAB_VERSION={jupyter_lab_version}" first for nondefault version(3.x)')

def _setup():
setuptools.setup(
name = 'nni',
Expand All @@ -91,6 +115,8 @@ def _setup():
'nni_node': _find_node_files() # note: this does not work before building
},

data_files = _get_data_files(),

python_requires = '>=3.6',
install_requires = _read_requirements_txt('dependencies/required.txt'),
extras_require = {
Expand All @@ -115,6 +141,12 @@ def _setup():
}
)

def _get_data_files():
data_files = []
if jupyter_lab_major_version == '2':
extension_file = glob.glob("nni_node/jupyter-extension/extensions/nni-jupyter-extension*.tgz")
data_files = [('share/jupyter/lab/extensions', extension_file)]
return data_files

def _find_python_packages():
packages = []
Expand Down Expand Up @@ -178,12 +210,16 @@ def finalize_options(self):
pass

def run(self):
check_jupyter_lab_version()
setup_ts.build(release)

class Build(build):
def run(self):
if not release:
sys.exit('Please set environment variable "NNI_RELEASE=<release_version>"')

check_jupyter_lab_version()

if os.path.islink('nni_node/main.js'):
sys.exit('A development build already exists. Please uninstall NNI and run "python3 setup.py clean --all".')
open('nni/version.py', 'w').write(f"__version__ = '{release}'")
Expand Down
39 changes: 37 additions & 2 deletions setup_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
node_version = 'v16.3.0'
yarn_version = 'v1.22.10'

def _get_jupyter_lab_version():
try:
import jupyterlab
return jupyterlab.__version__
except ImportError:
return '3.x'

jupyter_lab_major_version = _get_jupyter_lab_version().split('.')[0]

def build(release):
"""
Expand All @@ -40,11 +48,13 @@ def build(release):
if release or not os.environ.get('GLOBAL_TOOLCHAIN'):
download_toolchain()
prepare_nni_node()
update_package()
compile_ts(release)
if release or sys.platform == 'win32':
copy_nni_node(release)
else:
symlink_nni_node()
restore_package()

def clean(clean_all=False):
"""
Expand Down Expand Up @@ -120,6 +130,25 @@ def download_toolchain():
shutil.rmtree('toolchain/yarn', ignore_errors=True)
Path(f'toolchain/yarn-{yarn_version}').rename('toolchain/yarn')

def update_package():
if jupyter_lab_major_version == '2':
package_json = json.load(open('ts/jupyter_extension/package.json'))
json.dump(package_json, open('ts/jupyter_extension/.package_default.json', 'w'), indent=2)

package_json['scripts']['build'] = 'tsc && jupyter labextension link .'
package_json['dependencies']['@jupyterlab/application'] = '^2.3.0'
package_json['dependencies']['@jupyterlab/launcher'] = '^2.3.0'

package_json['jupyterlab']['outputDir'] = 'build'
json.dump(package_json, open('ts/jupyter_extension/package.json', 'w'), indent=2)
print(f'updated package.json with {json.dumps(package_json, indent=2)}')

def restore_package():
if jupyter_lab_major_version == '2':
package_json = json.load(open('ts/jupyter_extension/.package_default.json'))
print(f'stored package.json with {json.dumps(package_json, indent=2)}')
json.dump(package_json, open('ts/jupyter_extension/package.json', 'w'), indent=2)
os.remove('ts/jupyter_extension/.package_default.json')

def prepare_nni_node():
"""
Expand Down Expand Up @@ -177,7 +206,10 @@ def symlink_nni_node():

_symlink('ts/webui/build', 'nni_node/static')

if Path('ts/jupyter_extension/dist').exists():
if jupyter_lab_major_version == '2':
_symlink('ts/jupyter_extension/build', 'nni_node/jupyter-extension')
_symlink(os.path.join(sys.exec_prefix, 'share/jupyter/lab/extensions'), 'nni_node/jupyter-extension/extensions')
elif Path('ts/jupyter_extension/dist').exists():
_symlink('ts/jupyter_extension/dist', 'nni_node/jupyter-extension')


Expand Down Expand Up @@ -209,7 +241,10 @@ def copy_nni_node(version):

shutil.copytree('ts/webui/build', 'nni_node/static')

if version or Path('ts/jupyter_extension/dist').exists():
if jupyter_lab_major_version == '2':
shutil.copytree('ts/jupyter_extension/build', 'nni_node/jupyter-extension/build')
shutil.copytree(os.path.join(sys.exec_prefix, 'share/jupyter/lab/extensions'), 'nni_node/jupyter-extension/extensions')
elif version or Path('ts/jupyter_extension/dist').exists():
shutil.copytree('ts/jupyter_extension/dist', 'nni_node/jupyter-extension')


Expand Down

0 comments on commit 59f50ca

Please sign in to comment.