|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Yes You Can Use GitHub Pages with Python Sphinx |
| 4 | +excerpt: "Learn some pro tips to build Python Sphinx developer docs to both GitHub Pages and your local system." |
| 5 | +last_modified_at: Sat Apr 20 09:43:00 CDT 2019 |
| 6 | +categories: articles |
| 7 | +tags: [github, git, python, sphinx, documentation, developer, docs] |
| 8 | +image: |
| 9 | + path: /images/keyboard-mleung311.jpg |
| 10 | + caption: "[Flickr mleung311](https://flic.kr/p/fqJJbW)" |
| 11 | +comments: false |
| 12 | +share: true |
| 13 | +--- |
| 14 | + |
| 15 | +If you prefer to write in ReStructured Text instead of Markdown for your technical documentation, you're in good company. There are quite a few benefits to using Sphinx, Python, RST, and Sphinx extensions because these tools are custom-built with developer documentation in mind. |
| 16 | + |
| 17 | +Another great offering is GitHub Pages, with automatic publishing when a known branch, such as the `master` or `gh-pages` branch is updated. |
| 18 | + |
| 19 | +So, how about the best of both? Let's dive into the key configurations that enable you to publish Python Sphinx pages to GitHub Pages. |
| 20 | + |
| 21 | +## Naming the GitHub repository can affect the resulting URL |
| 22 | + |
| 23 | +If you want a URL like `<username>.github.io` or ``<orgname>.github.io`, name the repo `<username>.github.io` or `<orgname>.github.io`. Then, by default, GitHub Pages publishes to the URL matching the repo name. |
| 24 | + |
| 25 | +## Enable GitHub Pages for the GitHub repository |
| 26 | + |
| 27 | +> Note: You must have admin privileges on the repository to see the **Settings** tab. |
| 28 | +
|
| 29 | +1. Go to the repository on the GitHub website and make sure you are logged in. |
| 30 | +1. Add a `/docs` directory to the `master` branch. Otherwise you do not get the **master branch /docs folder** for the Source option in the drop-down list. |
| 31 | +1. Click the **Settings** tab. You first go to the Options section. |
| 32 | +1. Scroll down to the **GitHub Pages** section and choose the drop-down list under Source. |
| 33 | + > Note: Your choices will differ based on whether you're in a User repo or an Org repo. [Read all about the differences in the GitHub docs](https://help.github.com/en/articles/user-organization-and-project-pages). |
| 34 | +
|
| 35 | +1. To keep source and output HTML separate, choose **master branch /docs folder** for Source. |
| 36 | + |
| 37 | +Next, you set up the `.nojekyll` file to indicate you aren't using Jekyll as your static site generator in this repository. |
| 38 | + |
| 39 | +## Add a .nojekyll file in the /docs directory |
| 40 | + |
| 41 | +When GitHub sees a `.nojekyll` file, it serves the root `index.html` file. Read more in the [original blog post about this feature](https://github.blog/2009-12-29-bypassing-jekyll-on-github-pages/). |
| 42 | + |
| 43 | +> Note: I'd recommend using the `/docs` directory on the `master` branch, so that you are sure to keep source and output HTML separate. You could also use a `_build` directory in the root of your repo as the served HTML. |
| 44 | +
|
| 45 | +1. Create a new branch locally, named `docs-config` in this example, based on `master`: |
| 46 | + ``` |
| 47 | + $ git checkout -b docs-config |
| 48 | + ``` |
| 49 | +1. Create an empty dot file, named `.nojekyll` within the `/docs` folder of your repo. A dot file has a period prefix and may not be visible in all text editors or your Finder windows, so be aware and don't panic if you can't "see" it later. |
| 50 | +1. Use the add command to make sure Git starts tracking the file: |
| 51 | + ``` |
| 52 | + $ git add docs/.nojekyll |
| 53 | + ``` |
| 54 | +1. Commit the change with the `-a` option to add new files to the commit, and the `-m` option for a message: |
| 55 | + ``` |
| 56 | + $ git commit -a -m "Adds a .nojekyll file for docs builds" |
| 57 | + ``` |
| 58 | +1. Push the change to the remote, in this case named "origin", so you can compare the change to `master`. |
| 59 | + ``` |
| 60 | + $ git push origin docs-config |
| 61 | + ``` |
| 62 | +1. On the GitHub website, go to your repository and create a Pull Request. |
| 63 | +1. Merge the Pull Request to add the `.nojekyll` file to the `master` branch. |
| 64 | + |
| 65 | +## Create a "docsource" or "docsrc" folder for the source files |
| 66 | + |
| 67 | +Now, in the root of your repository, you can create a source folder for your doc builds: |
| 68 | + |
| 69 | + - If you're working with code and docs in the same repo, you can name the folder `docsource` or `docsrc`. |
| 70 | + - If your repo is for only documentation, you can name the folder `source`. |
| 71 | + |
| 72 | +Next, make sure that your `conf.py` file has been set up with these source and destination directories. The Sphinx documentation has a good [Configuration section](http://www.sphinx-doc.org/en/master/usage/configuration.html). |
| 73 | + |
| 74 | +## Make sure you can still build Sphinx locally for reviews |
| 75 | + |
| 76 | +Here's another pro tip I found while browsing [Issues in the Sphinx repository itself](https://github.com/sphinx-doc/sphinx/issues/3382#issuecomment-470772316). Since you want to keep the source and output separate, but still be able to both publish on GitHub Pages and preview builds locally, you can add an option to your `Makefile` to do both. |
| 77 | + |
| 78 | +``` |
| 79 | + github: |
| 80 | + @make html |
| 81 | + @cp -a _build/html/. ../docs |
| 82 | +``` |
| 83 | + |
| 84 | +Now you can run `make github` from the doc source directory to generate a local preview and move the docs where GitHub wants to serve them from. |
| 85 | + |
| 86 | +> Note: Realize that there's a `conf.py` setting for both where the `make` command is run and where files are built to. Read up in the [Configuration section](http://www.sphinx-doc.org/en/master/usage/configuration.html) of the Sphinx docs for all the details. |
| 87 | +
|
| 88 | +## Pull it all together |
| 89 | + |
| 90 | +I've been working on a talk titled, "Make an Instant Web Site with Webhooks" for DevNet Create. In it, I show how to publish and host on GitHub Pages, which is built to use Jekyll by default, using Python Sphinx instead. I've got a GitHub repository set up at [annegentle/create-demo](https://github.com/annegentle/create-demo) as a demonstration. I'll post the slides when it's done. In the meantime, we'd love to see you next week! Visit the [DevNet Create site](https://developer.cisco.com/devnetcreate/2019) for more information. |
| 91 | + |
| 92 | +{% include sign-up.html %} |
0 commit comments