Skip to content
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

#6697 beakerx commands refactor #6727

Merged
merged 3 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ conda create -y -n beakerx 'python>=3' nodejs pandas openjdk maven
source activate beakerx
conda install -y -c conda-forge ipywidgets
(cd beakerx; pip install -e . --verbose)
beakerx-install
beakerx install
```

### Build and Install for Lab
Expand All @@ -67,7 +67,7 @@ conda create -y -n labx 'python>=3' nodejs pandas openjdk maven pytest
source activate labx
conda install -y -c conda-forge jupyterlab
(cd beakerx; pip install -e . --verbose)
beakerx-install
beakerx install
jupyter labextension install @jupyter-widgets/jupyterlab-manager
(cd js/lab; jupyter labextension install .)
```
Expand Down
10 changes: 8 additions & 2 deletions beakerx/beakerx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from ._version import version_info, __version__
from .handlers import load_jupyter_server_extension
from .environment import *
import json
from .commands import parse

def _jupyter_nbextension_paths():
return [{
Expand All @@ -40,5 +40,11 @@ def _jupyter_nbextension_paths():
def _jupyter_server_extension_paths():
return [dict(module="beakerx")]


beakerx = BeakerX()

def run():
try:
parse()
except KeyboardInterrupt:
return 130
return 0
15 changes: 7 additions & 8 deletions beakerx/beakerx/bkr2ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,15 @@ def convertNotebook(notebook):
nb = parseBkr(data)
nbformat.write(nb, notebook.partition('.')[0] + '.ipynb')

def main():
def main(args):
for notebook in args.notebooks:
convertNotebook(notebook)

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('notebooks', nargs='+',
help="beaker notebooks to be converted. Enter *.bkr in case you want to convert all notebooks at once.")
help="beaker notebooks to be converted. Enter *.bkr in case you want to convert all notebooks at once.")
if len(sys.argv) == 1:
parser.print_help()
args = parser.parse_args()

for notebook in args.notebooks:
convertNotebook(notebook)

if __name__ == "__main__":
main()
main(args)
44 changes: 44 additions & 0 deletions beakerx/beakerx/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import argparse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

banner

import sys
import subprocess
import beakerx
from .install import install, uninstall
from .bkr2ipynb import main

def install_subparser(subparser):
install_parser = subparser.add_parser('install', help='installs beakerx extension')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

help='installs BeakerX extensions'

install_parser.set_defaults(func = install)
install_parser.add_argument("--prefix",
help="location of the environment to install into",
default=sys.prefix)
return subparser

def uninstall_subparser(subparser):
uninstall_parser = subparser.add_parser('uninstall', help='uninstalls beakerx extension')
uninstall_parser.set_defaults(func=lambda x : uninstall())
return subparser

def bkr2ipynb_subparser(subparser):
bkr2ipynb_parser = subparser.add_parser('bkr2ipynb', help='converts beaker notebooks to ipynb format')
bkr2ipynb_parser.set_defaults(func=main)
bkr2ipynb_parser.add_argument('notebooks', nargs='+',
help="beaker notebooks to be converted. Enter *.bkr in case you want to convert all notebooks at once.")
return subparser

def init_parser():
version = 'beakerx %s' % (beakerx.__version__)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print just the version do not include the name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional context, the Python community standardized the output for versions in PEP 440.

Here is what Django does, and notice they reference PEP 440: https://docs.djangoproject.com/en/2.0/ref/django-admin/#determining-the-version

I see that my example from MarkUpUpDownDown puts the app name in the version string, so that's my bad for misleading you there. Sorry, @lmitusinski.


parser = argparse.ArgumentParser()
parser.add_argument('--version', action='version', version=version)
parser.set_defaults(func=lambda x : subprocess.check_call(["jupyter", "notebook"]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you do it this way, then there are 2 problems:

  1. arguments are not passed,
  2. control-c is not passed to the underlying process, so any kernels the user started are leaked.
    maybe instead "import jupyter" and call it as a function?


subparsers = parser.add_subparsers()
install_subparser(subparsers)
uninstall_subparser(subparsers)
bkr2ipynb_subparser(subparsers)
return parser

def parse():
parser = init_parser()
args = parser.parse_args()
args.func(args)
16 changes: 5 additions & 11 deletions beakerx/beakerx/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,11 @@ def _install_beakerx(args):
_set_conf_privileges()


def install():
try:
parser = make_parser()
args = parser.parse_args()
if args.disable:
_disable_beakerx()
else:
_install_beakerx(args)
except KeyboardInterrupt:
return 130
return 0
def install(args):
_install_beakerx(args)

def uninstall():
_disable_beakerx()


if __name__ == "__main__":
Expand Down
3 changes: 1 addition & 2 deletions beakerx/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@
],
entry_points={
'console_scripts': [
'beakerx-install = beakerx.install:install',
'bkr2ipynb = beakerx.bkr2ipynb:main',
'beakerx = beakerx:run'
]
},
package_data={
Expand Down
2 changes: 1 addition & 1 deletion docker/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

source activate beakerx
(cd beakerx; pip install -e . --verbose)
beakerx-install
beakerx install
jupyter labextension install @jupyter-widgets/jupyterlab-manager
(cd js/lab; jupyter labextension install .)

Expand Down