Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 4c18e19

Browse files
authored
Generate reST documentation for magic commands (#113)
Auto generate docs for any added magics by searching through the source files for lines with register_line_cell_magic, capturing the names for those magics, and calling them inside an ipython kernel with the -h argument, then storing that output into a generated datalab.magics.rst file.
1 parent 7a3399b commit 4c18e19

File tree

7 files changed

+76
-32
lines changed

7 files changed

+76
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ MANIFEST
77
build
88
.coverage
99
dist
10+
datalab.magics.rst

docs/Makefile

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,42 @@ help:
5555
clean:
5656
rm -rf $(BUILDDIR)/*
5757

58-
html:
58+
pre-build:
59+
@echo "Generate reST for magic commands:"
60+
ipython gen-magic-rst.ipy
61+
62+
html: pre-build
5963
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
6064
@echo
6165
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
6266

63-
dirhtml:
67+
dirhtml: pre-build
6468
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
6569
@echo
6670
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
6771

68-
singlehtml:
72+
singlehtml: pre-build
6973
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
7074
@echo
7175
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
7276

73-
pickle:
77+
pickle: pre-build
7478
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
7579
@echo
7680
@echo "Build finished; now you can process the pickle files."
7781

78-
json:
82+
json: pre-build
7983
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
8084
@echo
8185
@echo "Build finished; now you can process the JSON files."
8286

83-
htmlhelp:
87+
htmlhelp: pre-build
8488
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
8589
@echo
8690
@echo "Build finished; now you can run HTML Help Workshop with the" \
8791
".hhp project file in $(BUILDDIR)/htmlhelp."
8892

89-
qthelp:
93+
qthelp: pre-build
9094
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
9195
@echo
9296
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
@@ -95,15 +99,15 @@ qthelp:
9599
@echo "To view the help file:"
96100
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/api.qhc"
97101

98-
applehelp:
102+
applehelp: pre-build
99103
$(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp
100104
@echo
101105
@echo "Build finished. The help book is in $(BUILDDIR)/applehelp."
102106
@echo "N.B. You won't be able to view it unless you put it in" \
103107
"~/Library/Documentation/Help or install it in your application" \
104108
"bundle."
105109

106-
devhelp:
110+
devhelp: pre-build
107111
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
108112
@echo
109113
@echo "Build finished."
@@ -112,85 +116,85 @@ devhelp:
112116
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/api"
113117
@echo "# devhelp"
114118

115-
epub:
119+
epub: pre-build
116120
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
117121
@echo
118122
@echo "Build finished. The epub file is in $(BUILDDIR)/epub."
119123

120-
latex:
124+
latex: pre-build
121125
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
122126
@echo
123127
@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
124128
@echo "Run \`make' in that directory to run these through (pdf)latex" \
125129
"(use \`make latexpdf' here to do that automatically)."
126130

127-
latexpdf:
131+
latexpdf: pre-build
128132
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
129133
@echo "Running LaTeX files through pdflatex..."
130134
$(MAKE) -C $(BUILDDIR)/latex all-pdf
131135
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
132136

133-
latexpdfja:
137+
latexpdfja: pre-build
134138
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
135139
@echo "Running LaTeX files through platex and dvipdfmx..."
136140
$(MAKE) -C $(BUILDDIR)/latex all-pdf-ja
137141
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
138142

139-
text:
143+
text: pre-build
140144
$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
141145
@echo
142146
@echo "Build finished. The text files are in $(BUILDDIR)/text."
143147

144-
man:
148+
man: pre-build
145149
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
146150
@echo
147151
@echo "Build finished. The manual pages are in $(BUILDDIR)/man."
148152

149-
texinfo:
153+
texinfo: pre-build
150154
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
151155
@echo
152156
@echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
153157
@echo "Run \`make' in that directory to run these through makeinfo" \
154158
"(use \`make info' here to do that automatically)."
155159

156-
info:
160+
info: pre-build
157161
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
158162
@echo "Running Texinfo files through makeinfo..."
159163
make -C $(BUILDDIR)/texinfo info
160164
@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
161165

162-
gettext:
166+
gettext: pre-build
163167
$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
164168
@echo
165169
@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
166170

167-
changes:
171+
changes: pre-build
168172
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
169173
@echo
170174
@echo "The overview file is in $(BUILDDIR)/changes."
171175

172-
linkcheck:
176+
linkcheck: pre-build
173177
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
174178
@echo
175179
@echo "Link check complete; look for any errors in the above output " \
176180
"or in $(BUILDDIR)/linkcheck/output.txt."
177181

178-
doctest:
182+
doctest: pre-build
179183
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
180184
@echo "Testing of doctests in the sources finished, look at the " \
181185
"results in $(BUILDDIR)/doctest/output.txt."
182186

183-
coverage:
187+
coverage: pre-build
184188
$(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage
185189
@echo "Testing of coverage in the sources finished, look at the " \
186190
"results in $(BUILDDIR)/coverage/python.txt."
187191

188-
xml:
192+
xml: pre-build
189193
$(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml
190194
@echo
191195
@echo "Build finished. The XML files are in $(BUILDDIR)/xml."
192196

193-
pseudoxml:
197+
pseudoxml: pre-build
194198
$(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml
195199
@echo
196200
@echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml."
@@ -202,7 +206,7 @@ prepublish:
202206
cd ../../datalab-docs && git clone https://github.com/GoogleCloudPlatform/datalab.git html && \
203207
git checkout gh-pages
204208

205-
publish:
209+
publish: pre-build
206210
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
207211
cd ../../datalab-docs/html && git add . && git commit -m "Updated" && git push --force origin gh-pages
208212

docs/README

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
To use, install the prerequisites:
1+
To use, install the prerequisites and the pydatalab module:
22

33
pip install sphinx sphinx_rtd_theme sphinxcontrib-napoleon
4+
pip install .. # from docs directory
45

6+
then in the docs directory, do 'make html' (or epub, or text, etc).
57

6-
then in the docs directory, do 'make html' (or epub, or pdf, etc).
7-
8-
Output will be in the docs/_build directory.
9-
8+
Output will be in $BUILDDIR, defaulting to ../../datalab-docs.

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
# Add any paths that contain custom static files (such as style sheets) here,
146146
# relative to this directory. They are copied after the builtin static files,
147147
# so a file named "default.css" will overwrite the builtin "default.css".
148-
html_static_path = ['_static']
148+
#html_static_path = []
149149

150150
# Add any extra paths that contain custom files (such as robots.txt or
151151
# .htaccess) here, relative to this directory. These files are copied

docs/gen-magic-rst.ipy

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import subprocess, pkgutil, importlib, sys
2+
from cStringIO import StringIO
3+
4+
# ignore mlalpha and tensorboard for now because of their tensorflow dependency
5+
# until tensorboard is pip installable and can be listed as a pydatalab dependency
6+
IGNORED_MAGICS = ['mlalpha', 'tensorboard']
7+
8+
# import submodules
9+
submodules = [s for _,s,_ in pkgutil.iter_modules(['../datalab'])]
10+
11+
for m in submodules:
12+
name = 'datalab.' + m + '.commands'
13+
try:
14+
importlib.import_module(name)
15+
except:
16+
sys.stderr.write('WARNING, could not find module ' + name + '. Ignoring..\n')
17+
18+
magic_regex = "find ../datalab -name '*.py' -exec perl -e '$f=join(\"\",<>); print \"$1\n\" if $f=~/register_line_cell_magic\ndef ([^\(]+)/m' {} \;"
19+
magics = subprocess.check_output(magic_regex, shell=True)
20+
21+
reSTfile = open('datalab.magics.rst', 'w')
22+
indent = '\n '
23+
24+
reSTfile.write('datalab.magics\n')
25+
reSTfile.write('=================\n\n')
26+
27+
for m in magics.split():
28+
if m in IGNORED_MAGICS:
29+
sys.stderr.write('Ignoring magic ' + m + '\n')
30+
else:
31+
reSTfile.write('.. attribute:: ' + m + '\n')
32+
reSTfile.write('.. parsed-literal::\n')
33+
# hijack stdout since the ipython kernel call writes to stdout/err directly
34+
# and does not return its output
35+
tmpStdout, sys.stdout = sys.stdout, StringIO()
36+
get_ipython().magic(m + ' -h')
37+
resultout = sys.stdout.getvalue().splitlines()
38+
sys.stdout = tmpStdout
39+
reSTfile.writelines(indent + indent.join(resultout) + '\n\n')

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Contents:
1616
datalab.data
1717
datalab.stackdriver.monitoring
1818
datalab.storage
19+
datalab.magics
1920

2021

2122
Indices and tables

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
'pytz>=2015.4',
8080
'pyyaml==3.11',
8181
'requests==2.9.1',
82-
'scikit-learn==0.17.1',
8382
'scipy==0.18.0',
83+
'scikit-learn==0.17.1',
8484
'ipykernel==4.4.1',
8585
],
8686
package_data={

0 commit comments

Comments
 (0)