<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scienceoss.com &#187; documentation</title>
	<atom:link href="http://scienceoss.com/tags/documentation/feed/" rel="self" type="application/rss+xml" />
	<link>http://scienceoss.com</link>
	<description>useful tidbits for using open source software in science</description>
	<lastBuildDate>Wed, 26 May 2010 03:34:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Use Sphinx for documentation</title>
		<link>http://scienceoss.com/use-sphinx-for-documentation/</link>
		<comments>http://scienceoss.com/use-sphinx-for-documentation/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 02:45:20 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[utilities]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[sphinx]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://scienceoss.com/?p=117</guid>
		<description><![CDATA[Update: After some folks requested it in the comments, I wrote another post, A minimal Sphinx setup for autodocumenting Python modules. You might want to check this out if you&#8217;re specifically interested in automatically documenting your code with Sphinx. I&#8217;ve been doing quite a bit of code documentation lately, and I decided to try and [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Update:</strong> After some folks requested it in the comments, I wrote another post, <a href="http://scienceoss.com/minimal-sphinx-setup-for-autodocumenting-python-modules/">A minimal Sphinx setup for autodocumenting Python modules</a>.  You might want to check this out if you&#8217;re specifically interested in automatically documenting your code with Sphinx.</em></p>
<p>I&#8217;ve been doing quite a bit of code documentation lately, and I decided to try and figure out the best tool to use.  I found it.  It&#8217;s called <a href="http://sphinx.pocoo.org/">Sphinx</a>, and you can see what the documentation looks like by checking out the documentation for Python itself (v. <a href="http://docs.python.org/dev/">2.6</a> and <a href="http://docs.python.org/dev/3.0/">3.0</a>).<br />
Here&#8217;s how to get started using Sphinx. <span id="more-117"></span><br />
Sphinx is the name of the program that takes your plain text files and converts them into hyperlinked, nicely formatted HTML documents.  It knows what to link and how to format based on simple formatting commands in the text files (specifically, it uses the <a href="http://docutils.sourceforge.net/rst.html">ReStructured Text (ReST)</a> markup language).</p>
<h3>Installation</h3>
<p>It is really, really easy.</p>
<p>If you have easy_install on your machine, running</p>
<pre class="brush: plain; title: ; notranslate">
easy_install sphinx -U
</pre>
<p> from the command line will do it.</p>
<p>If you don&#8217;t have easy_install yet, <a href="http://peak.telecommunity.com/DevCenter/EasyInstall#installing-easy-install">download and install it</a>, then run <span class="c">easy_install sphinx</span> from the command line. </p>
<h3>Run <span class="c">sphinx-quickstart</span> to automatically setup a directory structure</h3>
<p>Navigate to the directory that you want to generate documentation for.  Run <span class="c">sphinx-quickstart</span> from the command line.  You will get a series of questions that lead you through the process of setting up the directories and some files that Sphinx needs.  Most questions have defaults that you can just accept no prob.  When it comes to version name, you can just make something up.</p>
<p>If you accepted all the defaults (you conformist, you!) there are now a couple of new files and directories:</p>
<pre class="brush: plain; title: ; notranslate">
New directory contents:
/.build
/.templates
/.static
index.rst
conf.py</pre>
<p>In a moment we&#8217;ll look at index.rst and conf.py.  But first . . .</p>
<h3>Create some content</h3>
<p>Time to make some content.  All content is created in plain text files.  Create a new text file in the same directory as <span class="c">index.rst</span> and <span class="c">conf.py</span>.  Call it <span class="c">chapter1.rst</span> or something . . . what&#8217;s important though is that you give it the &#8220;.rst&#8221; extension (if you chose the default option for &#8220;Source file suffix&#8221; when you ran <span class="c">sphinx-build</span>, otherwise the extension must be whatever you chose in that step). Sphinx will recognize files that should be included based on their extension.  Since I tend to have random, non-documentation .txt files laying around which Sphinx will ask me about, I prefer to use .rst instead of .txt.</p>
<p>Inside <span class="c">chapter1.rst</span>, type some stuff.  You could be documenting the code that&#8217;s in the current directory, for example.  Here are some formatting elements you can try for now.  The Sphinx site has <a href="http://sphinx.pocoo.org/rest.html">lots more info on formatting</a>.</p>
<pre class="brush: plain; title: ; notranslate">
This is a header
================
Some text, *italic text*, **bold text**

* bulleted list.  There needs to be a space right after the &quot;*&quot;
* item 2

.. note::
    This is a note.

Here's some Python code:

&gt;&gt;&gt; for i in range(10):
...     print i
</pre>
<p>Once Sphinx is set up, it will read in the plain text above and convert it into some nicely formatted HTML.  The results of the above plain text will look something like this:<br />
<a href='http://scienceoss.com/wp-content/uploads/2008/04/documentation-preview.png'><img src="http://scienceoss.com/wp-content/uploads/2008/04/documentation-preview-300x161.png" alt="" title="documentation preview" width="300" height="161" class="aligncenter size-medium wp-image-120" /></a></p>
<p>But first you have to run it through Sphinx in order for it to look that way, and before you can do that you have to tell Sphinx to include this document in its processing.</p>
<h3>Tell <span class="c">index.rst</span> what files to include</h3>
<p>Now we have to edit <span class="c">index.rst </span>to tell Sphinx that chapter1.rst should be included in the documentation.  By default, <span class="c">index.rst</span> looks like this:</p>
<h4>Change this . . .</h4>
<pre class="brush: plain; title: ; notranslate">
Welcome to my project's documentation!
======================================

Contents:

.. toctree::
   :maxdepth: 2

Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
</pre>
<h4>To this . . .</h4>
<p>(Just add the <span class="c">chapter1.rst</span> line)</p>
<pre class="brush: plain; title: ; notranslate">
Welcome to my project's documentation!
======================================

Contents:

.. toctree::
    :maxdepth: 2

    chapter1.rst

Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
</pre>
<p>The indentation of the chapter1.rst line is important.  So is the blank line above, and the blank line below.</p>
<p>BE CAREFUL . . . this burned me the first time, and took a while to figure out.  See the :maxdepth: 2 line?  <strong>It&#8217;s only indented 3 spaces.</strong>  It doesn&#8217;t have to be 3 spaces, it just happens to be in this auto-generated file.  If you have your text editor set for 4 spaces, or anything other than 3 spaces, when you hit tab on another line your indentation will be inconsistent, resulting in errors.</p>
<p>Usually I just add a space to make the :maxdepth: 2 line a 4-space indentation.</p>
<h3>Build the documentation</h3>
<p>Back at the command line, make a new directory to hold your documentation.  I&#8217;m going to call mine <span class="c">doc</span>.</p>
<pre>mkdir doc</pre>
<p>Then, in the same directory as <span class="c">index.rst</span>, run</p>
<pre>sphinx-build . doc</pre>
<p>You&#8217;ll get some output that tells you what it&#8217;s doing.  </p>
<h3>View the result</h3>
<p>Now you can open up doc/index.html to view your newly generated documentation.  It looks something like this:<br />
<a href='http://scienceoss.com/wp-content/uploads/2008/04/documentation0.png'><img src="http://scienceoss.com/wp-content/uploads/2008/04/documentation0-300x132.png" alt="" title="first look at documentation" width="300" height="132" class="aligncenter size-medium wp-image-118" /></a></p>
<p>Clicking on the &#8220;This is a header&#8221; link shows you the content you added in chapter1.rst, and it looks something like this:<br />
<a href='http://scienceoss.com/wp-content/uploads/2008/04/documentation1.png'><img src="http://scienceoss.com/wp-content/uploads/2008/04/documentation1-300x157.png" alt="" title="first look at documentation, next page" width="300" height="157" class="aligncenter size-medium wp-image-119" /></a></p>
<h3>So what&#8217;s the big deal?</h3>
<p>The above example could have been done with a little work in Microsoft Word.  Where Sphinx really shines, though, is when you use it to document Python code.</p>
<p>Sphinx can auto-document by reading in a module, then displaying the docstrings of objects in that source code.  It hyperlinks modules, classes, methods, attributes, etc.  It can even take the code you write as a tutorial and use it as doctests, which test your code to ensure that it is correct.  With a little more setup, you can generate LaTeX (and then PDF files) of your code, along with the good-looking syntax highlighting.  (see upcoming posts for how to do all of this)</p>
<p>I feel that other documentation tools, like <a href="http://epydoc.sourceforge.net/">epydoc</a>, have too much of an auto-generated look, and it&#8217;s too easy to include extraneous content.  In order to include tutorial info in epydoc documents, you have to add text to the beginning of a module.  While epydoc has some nice formatting (allowing you to tag text as parameters or return values), it tends to look ugly&#8211;sometimes to the point of unreadability.</p>
<p>I like Sphinx because it allows you to insert auto-generated documentation when you need it but tends to focus on high-quality, handwritten, tutorial-style content which I think is the most effective kind of documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/use-sphinx-for-documentation/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

