Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/docs/source/conf.py
6939 views
1
# Configuration file for the Sphinx documentation builder.
2
# https://www.sphinx-doc.org/en/master/usage/configuration.html
3
4
from __future__ import annotations
5
6
import inspect
7
import os
8
import re
9
import sys
10
import warnings
11
from pathlib import Path
12
from typing import Any
13
14
import sphinx_autosummary_accessors
15
16
# -- Path setup --------------------------------------------------------------
17
18
# If extensions (or modules to document with autodoc) are in another directory,
19
# add these directories to sys.path here.
20
21
# Add py-polars directory
22
sys.path.insert(0, str(Path("../..").resolve()))
23
24
25
# -- Project information -----------------------------------------------------
26
27
project = "Polars"
28
author = "Ritchie Vink"
29
copyright = f"2025, {author}"
30
31
32
# -- General configuration ---------------------------------------------------
33
34
extensions = [
35
# Sphinx extensions
36
"sphinx.ext.autodoc",
37
"sphinx.ext.autosummary",
38
"sphinx.ext.githubpages",
39
"sphinx.ext.intersphinx",
40
"sphinx.ext.linkcode",
41
"sphinx.ext.mathjax",
42
# Third-party extensions
43
"autodocsumm",
44
"numpydoc",
45
"sphinx_autosummary_accessors",
46
"sphinx_copybutton",
47
"sphinx_design",
48
"sphinx_favicon",
49
"sphinx_reredirects",
50
"sphinx_toolbox.more_autodoc.overloads",
51
]
52
53
# Render docstring text in `single backticks` as code.
54
default_role = "code"
55
56
maximum_signature_line_length = 88
57
58
59
# Below setting is used by
60
# sphinx-autosummary-accessors - build docs for namespace accessors like `Series.str`
61
# https://sphinx-autosummary-accessors.readthedocs.io/en/stable/
62
templates_path = ["_templates", sphinx_autosummary_accessors.templates_path]
63
64
# List of patterns, relative to source directory, that match files and
65
# directories to ignore when looking for source files.
66
# This pattern also affects html_static_path and html_extra_path.
67
exclude_patterns = ["Thumbs.db", ".DS_Store"]
68
69
# Hide overload type signatures
70
# sphinx_toolbox - Box of handy tools for Sphinx
71
# https://sphinx-toolbox.readthedocs.io/en/latest/
72
overloads_location = ["bottom"]
73
74
75
# -- Extension settings ------------------------------------------------------
76
77
# sphinx.ext.intersphinx - link to other projects' documentation
78
# https://www.sphinx-doc.org/en/master/usage/extensions/intersphinx.html
79
intersphinx_mapping = {
80
"numpy": ("https://numpy.org/doc/stable/", None),
81
"pandas": ("https://pandas.pydata.org/docs/", None),
82
"pyarrow": ("https://arrow.apache.org/docs/", None),
83
"python": ("https://docs.python.org/3", None),
84
}
85
86
# numpydoc - parse numpy docstrings
87
# https://numpydoc.readthedocs.io/en/latest/
88
# Used in favor of sphinx.ext.napoleon for nicer render of docstring sections
89
numpydoc_show_class_members = False
90
91
# Sphinx-copybutton - add copy button to code blocks
92
# https://sphinx-copybutton.readthedocs.io/en/latest/index.html
93
# strip the '>>>' and '...' prompt/continuation prefixes.
94
copybutton_prompt_text = r">>> |\.\.\. "
95
copybutton_prompt_is_regexp = True
96
97
# redirect empty root to the actual landing page
98
redirects = {"index": "reference/index.html"}
99
100
101
# -- Options for HTML output -------------------------------------------------
102
103
# The theme to use for HTML and HTML Help pages.
104
html_theme = "pydata_sphinx_theme"
105
106
# Add any paths that contain custom static files (such as style sheets) here,
107
# relative to this directory. They are copied after the builtin static files,
108
# so a file named "default.css" will overwrite the builtin "default.css".
109
html_static_path = ["_static"]
110
html_css_files = ["css/custom.css"] # relative to html_static_path
111
html_show_sourcelink = False
112
113
# key site root paths
114
static_assets_root = "https://raw.githubusercontent.com/pola-rs/polars-static/master"
115
github_root = "https://github.com/pola-rs/polars"
116
web_root = "https://docs.pola.rs"
117
118
# Specify version for version switcher dropdown menu
119
git_ref = os.environ.get("POLARS_VERSION", "main")
120
version_match = re.fullmatch(r"py-(\d+)\.\d+\.\d+.*", git_ref)
121
switcher_version = version_match.group(1) if version_match is not None else "dev"
122
123
html_js_files = [
124
(
125
"https://plausible.io/js/script.js",
126
{"data-domain": "docs.pola.rs,combined.pola.rs", "defer": "defer"},
127
),
128
]
129
130
html_theme_options = {
131
"external_links": [
132
{
133
"name": "User guide",
134
"url": f"{web_root}/",
135
},
136
{
137
"name": "Polars Cloud API reference",
138
"url": "https://docs.cloud.pola.rs/reference/index.html",
139
},
140
],
141
"icon_links": [
142
{
143
"name": "GitHub",
144
"url": github_root,
145
"icon": "fa-brands fa-github",
146
},
147
{
148
"name": "Discord",
149
"url": "https://discord.gg/4UfP5cfBE7",
150
"icon": "fa-brands fa-discord",
151
},
152
{
153
"name": "X/Twitter",
154
"url": "https://x.com/datapolars",
155
"icon": "fa-brands fa-x-twitter",
156
},
157
{
158
"name": "Bluesky",
159
"url": "https://bsky.app/profile/pola.rs",
160
"icon": "fa-brands fa-bluesky",
161
},
162
],
163
"logo": {
164
"image_light": f"{static_assets_root}/logos/polars-logo-dark-medium.png",
165
"image_dark": f"{static_assets_root}/logos/polars-logo-dimmed-medium.png",
166
},
167
"switcher": {
168
"json_url": f"{web_root}/api/python/dev/_static/version_switcher.json",
169
"version_match": switcher_version,
170
},
171
"show_version_warning_banner": False,
172
"navbar_end": ["theme-switcher", "version-switcher", "navbar-icon-links"],
173
"check_switcher": False,
174
}
175
176
# sphinx-favicon - Add support for custom favicons
177
# https://github.com/tcmetzger/sphinx-favicon
178
favicons = [
179
{
180
"rel": "icon",
181
"sizes": "32x32",
182
"href": f"{static_assets_root}/icons/favicon-32x32.png",
183
},
184
{
185
"rel": "apple-touch-icon",
186
"sizes": "180x180",
187
"href": f"{static_assets_root}/icons/touchicon-180x180.png",
188
},
189
]
190
191
192
# sphinx-ext-linkcode - Add external links to source code
193
# https://www.sphinx-doc.org/en/master/usage/extensions/linkcode.html
194
def linkcode_resolve(domain: str, info: dict[str, Any]) -> str | None:
195
"""
196
Determine the URL corresponding to Python object.
197
198
Based on pandas equivalent:
199
https://github.com/pandas-dev/pandas/blob/main/doc/source/conf.py#L629-L686
200
"""
201
if domain != "py":
202
return None
203
204
modname = info["module"]
205
fullname = info["fullname"]
206
207
submod = sys.modules.get(modname)
208
if submod is None:
209
return None
210
211
obj = submod
212
for part in fullname.split("."):
213
try:
214
with warnings.catch_warnings():
215
# Accessing deprecated objects will generate noisy warnings
216
warnings.simplefilter("ignore", FutureWarning)
217
obj = getattr(obj, part)
218
except AttributeError: # noqa: PERF203
219
return None
220
221
try:
222
fn = inspect.getsourcefile(inspect.unwrap(obj))
223
except TypeError:
224
try: # property
225
fn = inspect.getsourcefile(inspect.unwrap(obj.fget))
226
except (AttributeError, TypeError):
227
fn = None
228
if not fn:
229
return None
230
231
try:
232
source, lineno = inspect.getsourcelines(obj)
233
except TypeError:
234
try: # property
235
source, lineno = inspect.getsourcelines(obj.fget)
236
except (AttributeError, TypeError):
237
lineno = None
238
except OSError:
239
lineno = None
240
241
linespec = f"#L{lineno}-L{lineno + len(source) - 1}" if lineno else ""
242
243
conf_dir_path = Path(__file__).absolute().parent
244
polars_root = (conf_dir_path.parent.parent / "polars").absolute()
245
246
fn = os.path.relpath(fn, start=polars_root)
247
return f"{github_root}/blob/{git_ref}/py-polars/polars/{fn}{linespec}"
248
249
250
def _minify_classpaths(s: str) -> str:
251
# strip private polars classpaths, leaving the classname:
252
# * "pl.Expr" -> "Expr"
253
# * "polars.expr.expr.Expr" -> "Expr"
254
# * "polars.lazyframe.frame.LazyFrame" -> "LazyFrame"
255
# also:
256
# * "datetime.date" => "date"
257
s = s.replace("datetime.", "")
258
return re.sub(
259
pattern=r"""
260
~?
261
(
262
(?:pl|
263
(?:polars\.
264
(?:_reexport|datatypes)
265
)
266
)
267
(?:\.[a-z.]+)?\.
268
([A-Z][\w.]+)
269
)
270
""",
271
repl=r"\2",
272
string=s,
273
flags=re.VERBOSE,
274
)
275
276
277
def process_signature( # noqa: D103
278
app: object,
279
what: object,
280
name: object,
281
obj: object,
282
opts: object,
283
sig: str,
284
ret: str,
285
) -> tuple[str, str]:
286
return (
287
_minify_classpaths(sig) if sig else sig,
288
_minify_classpaths(ret) if ret else ret,
289
)
290
291
292
def setup(app: Any) -> None: # noqa: D103
293
# TODO: a handful of methods do not seem to trigger the event for
294
# some reason (possibly @overloads?) - investigate further...
295
app.connect("autodoc-process-signature", process_signature)
296
297