Skip to content

Commit 5f8a53f

Browse files
authored
Merge pull request #134 from justwriteclick/build
Edits to Python Sphinx SSG lesson and a new Python Sphinx article
2 parents 4be1480 + 2f94193 commit 5f8a53f

4 files changed

Lines changed: 96 additions & 3 deletions

File tree

_learn/01-sphinx-python-rtd.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Most people use Virtual Environments because it's a recommended practice when wo
7171
1. First create a Python 3 virtual environment using the `venv` module included with Python 3. Notice the example uses `python3.7` to be clear which version of Python you want.
7272
7373
```
74-
python3.6 -m venv py3-sphinx
74+
python3.7 -m venv py3-sphinx
7575
```
7676
7777
1. Now "activate" the environment. Look for the name of the virtual environment enclosed in parenthesis after activation.
@@ -88,11 +88,11 @@ Most people use Virtual Environments because it's a recommended practice when wo
8888
1. Now verify that `python` is now linked to Python 3.
8989
9090
```
91-
(py3-sphinx) $ python -V
91+
(py3-sphinx) $ python3.7 -V
9292
```
9393
9494
```
95-
(py3-sphinx) $ Python 3.7.0
95+
(py3-sphinx) $ Python 3.7.2
9696
```
9797
9898
## Install Sphinx in the Virtual Environment
@@ -124,6 +124,7 @@ You can also get familiar with [ReStructured text](http://docutils.sourceforge.n
124124
1. Answer all the questions from the prompts.
125125
You can choose enter to pick all the defaults and get a working project in the current directory (`.`).
126126
>Some notes for the context of this tutorial:
127+
* If you create a working directory in your home directory, such as `~/src/`, then you can use a `git clone` command in the `src` directory every time. For this tutorial, you can create a directory for your project, such as `~/src/project-name-here`. This directory becomes your root path.
127128
* You can either use a directory named `_build` within the root path, or have separate `source` and `build` directories, which is the default. To see an example directory structure with a `source` directory, refer to this [justwriteclick/rockthedocs-demo](https://github.com/justwriteclick/rockthedocs-demo) repo on GitHub.
128129
* When answering the questions, note that you can choose "githubpages set to yes" to create a `.nojekyll` file to publish the document on GitHub pages. In our case, though, our example builds to Read the Docs, so you can use the defaults throughout.
129130
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 %}

favicon.ico

256 Bytes
Binary file not shown.

images/keyboard-mleung311.jpg

561 KB
Loading

0 commit comments

Comments
 (0)