-
-
Notifications
You must be signed in to change notification settings - Fork 395
/
command.py
executable file
·100 lines (77 loc) · 3.04 KB
/
command.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
97
98
99
100
#! /usr/bin/env python
"""
python -m holoviews.util.command Conversion_Example.ipynb
OR
holoviews Conversion_Example.ipynb
"""
import sys
import os
import argparse
from argparse import RawTextHelpFormatter
try:
import nbformat
import nbconvert
except ImportError:
print('nbformat, nbconvert and ipython need to be installed to use the holoviews command')
sys.exit()
try:
from ..ipython.preprocessors import OptsMagicProcessor, OutputMagicProcessor
from ..ipython.preprocessors import StripMagicsProcessor
except ImportError:
from holoviews.ipython.preprocessors import OptsMagicProcessor, OutputMagicProcessor
from holoviews.ipython.preprocessors import StripMagicsProcessor
from . import examples
def main():
if len(sys.argv) < 2:
print("For help with the holoviews command run:\n\nholoviews --help\n")
sys.exit()
parser = argparse.ArgumentParser(prog='holoviews',
formatter_class=RawTextHelpFormatter,
description=description,
epilog=epilog)
parser.add_argument('--notebook', metavar='notebook', type=str, nargs=1,
help='The Jupyter notebook to convert to Python syntax.')
parser.add_argument('--install-examples', metavar='install_examples',
type=str, nargs='?',
help='Install examples to the specified directory.')
args = parser.parse_args()
if args.notebook:
print(export_to_python(args.notebook[0]), file=sys.stdout)
else:
if args.install_examples is None:
examples_dir = 'holoviews-examples'
else:
examples_dir = args.install_examples
curdir,_ = os.path.split(__file__)
root = os.path.abspath(os.path.join('..','..', curdir))
examples(path=examples_dir, root=root)
def export_to_python(filename=None,
preprocessors=[OptsMagicProcessor(),
OutputMagicProcessor(),
StripMagicsProcessor()]):
filename = filename if filename else sys.argv[1]
with open(filename) as f:
nb = nbformat.read(f, nbformat.NO_CONVERT)
exporter = nbconvert.PythonExporter()
for preprocessor in preprocessors:
exporter.register_preprocessor(preprocessor)
source, meta = exporter.from_notebook_node(nb)
return source
description = """
Command line interface for holoviews.
This utility allows conversion of notebooks containing the HoloViews
%opts, %%opts, %output and %%output magics to regular Python
syntax. This is useful for turning Jupyter notebooks using HoloViews
into Bokeh applications that can be served with:
bokeh server --show converted_notebook.py
The holoviews command supports the following options:
"""
epilog="""
Example usage
-------------
$ holoviews ./examples/demos/matplotlib/area_chart.ipynb
The converted syntax is then output to standard output where you can
direct it to a Python file of your choosing.
"""
if __name__ == '__main__':
main()