genindex和modindex页脚链接在readthedocs.io中不起作用

如何解决genindex和modindex页脚链接在readthedocs.io中不起作用

我有一个使用Sphinx用于文档的Python项目。我要在readthedocs.io服务上远程构建文档。

我使用了sphinx-quickstart,它生成了一个index.rst文件,其中这些链接位于页脚中:

Indices and tables
~~~~~~~~~~~~~~~~~~

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

当我将更改推送到readthedocs.io并构建文档时,构建成功。我通过toctree指令手动链接的文档都可以正常工作。

search链接可以正常工作。

但是genindex链接会转到空白页,标题为“索引”

然后modindex页链接到py-modindex.html,它是404。

遵循本指南:https://samnicholls.net/2016/06/15/how-to-sphinx-readthedocs我已经运行sphinx-apidoc -o api-docs/ ../myproject来生成autodoc .rst文件。

我在api-docs/modules.rst顶部的toctree部分中链接了所得的index.rst ...该链接有效,并且如果我单击api-docs,就会生成该链接正确。

sphinx-autodoc生成的也是我项目中每个程序包的文件,它们包含如下指令:

myproject.whatever module
-------------------------

.. automodule:: myproject.whatever
   :members:
   :undoc-members:
   :show-inheritance:

如果我直接浏览到这些页面,则它们具有内容,但是它们没有出现在索引中(只有它们被手动链接到的目录)。

我还有一些手动创作的页面,再次通过目录链接。

我的docs/conf.py如下:

import os
import sys

sys.path.insert(0,os.path.abspath("../"))

extensions = [
    "sphinx.ext.autodoc","sphinx.ext.viewcode","sphinx.ext.napoleon","sphinx_autodoc_typehints",]

templates_path = ["_templates"]

exclude_patterns = ["_build","Thumbs.db",".DS_Store"]

html_theme = "alabaster"

html_static_path = ["_static"]

我相信从autodoc .rst存根文件生成的html充满了从我项目中.py文件中提取的模块和类的事实,表明sys path fix和autodoc基本上可以正常工作。

所以我的问题是:

  • 如何使:ref:`genindex` 有一些内容?
  • 如何将:ref:`modindex` 的点修复为不存在的py-modindex.html

解决方法

genindexmodindex由Sphinx自动管理。应该考虑两种情况:

  1. .rst文件中的任何声明都将插入这些索引中。例如,如果您从Python域中声明a function
Your rst file
-------------

.. py:function:: name(parameters)

即使在任何.py文件中没有对应的功能,它也会插入索引中。

  1. 使用autodoc指令,同样适用于更多规则。 autodoc扩展名将替换域声明(如上),具体取决于对象是否具有docstring以及您是否使用:members:和/或:undoc-members:选项。因此,您必须验证所使用的选项和指令是否正确。
Your rst file
-------------

.. autoclass:: Your_Module.Your_Class
   :members:

如果相应的类具有文档字符串,则上面的示例将用:py:class::域声明代替,否则,您需要添加:undoc-members:选项。



当您在.rst文件中未声明任何内容时,就会出现所描述的具有空索引的症状。细微之处在于autodoc指令可能会为您做那些声明,也可能不会为您做这些声明,具体取决于对象是否具有docstrings,并且您在指令中使用了正确的选项。



编辑:您还应该在构建之前运行make clean(例如make html),因为构建之间的不一致会破坏索引。

,

当我最终在注释中得出结论时,由于@bad_coder的帮助,我的问题专门针对在readthedocs.io中构建文档

在本地构建文档工作正常。

原因归结为使用sphinx.ext.autodoc或与sphinx_autodoc_typehints结合使用,这似乎需要导入我的实际python代码。查看我看似成功的readthedocs版本的日志,结果实际上显示了以下警告:

WARNING: autodoc: failed to import module 'whatever' from module 'myproject'; the following exception was raised:
No module named 'somelib'

即文档仅部分构建,它跳过了无法完成的部分。

该构建在本地工作,因为我已经安装了我所有项目依赖项的virtualenv。

(恕我直言,这似乎是sphinx.ext.autodoc和/或sphinx_autodoc_typehints的错误设计……对于Python而言,存在良好的静态分析工具,该工具可以构建AST或CST并提取结构和文档字符串而无需导入任何代码。)

无论如何,这意味着我需要告诉readthedocs.io如何安装我所有的项目部门。由于我使用的是诗歌,这一点稍微复杂了一点,但没有明确支持。这意味着我没有指向的requirements.txt文件(并且我不想手动创建一个与pyproject.toml中所有内容重复的文件)。

幸运的是,pyproject.toml可以理解pip文件,因此我们可以使用此处描述的pip install方法为readthedocs.io安装我的项目部门和其他部门只需要生成文档即可使用:https://github.com/readthedocs/readthedocs.org/issues/4912#issuecomment-664002569

总结:

  1. 删除了我的docs/requirements.txt
  2. 已添加:
    [tool.poetry.dependencies]
    ...
    sphinx = {version = "^3.1.1",optional = true}
    sphinx-autodoc-typehints ={version = "^1.11.1",optional = true}
    
    和:
    [tool.poetry.extras]
    docs = [
        "sphinx","sphinx-autodoc-typehints"
    ]
    
    到我的pyproject.toml
  3. 将我的.readthedocs.yml更新为:
    version: 2
    
    sphinx:
      configuration: docs/conf.py
    
    python:
      version: 3.8
      install:
        - method: pip
          path: .
          extra_requirements:
            - docs
    
  4. 将这些更改推送到readthedocs.io ...voilà,现在可以使用了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res