Outils pour utilisateurs

Outils du site


issue205:python

Ceci est une ancienne révision du document !


More Mysteries of the Sphinx

Greetings fellow Sentient Lifeforms. Beaming yet again from somewhere in time and space, I come again to, hopefully, provide more well needed information. (If you are not a Sentient Lifeform, I do not intend to withhold this from you, so feel free to attempt to read this.)

This month's article was inspired by one of the PAGE users on the PAGE Discord forum and someone whom I consider a good friend, Professor Roberto Machorro Mejía, who started using PAGE last school year. He teaches at UNAM, a public university, the largest in Mexico. He was using PAGE to create a GUI for a project he and his students were working on. Long story short, he has been reading Full Circle Magazine for a while now and he sent me a note that he really enjoyed my article on Sphinx in last month's issue. He was wondering, however, if Sphinx worked directly on a source file. So, I decided to write about how to make this happen.

I created a dummy program that really does nothing much but has functions that have Python Docstrings that can be used by Sphinx to “automatically document” the functions in the generated HTML or EPUB files.

Creating the docstrings that are needed to do this is very simple, but you need to remember to use the docstrings in every function, no matter how simple. If you don’t, you’ll probably have to go back and update the source file.

So, it all starts with your source code. If you use PAGE to create your GUI program, you don’t need to do anything to the PAGE .tcl file or the Python GUI.py file. The only file you need to concern yourself with is the support.py file.

One other thing to note, I’m using the bizstyle theme for Sphinx. If you decide to use a different Sphinx theme, your document won’t match the images that I’ve included here.

The docstring

The docstring is simply a section of your source code that starts and ends with three double quotes and in between is pretty much free-form information. However, for Sphinx to grab the information, there needs to be a few special lines. Here (next page, top right) is a snippet of what you can expect to provide…

Of course the first line is the function definition. Then we start the docstring with the three double quotes.

The next line is a description of what the function is for, which must be a single line. Then any parameters need to be documented using the format:

:param {parameter name}: description of parameter

:type {parameter name}: parameter type

Then what the function returns:

:return: description of the return value

If there is more than one parameter, simply repeat the param and type keys.

For more information on Docstrings, you can follow this link https://peps.python.org/pep-0257/#abstract

Doing it Manually

Of course you can do the documentation of your functions and methods manually. I'll create a “dummy” entry for a fictitious Sphinx document.

This is just the description that I would put into the fictitious index.rst file…

One of the best things about the program is for the user to have the ability to change themes at any time to any theme in the theme folder. Of course, these are all .tcl themes. Use the load_tcl_themes function to generate a list of all of the themes, then load this into a TCombobox values property. Then bind the virtual «ComboboxSelected» event to the TCombobox providing a callback function whenever the user selects something from the TCombobox list. Here is the ``sphinxDemo1_support.on_ComboSelect()`` function

Now we describe the function using the .. py:function:: directive.

You can see that this is very similar to the docstring information that I suggested you place into the source code, but manually entered into the index.rst Sphinx file (right).

Here (next page, top right) is how it will look in your HTML file. That’s a lot of work to do, especially if the code has already been documented using docstrings.

Doing it Automatically

In order to take advantage of the automatic documentation abilities of Sphinx, you will need to do a little bit of preparation. You will need to modify the conf.py file and add at least one extra .rst file to the /sphinx/docs/source folder.

The changes need to go into the conf.py file. These lines (right) need to go somewhere around line 16.

The most important line is the one that starts with

sys.path.append()

This is the fully qualified path to your source code. You don’t need to enter the filename(s) at this point, just the path.

The other important part is the extensions section. You will need to have all four lines in the list.

Now you just need to add one line to your index.rst file for each function you want Sphinx to document.

.. autofunction:: sphinxDemo1_support.load_tcl_themes

Of course, you can add text before or after this line, but here is what the output will look like. (I included the first line, just to make it flow better). See image bottom left.

Adding an Autosummary You need to create an extra .rst file named something like “api.rst”. In it you place just a few lines…

API

.. autosummary::

 :toctree: generated
 sphinxDemo1_support

The final addition to your index.rst file needs to tell Sphinx to include the api.rst file. This is actually just a single line in the TableOfContentsTree section (which is near the end, just before the Indices and tables section).

.. toctree::

 :maxdepth: 2
 :caption: Contents:
 api

Notice that the only line in bold is the one that adds the api.rst file, and this is the one you need to add. You don’t have to include the .rst extension.

When you run your next build, you will see the addition to the bottom of the main page…

And when you click on the hyperlink in red, you will be directed to the api listing.

You can see that the two functions that I included with the .. py:function:: directive, are hyperlinked to the section that I added in the main document.

You can create a special .rst file that holds all the ..py:function:: directives to make it easier to add more information about each of those functions if you wish. Simply place the extra .rst file in the same section before your api.rst inclusion in the toctree section.

One word of warning here. If you are using Formiko as your .rst editor, you will end up with a number of warnings in the preview window. These are usually a “System Message: Error/3” warning. You can usually ignore these messages, since they refer to unknown directives. I’m fairly certain that the folks at Formiko will get these added or addressed in the near future.

Conclusion

If you are getting a number of errors when you do your build, the first thing that you should look at is the directive formatting and the line(s) following the directive. Sphinx is VERY picky about formatting and white space. It’s usually something very simple.

I’ve created a repository as I often do. I included the Sphinx index.rst and conf.py file to make it easy for you to have a reference to this month’s information.

The repository is located at https://github.com/gregwa1953/FCM-205. This repository is a little different from my normal ones. There are two folders that hold the demo files. The first is the code folder which holds all of the files for the demo program, including the PAGE .tcl file and the docs folder that has the Sphinx documentation HTML file. The other folder is the SphinxSource folder which holds the images folder, api.rst, index.rst and the conf.py files.

I also included the generated HTML file in the code/docs/html file as well as enabled the help button in the sample program to display the HTML on demand.

Until next time, as always; stay safe, healthy, positive and creative!

issue205/python.1717230052.txt.gz · Dernière modification : 2024/06/01 10:20 de auntiee