forked from holoviz/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
96 lines (79 loc) · 2.96 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Commandline interface to Panel
"""
from __future__ import absolute_import, division, unicode_literals
import sys
import argparse
from bokeh.__main__ import main as bokeh_entry_point
from bokeh.command.util import die
from bokeh.util.string import nice_join
from . import __version__
from .io.server import INDEX_HTML
def transform_cmds(argv):
"""
Allows usage with anaconda-project by remapping the argv list provided
into arguments accepted by Bokeh 0.12.7 or later.
"""
replacements = {'--anaconda-project-host':'--allow-websocket-origin',
'--anaconda-project-port': '--port',
'--anaconda-project-address': '--address'
}
transformed = []
skip = False
for arg in argv:
if skip:
skip = False
continue
if arg in replacements.keys():
transformed.append(replacements[arg])
elif arg == '--anaconda-project-iframe-hosts':
skip = True
continue
elif arg.startswith('--anaconda-project'):
continue
else:
transformed.append(arg)
return transformed
def main(args=None):
"""Merges commands offered by pyct and bokeh and provides help for both"""
from bokeh.command.subcommands import all as bokeh_commands
try:
import pyct.cmd
pyct_commands = ['copy-examples', 'examples']
except Exception:
pass
parser = argparse.ArgumentParser(
prog="panel", epilog="See '<command> --help' to read about a specific subcommand."
)
parser.add_argument('-v', '--version', action='version', version=__version__)
subs = parser.add_subparsers(help="Sub-commands")
for cmd in pyct_commands:
cmd = cmd.replace('-', '_')
fn = getattr(pyct.cmd, cmd)
subs.add_parser(cmd, help=fn.__doc__)
for cls in bokeh_commands:
subs.add_parser(cls.name, help=cls.help)
if len(sys.argv) == 1:
all_commands = sorted([c.name for c in bokeh_commands] + pyct_commands)
die("ERROR: Must specify subcommand, one of: %s" % nice_join(all_commands))
if sys.argv[1] in ('--help', '-h'):
args = parser.parse_args(sys.argv[1:])
args.invoke(args)
sys.exit()
if len(sys.argv) > 1 and any(sys.argv[1] == c.name for c in bokeh_commands):
if sys.argv[1] == 'serve' and not any(arg.startswith('--index') for arg in sys.argv):
sys.argv = sys.argv + ['--index=%s' % INDEX_HTML]
sys.argv = transform_cmds(sys.argv)
bokeh_entry_point()
elif sys.argv[1] in pyct_commands:
try:
import pyct.cmd
except ImportError:
print("install pyct to enable this command (e.g. `conda install -c pyviz pyct` or `pip install pyct[cmd]`)")
sys.exit(1)
pyct.cmd.substitute_main('panel', cmds=pyct_commands, args=args)
else:
parser.parse_args(sys.argv[1:])
sys.exit(1)
if __name__ == "__main__":
main()