PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/README.md

https://gitlab.com/Fair_lab/FNLSphinx
Markdown | 284 lines | 217 code | 67 blank | 0 comment | 0 complexity | 245a417fac379e16107b492fc2afa45f MD5 | raw file
  1. # FNL Sphinx Documentation Tutorial/Demo
  2. ### Goals for today:
  3. 1. What's this Sphinx thing anyway?
  4. 2. Demo: Sphinx setup for a simple project on the AIRC
  5. 3. Demo: deploy Sphinx documentation on GitLab pages
  6. 4. Showcase: hcponeclick documentation
  7. ### Format:
  8. - I'll demo, but interrupt at any time if you have questions
  9. - If time/interest: informal discussion at the end about documentation standards
  10. #### If you want to follow along:
  11. - You can follow along on your laptop, but we might not have time to troubleshoot during this session.
  12. - You'll have access to this whole tutorial and can repeat anything in this tutorial at a later time if you want:
  13. - https://gitlab.com/Fair_lab/FNLSphinx
  14. - You can see what your code should look like after the tutorial is complete by checking out the pagesExample branch: https://gitlab.com/Fair_lab/FNLSphinx/tree/pagesExample
  15. - The tutorial expects you to have terminal access to the AIRC, but you can do these things on other systems
  16. - The tutorial's commands are geared towards mac/linux users.
  17. - You can try using [git-bash](https://git-scm.com/download/win) on windows
  18. - I'm happy to help people one-on-one later if they run into issues/need help with configuration for their system :-).
  19. Just shoot me an email at waltonwe@ohsu.edu
  20. ### Caveat: I'm not a Sphinx expert
  21. ![not an expert](http://memesvault.com/wp-content/uploads/I-Have-No-Idea-What-Im-Doing-Dog-02.jpg)
  22. -----------------------------------------------------------------------------------------
  23. ## What's this Sphinx thing anyway?
  24. 1. Documentation tool for Python, written in Python.
  25. - http://www.sphinx-doc.org/en/stable/
  26. - sometimes used to document non-Python [things](http://www.sphinx-doc.org/en/stable/examples.html#homepages-and-other-non-documentation-sites)
  27. 2. Generates html, LaTeX, PDF...
  28. 3. Can turn your in-code function docblocs into formatted html
  29. 4. Uses a markdown-like code called reStructuredText
  30. ### Why do *we* care?
  31. 1. repeat #3
  32. 2. Documentation is arguably more readable and definitely more automated than our current wiki setup
  33. 3. Deploy docs to public GitLab pages for each project. Free hosting!
  34. ### Things to consider before jumping on the Sphinx train
  35. 1. Sphinx doesn't do a great job at pulling code documentation from other languages
  36. - has support for C/C++, but AFAIK that's it
  37. - there are usually other packages that help with that: Javadoc, PHPdoc
  38. 2. [insert the usual debates about code documentation here]
  39. - Let's save this debate for the end
  40. -----------------------------------------------------------------------------------------
  41. ## Sphinx setup
  42. ### virtualenv
  43. If you're fortunate enough to have sufficient privileges on your system to install Python packages and don't care about the AIRC, skip to [Upgrade Sphinx and install packages](#upgrade-sphinx-and-install-packages)
  44. ![virtualenvs are scary](http://i.giphy.com/Zt2f45vHLZtaU.gif)
  45. Ok, this is just *one* way to get Sphinx set up. With our current situation on the AIRC, using virtualenv is easiest way to get started. I've been able to install and run Sphinx on my linux box at home (I have sudo privileges) and on my Windows machine at work (after updating pip). I found out the hard way that Python libraries I sometimes use aren't available on Windows(fcntl, pwd), so I think it's best to run Sphinx on whatever OS you're writing your code for.
  46. **It's really not *that* bad.**
  47. Just execute the following on the AIRC command prompt:
  48. ```
  49. #go to your home directory.
  50. cd ~
  51. mkdir Sphinx
  52. #init your virtualenv
  53. virtualenv --system-site-packages Sphinx
  54. #start using your virtualenv
  55. source ~/Sphinx/bin/activate
  56. ```
  57. Now your command prompt should look like:
  58. ```
  59. (Sphinx)you@beast:~$
  60. ```
  61. You can activate and deactivate this virtualenv whenever you want (i.e. if you need to
  62. get back to the standard beast Python configuration):
  63. ```
  64. # stop using your Sphinx virtualenv:
  65. deactivate
  66. # start using your Sphinx virtualenv just like before:
  67. source ~/Sphinx/bin/activate
  68. ```
  69. More on [virtualenvs](http://docs.python-guide.org/en/latest/dev/virtualenvs/)
  70. ### Upgrade Sphinx and install packages
  71. The AIRC has Sphinx, but it's a crusty old version. We're going to install a new version and
  72. a few friends, so execute the following on the AIRC command line:
  73. ```
  74. pip install --upgrade sphinx
  75. pip install sphinxcontrib-napoleon
  76. pip install sphinx-argparse
  77. ```
  78. If you're on a system that doesn't have sphinx, run ```pip install sphinx``` instead of ```pip install --upgrade sphinx```
  79. This takes some time, so while that's going on, let's look at our example script: count_processed_subjects.py.
  80. ### Initialize project documentation
  81. **If you're using FNLSphinx as a sandbox, at this point you'll want to fork your own copy via [GitLab](https://gitlab.com/Fair_lab/FNLSphinx) and
  82. clone it wherever you work on projects.**
  83. Sphinx has a handy tool called ```sphinx-quickstart``` you can use to help you get your
  84. documentation set up
  85. 1. cd into your project directory(mine's ~/repos/FNLSphinx), and checkout a new branch:
  86. ```
  87. cd ~/repos/FNLSphinx
  88. git checkout -b pages
  89. ```
  90. 2. run sphinx-quickstart:
  91. ```
  92. sphinx-quickstart
  93. ```
  94. - **READ THE SCRIPT'S OUTPUT** so you know what's going on
  95. - Most of the defaults are fine, but make sure to enter the following:
  96. - ```> Root path for the documentation [.]:``` docs
  97. - ```> autodoc: automatically insert docstrings from modules (y/n) [n]:``` y
  98. - There are more things you can play with here, but this is all you need
  99. 3. Inspect the results! You should see a new ```docs``` directory in your project
  100. ### Update your Sphinx configuration file
  101. Open ```docs/config.py``` for editing:
  102. 1. Add your project directory to the path
  103. - Add to the top of the file:
  104. ```
  105. import sys, os
  106. sys.path.insert(0,os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
  107. ```
  108. - ![NO](http://i.giphy.com/vDurI6FYH7qi4.gif)
  109. Generally, having to manually update your Python path is a
  110. sign that you're doing something wrong--but this is an exception.
  111. 2. Add the packages we downloaded to the ```extensions``` list:
  112. ```
  113. extensions = [
  114. 'sphinx.ext.autodoc',
  115. 'sphinx.ext.napoleon',
  116. 'sphinxarg.ext'
  117. ]
  118. ```
  119. 3. (Optional) Update the html_theme:
  120. - Latest Sphinx defaults to the 'alabaster' theme
  121. - I set mine to 'default'
  122. - Other built-in themes: http://www.sphinx-doc.org/en/stable/theming.html#builtin-themes
  123. - Make your own theme: http://www.sphinx-doc.org/en/stable/theming.html#creating-themes
  124. ### Build and inspect your bare-bones docs
  125. ```
  126. cd docs
  127. make html
  128. ```
  129. open ```docs/_build/html/index.html``` using the browser of your choice
  130. ```
  131. firefox _build/html/index.html
  132. ```
  133. ### Your first reStructuredText file
  134. open docs/index.rst
  135. pull in your function and argparse documentation by adding this code:
  136. ```
  137. How to run count_processed_subjects.py:
  138. ---------------------------------------
  139. .. argparse::
  140. :module: count_processed_subjects
  141. :func: get_parser
  142. :prog: ./count_processed_subjects.py
  143. .. toctree::
  144. :maxdepth: 2
  145. Functions:
  146. ----------
  147. .. automodule:: count_processed_subjects
  148. :members:
  149. ```
  150. This is where the napoleon, autodoc, and argparse extension come in.
  151. - autodoc: pulls code documentation into reStructuredText
  152. - napolean: lets you use google style docstrings. These are [much more readable](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/) than
  153. the default Sphinx docStrings
  154. - argparse: Generates documentation from your argparse code
  155. ### Build and inspect your docs again
  156. Same as before:
  157. ```
  158. cd docs
  159. make html
  160. firefox _build/html/index.html
  161. ```
  162. ![Yayyy!!!](http://i.giphy.com/LZElUsjl1Bu6c.gif)
  163. -----------------------------------------------------------------------------------------
  164. ## Deploy Docs to GitLab
  165. GitLab gives you a free web page to go along with your GitLab repo on 'gitlab.io'.
  166. GitLab also has it's own CI(Continuous Integration) server you can leverage to do things like automate
  167. builds or trigger automated tests.
  168. #### You need to dip your toes into the CI pool to get your Sphinx docs deployed to gitlab.io.
  169. ![NOPE](http://i.giphy.com/6h4z4b3v6XWxO.gif)
  170. **I promise it's not scary! You only have to do the setup ONE TIME:**
  171. 1. In your the root of your project directory, create a file called ```.gitlab-ci.yml```
  172. 2. Add the following snippet to that file (and save):
  173. ```
  174. pages:
  175. script:
  176. - rm -rf public
  177. - mv docs/_build/html public
  178. artifacts:
  179. paths:
  180. - public
  181. only:
  182. - pages
  183. ```
  184. 3. Commit everything to your pages branch, and push your branch to GitLab:
  185. ```
  186. cd ~/repos/FNLSphinx
  187. git add -A
  188. git add -A docs
  189. git commit -m 'built and deployed first documentation page'
  190. git push origin pages:pages
  191. ```
  192. 4. Open the project in GitLab and look in the 'Builds' tab. For me, it's https://gitlab.com/alexwweston/FNLSphinx/builds.
  193. - click on the 'BuildID' if you're curious about what gitlab-ci did/is doing
  194. 5. Go to http://group_or_uname.gitlab.io/your_project/. For me, it's http://alexwweston.gitlab.io/FNLSphinx/
  195. ![WOOOOOO!](http://i.giphy.com/4T48716LEWUGA.gif)
  196. Every time you push changes to the pages branch, your latest documentation will be automatically deployed!
  197. -----------------------------------------------------------------------------------------
  198. ## Tour of hcponeclick docs
  199. If there's time, let's do a quick tour what I was able to do for the hcponeclick docs.
  200. - docs: http://fair_lab.gitlab.io/hcponeclick/
  201. - code: https://gitlab.com/Fair_lab/hcponeclick/tree/pages/docs
  202. -----------------------------------------------------------------------------------------
  203. ## Resources:
  204. Sphinx:
  205. - Sphinx homepage: http://www.sphinx-doc.org/en/stable/index.html
  206. - Sphinx getting started: http://www.sphinx-doc.org/en/stable/tutorial.html
  207. - also explains reStructuredText directives (things like ```..toctree::```)
  208. - intro to reStructuredText: http://www.sphinx-doc.org/en/stable/rest.html
  209. - napolean: https://sphinxcontrib-napoleon.readthedocs.io/en/latest/
  210. - autodoc: http://www.sphinx-doc.org/en/stable/ext/autodoc.html
  211. - http://sphinx-argparse.readthedocs.io/en/latest/
  212. - Sphinx full code example: https://pythonhosted.org/an_example_pypi_project/sphinx.html#full-code-example
  213. GitLab:
  214. - pages: http://doc.gitlab.com/ee/pages/README.html
  215. - GitLab CI: https://about.gitlab.com/gitlab-ci/
  216. Misc:
  217. - How to use quick documentation to bring up docblocs in PyCharm: https://www.jetbrains.com/help/pycharm/2016.1/documentation-tool-window.html?origin=old_help