Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beginning of new Sphinx tutorial #9276

Merged
merged 18 commits into from
Jun 13, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added doc/_static/tutorial/lumache-first-light.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 7 additions & 5 deletions doc/_templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,25 @@ <h2 style="margin-bottom: 0">{%trans%}Documentation{%endtrans%}</h2>
<p class="biglink"><a class="biglink" href="{{ pathto("usage/quickstart") }}">{%trans%}First steps with Sphinx{%endtrans%}</a><br/>
<span class="linkdescr">{%trans%}overview of basic tasks{%endtrans%}</span></p>
</td><td>
{%- if hasdoc('search') %}<p class="biglink"><a class="biglink" href="{{ pathto("search") }}">{%trans%}Search page{%endtrans%}</a><br/>
<span class="linkdescr">{%trans%}search the documentation{%endtrans%}</span></p>{%- endif %}
<p class="biglink"><a class="biglink" href="{{ pathto("tutorial/index") }}">{%trans%}Tutorial{%endtrans%}</a><br/>
<span class="linkdescr">{%trans%}beginners tutorial{%endtrans%}</span></p>
</td>
</tr><tr>
<td>
<p class="biglink"><a class="biglink" href="{{ pathto("contents") }}">{%trans%}Contents{%endtrans%}</a><br/>
<span class="linkdescr">{%trans%}for a complete overview{%endtrans%}</span></p>
</td><td>
{%- if hasdoc('genindex') %}<p class="biglink"><a class="biglink" href="{{ pathto("genindex") }}">{%trans%}General Index{%endtrans%}</a><br/>
<span class="linkdescr">{%trans%}all functions, classes, terms{%endtrans%}</span></p>{%- endif %}
{%- if hasdoc('search') %}<p class="biglink"><a class="biglink" href="{{ pathto("search") }}">{%trans%}Search page{%endtrans%}</a><br/>
<span class="linkdescr">{%trans%}search the documentation{%endtrans%}</span></p>{%- endif %}
</td>
</tr><tr>
<td>
<p class="biglink"><a class="biglink" href="{{ pathto("changes") }}">{%trans%}Changes{%endtrans%}</a><br/>
<span class="linkdescr">{%trans%}release history{%endtrans%}</span></p>
</td><td>
</td>
{%- if hasdoc('genindex') %}<p class="biglink"><a class="biglink" href="{{ pathto("genindex") }}">{%trans%}General Index{%endtrans%}</a><br/>
<span class="linkdescr">{%trans%}all functions, classes, terms{%endtrans%}</span></p>{%- endif %}
</td><td>
</tr>
</table>

Expand Down
1 change: 1 addition & 0 deletions doc/contents.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Sphinx documentation contents
:maxdepth: 2

usage/index
tutorial/index
tk0miya marked this conversation as resolved.
Show resolved Hide resolved
development/index
man/index

Expand Down
213 changes: 213 additions & 0 deletions doc/tutorial/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
.. _tutorial:

===============
Sphinx tutorial
===============

In this tutorial you will build a simple documentation project using Sphinx,
and view it in your browser as HTML.
The project will include narrative, handwritten documentation,
as well as autogenerated API documentation.

The tutorial is aimed towards Sphinx newcomers
willing to learn the fundamentals of how projects are created and structured.
You will create a fictional software library to generate random food recipes
that will serve as a guide throughout the process,
with the objective of properly documenting it.

To showcase Sphinx capabilities for code documentation
you will use Python,
which also supports *automatic* documentation generation.
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved

.. note::

Several other languages are natively supported in Sphinx
for *manual* code documentation,
however they require extensions for *automatic* code documentation,
like `Breathe <https://breathe.readthedocs.io/>`_.

To follow the instructions you will need access to a Linux-like command line
and a basic understanding of how it works,
as well as a working Python installation for development,
since you will use *Python virtual environments* to create the project.

Getting started
---------------

Setting up our project and development environment
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

On a new directory,
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved
create a file called ``README.rst``
with the following contents:
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: rest

Lumache
=======

**Lumache** (/luˈmake/) is a Python library for cooks and food lovers
that creates recipes mixing random ingredients.

It is a good moment to create a Python virtual environment
and install the required tools.
For that, open a command line terminal,
``cd`` into the directory you just created,
and run the following commands:

.. code-block:: bash

$ python -m venv .venv
tk0miya marked this conversation as resolved.
Show resolved Hide resolved
$ source .venv/bin/activate
(.venv) $ python -m pip install sphinx

.. note::

The installation method used above
is described in more detail in :ref:`install-pypi`.
For the rest of this tutorial,
the instructions will assume a Python virtual environment.

If you executed these instructions correctly,
you should have the Sphinx command line tools available.
You can do a basic verification running this command:

.. code-block:: bash

(.venv) $ sphinx-build --version
sphinx-build 4.0.2

If you see a similar output, you are on the right path!

Creating the documentation layout
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Then, from the command line,
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved
run the following command:

.. code-block:: bash

(.venv) $ sphinx-quickstart docs

This will present you a series of questions
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved
required to create the basic directory and configuration layout for your project
inside the `docs/` folder.
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved
To proceed, introduce these answers:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answer each question as follows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I observed that you tend to remove colons at the end of sentences that end in "as follows", whereas you are okay with some other sentences that precede enumerations or instructions. I'd be keen to learn more, would you please share some explanations about it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First I should preface that the dominant practice should be continued, but I don't know what that is for Sphinx. A style guide should be adopted. @tk0miya is there an established preferred style in Sphinx for how to end a sentence when followed by a list or code block?

There are varying opinions about ending a sentence with a colon or a period when followed by a list, but not many about following it with code. The context and audience need to be considered. For example, technical documentation versus a narrative tutorial. Even American and English English don't agree. No one way is correct, as they are all opinions. I may have been inconsistent when spotting these instances and reviewing them. Apologies for that.

Some guides prefer to end all sentences with a period and never a colon. I prefer this style because it prevents the confusion of what punctuation to use when ending a sentence, as they always end with a period without exception. It avoids sentence fragments, such as "For example:" and encourages the writer to form complete sentences. This is especially helpful for visually impaired readers and screen readers. It prevents the confusion of whether to use : or :: when a code block follows to get syntax highlighting.

"As follows", "following", and similar phrases are hints to readers—especially visually impaired or people with reading comprehension impairments—that something is coming up related to this sentence, so I almost always include them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there an established preferred style in Sphinx for how to end a sentence when followed by a list or code block?

AFAIK, there are no policies and rules for them.

IMO, reST provides :: syntax to notate a code-block after a paragraph

blah blah blah::

    here is a code block.

This is rendered as following.

blah blah blah:

    here is a code block.

So I think the sentences that end with a colon just before code-block are commonly used in reST documents. I've usually written them also. To imitate the colon at the tail of the sentence, I've usually use a single colon and code-block directive as follows

blah blah blah:

.. code-block:: some-highlighting language

   here is a code block.

On the other hand, I don't see the sentences that end with a colon just before bullet-list much.


- ``> Separate source and build directories (y/n) [n]``: Write "``y``" (without quotes)
and press :kbd:`Enter`.
- ``> Project name``: Write "``Lumache``" (without quotes)
and press :kbd:`Enter`.
- ``> Author name(s)``: Write "``Graziella``" (without quotes)
and press :kbd:`Enter`.
- ``> Project release []``: Write "``0.1``" (without quotes)
and press :kbd:`Enter`.
- ``> Project language [en]``: Leave it empty (the default, English)
and press :kbd:`Enter`.
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved

After the last question,
you will see the new ``docs`` directory with some content::
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved

docs/
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved
├── build
├── make.bat
├── Makefile
└── source
├── conf.py
├── index.rst
├── _static
└── _templates

These files are:
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved

``build/``

An empty directory (for now)
that will hold the rendered documentation.

``make.bat`` and ``Makefile``

Convenience scripts
to simplify some common Sphinx operations,
such as rendering the content.

``source/conf.py``

A Python script holding the configuration of the Sphinx project.
It contains the project name and release you specified to ``sphinx-quickstart``,
as well as some extra configuration keys.

``source/index.rst``

The :term:`master document` of the project,
which serves as welcome page
and contains the root of the "table of contents tree" (or *toctree*).
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved

Thanks to this bootstrapping step,
you already have everything needed
to render the documentation as HTML for the first time.
To do that, run this command:

.. code-block:: bash

(.venv) $ sphinx-build -b html docs/source/ docs/build/html

And finally, open `docs/build/html/index.html` in your browser.
You should see something like this:

.. image:: /_static/tutorial/lumache-first-light.png

There we go! You created your first HTML documentation using Sphinx.

Making some tweaks to the index
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The ``index.rst`` file that ``sphinx-quickstart`` created
has some content already,
and it gets rendered as the front page of our HTML documentation.
It is written in reStructuredText,
a powerful markup language.

Modify the file like follows:
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: rest

Welcome to Lumache's documentation!
===================================

**Lumache** (/luˈmake/) is a Python library for cooks and food lovers
that creates recipes mixing random ingredients.
It pulls data from the `Open Food Facts database <https://world.openfoodfacts.org/>`_
and offers a *simple* and *intuitive* API.

.. note::

This project is under active development.

This showcases several features of the reStructuredText syntax, including:

- a **section header** using ``===`` for the underline,
- two examples of **inline markup**: ``**strong emphasis**`` (typically bold)
and ``*emphasis*`` (typically italics),
- an **inline external link**,
- and a ``note`` **admonition**.
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved

Now, to render it with the new content,
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved
you can use the ``sphinx-build`` command as before,
or leverage the convenience script like this:
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: bash

(.env) $ cd docs/
(.env) $ make html
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved

After running this command,
you will see that ``index.html`` reflects the new changes!

Where to go from here
---------------------

This tutorial covered
the very first steps to create a documentation project with Sphinx.
To continue learning more about Sphinx,
check out the :ref:`rest of the documentation <contents>`.
24 changes: 24 additions & 0 deletions doc/usage/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,30 @@ the ``--pre`` flag.

$ pip install -U --pre sphinx

Using virtual environments
~~~~~~~~~~~~~~~~~~~~~~~~~~

When installing Sphinx using pip,
it is highly recommended to use *virtual environments*,
which isolate the Installation
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved
and remove the need to use administrator privileges.
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved
To create a virtual environment in the ``.venv`` directory,
use the following command.

::

$ python -m venv .venv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more reason to use env instead of .venv, the PyPA uses this in its documentation:

python3 -m venv env

or

py -m venv env

Being consistent with the PyPA docs is a very Good Thing™.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being consistent with the PyPA docs is a very Good Thing™.

I might be excessively nitpicky with this, but the Python Packaging Authority has stated several times that they are not an authority, despite the name :) Many people (including myself) have fallen this trap several times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having said that:

  1. All options seem to be somewhat used, judging at the output of https://www.gitignore.io/api/python:
# Environments
# .env
.env/
.venv/
env/
venv/
ENV/
env.bak/
venv.bak/
pythonenv*
  1. Consistency with the PyPA docs is a good thing in itself, but A Foolish Consistency is the Hobgoblin of Little Minds. I like .venv because it's hidden, and I avoid env or .env to remove confusion with direnv and similar tools. However, I'm sure they have their own reasons to adopt env, and probably either name is just fine.
  2. On the other hand, giving it a different name from the PyPA docs also communicates that it doesn't really matter, or that the user can change it if they want.
  3. Like all serious things in life, let's decide them with a Twitter poll (I'm joking, but anyway I'm curious about the results)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. OK. I see env is common usage.
  2. Would this consistency with the PyPA example be foolish?
  3. That would be a sidebar to creating a virtual environment.
  4. There's also strong opinions about where to put them, either inside the project folder or in the user home directory.

For tutorials, I strongly advocate for keeping things as simple as possible on all levels to avoid confusion. If you have ever conducted any training, you know how difficult it is to get people to install packages into a virtual environment in the first place. That's the background to my reasons for using env in my original comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python3 -m venv env

Only because I need to update https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/?highlight=venv#creating-a-virtual-environment. 😅

In general, I think a lot of the material is converging toward .venv and the fact that packaging.python.org is lagging is due to lack of volunteer time to update it more than anything else.

Being consistent with the PyPA docs is a very Good Thing™.

WELL. I guess it's a good thing that I'm currently working on improving PyPA documentation stuff then. ;)

Copy link
Contributor

@pradyunsg pradyunsg Jun 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python Packaging Authority has stated several times that they are not an authority, despite the name :)

As an aside, there is definitely authority associated with the PyPA now -- Python Steering Council officially delegates power for deciding on all Python Packaging things to the PyPA. I think we're aware of the "what we say matters" situation, but that is not as coordinated/structured as folks would expect from "authority". :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this is to say, .venv works and I don't think it needs to change to be in sync with packaging.python.org; although I'll likely update that to match this style later. :)


You can read more about them in the `Python Packaging User Guide`_.

.. _Python Packaging User Guide: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment

.. warning::

Note like in some Linux distributions like Debian and Ubuntu
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved
this might require an extra installation step::
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved

$ apt-get install python3-venv
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved

Docker
------
Expand Down