sphinx.ext.viewcode -- ハイライト済みのソースコードへのリンクを追加

モジュールの作者: Georg Brandl

Added in version 1.0.

この拡張は、Pythonのオブジェクト説明(.. class::, .. function:: など)、そのオブジェクトが含まれるソースコードを探しに行きます。もし見つかれば、それぞれのモジュールごとにハイライトされたソースコードのHTMLページを出力し、すべての説明オブジェクトからのリンクが追加されます。これにより、説明オブジェクトのソースコードを見に行くことができます。また、説明オブジェクトへの逆リンクも挿入されます。


Basically, viewcode extension will import the modules being linked to. If any modules have side effects on import, these will be executed when sphinx-build is run.

もしあなたが (ライブラリモジュールではなく) スクリプトをドキュメント化したいのであれば、スクリプトのメインルーチンが if __name__ == '__main__' 条件により保護されていることを確認して下さい。

In addition, if you don't want to import the modules by viewcode, you can tell the location of the location of source code to viewcode using the viewcode-find-source event.

If viewcode_follow_imported_members is enabled, you will also need to resolve imported attributes using the viewcode-follow-imported event.

This extension works only on HTML related builders like html, applehelp, devhelp, htmlhelp, qthelp and so on except singlehtml. By default epub builder doesn't support this extension (see viewcode_enable_epub).



If this is True, viewcode extension will emit viewcode-follow-imported event to resolve the name of the module by other extensions. The default is True.

Added in version 1.3.

バージョン 1.8 で変更: Renamed from viewcode_import to viewcode_follow_imported_members.


If this is True, viewcode extension is also enabled even if you use epub builders. This extension generates pages outside toctree, but this is not preferred as epub format.

Until 1.4.x, this extension is always enabled. If you want to generate epub as same as 1.4.x, you should set True, but epub format checker's score becomes worse.

デフォルト値は False です。

Added in version 1.5.


Not all epub readers support pages generated by viewcode extension. These readers ignore links to pages are not under toctree.

Some reader's rendering result are corrupted and epubcheck's score becomes worse even if the reader supports.


Default: False.

If set to True, inline line numbers will be added to the highlighted code.

Added in version 7.2.

viewcode-find-source(app, modname)

Added in version 1.8.

Find the source code for a module. An event handler for this event should return a tuple of the source code itself and a dictionary of tags. The dictionary maps the name of a class, function, attribute, etc to a tuple of its type, the start line number, and the end line number. The type should be one of "class", "def", or "other".

  • app -- The Sphinx application object.

  • modname -- The name of the module to find source code for.

viewcode-follow-imported(app, modname, attribute)

Added in version 1.8.

Find the name of the original module for an attribute.

  • app -- The Sphinx application object.

  • modname -- The name of the module that the attribute belongs to.

  • attribute -- The name of the member to follow.