Loading video player…

Understanding the Basics of Sphinx

00:00 In the previous lesson, I gave an overview of the course. In this lesson, I’ll be covering the basics of Sphinx, what you need to know to build your first document.

00:10 Sphinx is a document generation tool. It supports multiple formats including HTML, PDF, man pages, and more. Within a Sphinx document, you can make references to other parts of the document. In HTML output, these end up as links. When combined with the autodoc extension, this allows you to write text about code and link to extracted pydoc comments incorporated into your documents.

00:36 One of my favorite uses of this is to document my unit tests. Management often wants to know what is being tested, especially if they’re coming from the old school, everything-has-a-test-case world.

00:47 By including some pydoc in your tests, you can extract it all and produce a PDF highlighting the current test suite without having to maintain a separate testing doc.

00:57 Sphinx uses the structure of your text to automatically create a table of contents and an index for your doc, and you can change the look and feel by specifying a theme.

01:08 By default, Sphinx uses reStructuredText—that’s RST for short. This is a text markup language. If you’re familiar with Markdown, it’s conceptually similar.

01:19 This course doesn’t cover it, but there are also extensions that allow the use of MyST Markdown if you really want to use a Markdown flavor instead. Sphinx is available as a PyPI package and you install it using the usual pip command.

01:35 I believe this is the part in the script where I always recommend virtual environments for your projects. There, you’ve been recommended. Once you’ve got the package installed, you’ll find the sphinx-quickstart command available on your path.

01:49 This command creates the structure needed for your documentation directory. Let’s go see how this works in practice.

01:58 I’m at the regular old command line here. Throughout this course, I’m going to be documenting a mini project named Serenity. So the first thing I need to do is create a directory for my project.

02:13 Now I’ll create a directory for our docs

02:19 and inside the docs directory, I run the sphinx-quickstart command.

02:25 This asks me a series of questions to configure my doc structure. The first question is whether to use separate build and source directories. If you say yes, you will get a directory named build and another named "source".

02:38 If you say no, the default, you get a directory named "_build", and your source files go in the same local folder. As the build files still end up in their own location, I don’t find the separation to be an advantage, so I go with the default here.

02:55 The next question is what to call the project. This name shows up in the documentation a bunch of times. It’s case sensitive, so capitalize it the way you want the name to show up in the docs.

03:07 Next is who to blame, and that would be me.

03:12 And then the version number. To be clear, I’ve gone with something indicating very premature, 0.0.1. Sphinx supports multiple languages. This is the spot where you can change the language for your docs.

03:27 I don’t know if they support Klingon, so I’ll be sticking with English. And with all that out of the way, it creates your skeleton files. That list of stuff there, starting with creating shows the files it’s writing to your folder.

03:41 Let’s take a look inside.

04:44 You get a makefile, some directories for building stuff, a make batch file in case you’re on Windows, a configuration Python script, and an index page in the RST format.

03:56 Let’s start by taking a look at the configuration script.

04:02 This is conf.py, the configuration script for the documentation folder that I just generated. Sphinx imports this and uses the variables when it builds things.

04:12 Notice the first block of variables are the answers to the questions asked in the quick start script. Because this is a Python script, you can do some fancy things that you couldn’t otherwise accomplish with a text format.

04:24 Later, I’ll write some code to automatically update the copyright date. For now, let’s move on to index.rst.

04:35 This is the index file for your documentation. It’s the equivalent of a homepage in HTML. As the name implies, it’s in RST format. In the next lesson, I’ll go over RST in detail.

04:48 For now, just notice the equal signs (=) as underlines indicating section titles. The chunk at the top indicated by the double dots is a comment.

04:56 There are also directives in here for inserting a table of contents. If all you’re generating is a single-page document, you can start writing in here. Once I’ve covered RST, I’ll be adding some content in this document.

05:10 Alright, you’ve seen the files. Let’s go create some output.

05:17 Back in my shell in my documents folder. Remember that quick start script generated a makefile for you. If you’re on a Unix-based system, you run the make command.

05:26 If you’re on Windows, you’ll be using the batch file instead. make on its own spits out some help. You need to provide a target indicating what you want made.

05:37 You can see from this list that there are a bunch of different output formats. I mostly just use HTML personally. Scrolling down, and let’s build an HTML doc.

05:50 And there you go. All that output says is it got made. If there was a problem with your files, it would show up in this output. Let’s peek inside the build directory now that I’ve built something.

06:05 And now inside the build HTML directory,

06:11 all those files were created as part of the build process. It’s more than just a single file because of things like the ability to search and the indices.

06:21 Let me go open up a browser and I’ll show you what this looks like.

06:27 Not much to see here since my index.rst file was almost empty but you can see the title and an area on the left-hand side where you’ll get some more information when your document becomes more complicated.

06:39 This will all become more interesting in the next lesson, when I show you how to add some content.

06:46 Next up, I show you how to add some more content. Get ready to get your markup on.

Become a Member to join the conversation.