Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python sphinx, how to link to a file in _static directory

Tags:

I have some code and data samples I copy to _static directory and I would like to link to those files in documentation, something like:

.. _pca-run.py: _static/example.data 

But the problem is that sphinx does not create a proper relative link to this file, but just copies the values as it is. So for nested files where _static is not in the same directory links do not work.

like image 519
Mitar Avatar asked Dec 12 '11 14:12

Mitar


People also ask

What is Toctree Sphinx?

The toctree directive is the central element. Note. Simple “inclusion” of one file in another can be done with the include directive.

How do you set up a Sphinx Python?

You can install Sphinx directly from a clone of the Git repository. This can be done either by cloning the repo and installing from the local clone, on simply installing directly via git. $ git clone https://github.com/sphinx-doc/sphinx $ cd sphinx $ pip install .

Does Sphinx support markdown?

To support Markdown-based documentation, Sphinx can use MyST-Parser. MyST-Parser is a Docutils bridge to markdown-it-py, a Python package for parsing the CommonMark Markdown flavor.

How does Sphinx execute a Python file?

The configuration file is executed as Python code at build time (using importlib.import_module (), and with the current directory set to its containing directory), and therefore can execute arbitrarily complex code. Sphinx then reads simple names from the file’s namespace as its configuration.

How do I get a list of all links in Sphinx?

When you build your documentation, Sphinx will generate an inventory of all explicit and implicit links called objects.inv. You can list all of these targets to explore what is available for you to reference. Where <link> is either a URL or a local path that points to your inventory file (usually in _build/html/objects.inv ).

How to load an extname from the subdirectory in Sphinx?

That way, you can load an extension called extname from the subdirectory sphinxext. The configuration file itself can be an extension; for that, you only need to provide a setup () function in it. The file extensions of source files. Sphinx considers the files with this suffix as sources.

What does <link> mean in intersphinx?

Where <link> is either a URL or a local path that points to your inventory file (usually in _build/html/objects.inv ). For example, to see all targets from the Read the Docs documentation: You can reference to docs outside your project too! See Link to Other Projects’ Documentation With Intersphinx.


2 Answers

What you want is the :download: text role. (as Mitar mentioned in his comment).

https://www.sphinx-doc.org/en/stable/usage/restructuredtext/roles.html#role-download

Using this will tell Sphinx to copy the given file to a "_downloads" directory and create a hyperlink to it. This was originally intended to be used for downloadable files, like perhaps PDFs (in html output) or tarballs, or whatever. It works just fine for any other non-ReST file though.

If you really wanted, you could write an extension to do this, but I've never seen a need to, since :download: does exactly what I want.

like image 185
Kevin Horn Avatar answered Sep 21 '22 23:09

Kevin Horn


Just find the right answer to this for images: preprend the path of the target file with /. For images referenced from an automodule

.. image:: some_file.png 

will refer to the file some_file.png relative to the python file currently being processed, while

.. image:: /some_file.png 

will refer to the file some_file.png relative to the location of the conf.py. This way, there is no need to pollute the source hierarchy with images.

like image 25
Raffi Avatar answered Sep 17 '22 23:09

Raffi