conf.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. """
  2. Configuration file for the Sphinx documentation builder.
  3. For the full list of built-in configuration values, see the documentation:
  4. https://www.sphinx-doc.org/en/master/usage/configuration.html
  5. """
  6. import os
  7. from functools import wraps
  8. from pathlib import Path
  9. from sphinxcontrib_autodocgen import AutoDocGen
  10. os.environ["UNFCCC_GHG_ROOT_PATH"] = str(Path("..") / "..")
  11. import unfccc_ghg_data # noqa: E402, I001
  12. # -- Project information -----------------------------------------------------
  13. # https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
  14. project = "Country greenhouse gas data submitted to the UNFCCC"
  15. # put the authors in their own variable, so they can be reused later
  16. authors = ", ".join(["Johannes Gütschow"])
  17. # add a copyright year variable, we can extend this over time in future as
  18. # needed
  19. copyright_year = "2023"
  20. copyright = f"{copyright_year}, {authors}"
  21. # -- General configuration ---------------------------------------------------
  22. # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
  23. extensions = [
  24. # create documentation automatically from source code
  25. # https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html
  26. "sphinx.ext.autodoc",
  27. # automatic summary
  28. "sphinx.ext.autosummary",
  29. # automatic summary with better control
  30. "sphinxcontrib_autodocgen",
  31. # tell sphinx that we're using numpy style docstrings
  32. # https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html
  33. "sphinx.ext.napoleon",
  34. # add support for type hints too (so type hints are included next to
  35. # argument and return types in docs)
  36. # https://github.com/tox-dev/sphinx-autodoc-typehints
  37. # this must come after napoleon
  38. # in the list for things to work properly
  39. # https://github.com/tox-dev/sphinx-autodoc-typehints#compatibility-with-sphinxextnapoleon
  40. "sphinx_autodoc_typehints",
  41. # jupytext rendered notebook support (also loads myst_parser)
  42. "myst_nb",
  43. # links to other docs
  44. "sphinx.ext.intersphinx",
  45. # add source code to docs
  46. "sphinx.ext.viewcode",
  47. # add copy code button to code examples
  48. "sphinx_copybutton",
  49. # math support
  50. "sphinx.ext.mathjax",
  51. # execute code
  52. "sphinx_exec_code",
  53. # tables in markdown documents
  54. "sphinx_markdown_tables",
  55. ]
  56. # general sphinx settings
  57. # https://www.sphinx-doc.org/en/master/usage/configuration.html
  58. # Don't include module names in object names (can also be left on,
  59. # depends a bit how your project is structured and what you prefer)
  60. add_module_names = False
  61. # Other global settings which we've never used but are included by default
  62. templates_path = ["_templates"]
  63. # Avoid sphinx thinking that conf.py is a source file because we use .py
  64. # endings for notebooks
  65. exclude_patterns = ["conf.py"]
  66. # Stop sphinx doing funny things with byte order markers
  67. source_encoding = "utf-8"
  68. # configure default settings for autodoc directives
  69. # https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#directives
  70. autodoc_default_options = {
  71. # Show the inheritance of classes
  72. "show-inheritance": True,
  73. }
  74. # autosummary with autodocgen
  75. # make sure autosummary doesn't interfere
  76. autosummary_generate = True
  77. autosummary_generate_overwrite = False
  78. autodocgen_config = [
  79. {
  80. "modules": [unfccc_ghg_data],
  81. "generated_source_dir": "docs/source/api",
  82. # choose a different title for specific modules, e.g. the toplevel one
  83. "module_title_decider": lambda modulename: "API Reference"
  84. if modulename == "unfccc_ghg_data"
  85. else modulename,
  86. }
  87. ]
  88. # monkey patch to remove leading newlines
  89. generate_module_rst_orig = AutoDocGen.generate_module_rst
  90. @wraps(generate_module_rst_orig)
  91. def _generate_module_rst_new(*args, **kwargs):
  92. default = generate_module_rst_orig(*args, **kwargs)
  93. out = default.lstrip("\n")
  94. if not out.endswith("\n"):
  95. out = f"{out}\n"
  96. return out
  97. AutoDocGen.generate_module_rst = _generate_module_rst_new
  98. # napoleon extension settings
  99. # https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html
  100. # We use numpy style docstrings
  101. napoleon_numpy_docstring = True
  102. # We don't use google docstrings
  103. napoleon_google_docstring = False
  104. # Don't use separate rtype for the return documentation
  105. napoleon_use_rtype = False
  106. # autodoc type hints settings
  107. # https://github.com/tox-dev/sphinx-autodoc-typehints
  108. # include full name of classes when expanding type hints?
  109. typehints_fully_qualified = True
  110. # Add rtype directive if needed
  111. typehints_document_rtype = True
  112. # Put the return type as part of the return documentation
  113. typehints_use_rtype = False
  114. # Left-align maths equations
  115. mathjax3_config = {"chtml": {"displayAlign": "center"}}
  116. # myst configuration
  117. myst_enable_extensions = ["amsmath", "dollarmath"]
  118. # cache because we save our notebooks as `.py` files i.e. without output
  119. # stored so auto doesn't work (it just ends up being run every time)
  120. nb_execution_mode = "cache"
  121. nb_execution_raise_on_error = True
  122. nb_execution_show_tb = True
  123. nb_execution_timeout = 120
  124. nb_custom_formats = {".py": ["jupytext.reads", {"fmt": "py:percent"}]}
  125. # exec-code config
  126. exec_code_working_dir = "." # Path('..') / '..'
  127. exec_code_source_folders = [Path("..") / ".." / "src" / "unfccc_ghg_data"]
  128. exec_code_example_dir = "."
  129. # -- Options for HTML output -------------------------------------------------
  130. # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
  131. # Pick your theme for html output, we typically use the read the docs theme
  132. html_theme = "sphinx_book_theme"
  133. html_static_path = ["_static"]
  134. html_theme_options = {
  135. "repository_url": "https://github.com/JGuetschow/UNFCCC_non-AnnexI_data",
  136. "repository_branch": "main",
  137. "path_to_docs": "docs/source",
  138. "use_repository_button": True,
  139. "use_issues_button": True,
  140. "use_edit_page_button": True,
  141. }
  142. # Ignore ipynb files when building (see https://github.com/executablebooks/MyST-NB/issues/363).
  143. def setup(app):
  144. """
  145. Set up the Sphinx app
  146. """
  147. app.registry.source_suffix.pop(".ipynb", None)
  148. # Intersphinx mapping
  149. intersphinx_mapping = {
  150. "numpy": ("https://docs.scipy.org/doc/numpy", None),
  151. "pandas": ("https://pandas.pydata.org/pandas-docs/stable", None),
  152. "python": ("https://docs.python.org/3", None),
  153. "pyam": ("https://pyam-iamc.readthedocs.io/en/latest", None),
  154. "scmdata": ("https://scmdata.readthedocs.io/en/latest", None),
  155. "xarray": ("http://xarray.pydata.org/en/stable", None),
  156. "pint": (
  157. "https://pint.readthedocs.io/en/latest",
  158. None,
  159. ),
  160. }