Skip to content

Commit ef0ad36

Browse files
DOC: Add redirect for moved classes (pandas-dev#16200)
* DOC: Add redirect for moved classes The new redirects in this commit are for Resampler and Styler Refactor how we do redirects. Moved all the logic into the config file, where you state the methods / classes to be redirected. Removed all the logic from the template, and just look up in the new html_context variable. Closes pandas-dev#16186 * Only redirect for used dunder methods
1 parent 20fda22 commit ef0ad36

File tree

2 files changed

+67
-22
lines changed

2 files changed

+67
-22
lines changed

doc/_templates/api_redirect.html

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
{% set pgn = pagename.split('.') -%}
2-
{% if pgn[-2][0].isupper() -%}
3-
{% set redirect = ["pandas", pgn[-2], pgn[-1], 'html']|join('.') -%}
4-
{% else -%}
5-
{% set redirect = ["pandas", pgn[-1], 'html']|join('.') -%}
6-
{% endif -%}
1+
{% set redirect = redirects[pagename.split("/")[-1]] %}
72
<html>
83
<head>
9-
<meta http-equiv="Refresh" content="0; url={{ redirect }}" />
4+
<meta http-equiv="Refresh" content="0; url={{ redirect }}.html" />
105
<title>This API page has moved</title>
116
</head>
127
<body>
13-
<p>This API page has moved <a href="{{ redirect }}">here</a>.</p>
8+
<p>This API page has moved <a href="{{ redirect }}.html">here</a>.</p>
149
</body>
15-
</html>
10+
</html>

doc/source/conf.py

+63-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import os
1515
import re
1616
import inspect
17+
import importlib
1718
from pandas.compat import u, PY3
1819

1920
# https://github.com/sphinx-doc/sphinx/pull/2325/files
@@ -226,20 +227,69 @@
226227
# Additional templates that should be rendered to pages, maps page names to
227228
# template names.
228229

229-
# Add redirect for previously existing API pages (which are now included in
230-
# the API pages as top-level functions) based on a template (GH9911)
230+
# Add redirect for previously existing API pages
231+
# each item is like `(from_old, to_new)`
232+
# To redirect a class and all its methods, see below
233+
# https://github.com/pandas-dev/pandas/issues/16186
234+
231235
moved_api_pages = [
232-
'pandas.core.common.isnull', 'pandas.core.common.notnull', 'pandas.core.reshape.get_dummies',
233-
'pandas.tools.merge.concat', 'pandas.tools.merge.merge', 'pandas.tools.pivot.pivot_table',
234-
'pandas.tseries.tools.to_datetime', 'pandas.io.clipboard.read_clipboard', 'pandas.io.excel.ExcelFile.parse',
235-
'pandas.io.excel.read_excel', 'pandas.io.html.read_html', 'pandas.io.json.read_json',
236-
'pandas.io.parsers.read_csv', 'pandas.io.parsers.read_fwf', 'pandas.io.parsers.read_table',
237-
'pandas.io.pickle.read_pickle', 'pandas.io.pytables.HDFStore.append', 'pandas.io.pytables.HDFStore.get',
238-
'pandas.io.pytables.HDFStore.put', 'pandas.io.pytables.HDFStore.select', 'pandas.io.pytables.read_hdf',
239-
'pandas.io.sql.read_sql', 'pandas.io.sql.read_frame', 'pandas.io.sql.write_frame',
240-
'pandas.io.stata.read_stata']
241-
242-
html_additional_pages = {'generated/' + page: 'api_redirect.html' for page in moved_api_pages}
236+
('pandas.core.common.isnull', 'pandas.isnull'),
237+
('pandas.core.common.notnull', 'pandas.notnull'),
238+
('pandas.core.reshape.get_dummies', 'pandas.get_dummies'),
239+
('pandas.tools.merge.concat', 'pandas.concat'),
240+
('pandas.tools.merge.merge', 'pandas.merge'),
241+
('pandas.tools.pivot.pivot_table', 'pandas.pivot_table'),
242+
('pandas.tseries.tools.to_datetime', 'pandas.to_datetime'),
243+
('pandas.io.clipboard.read_clipboard', 'pandas.read_clipboard'),
244+
('pandas.io.excel.ExcelFile.parse', 'pandas.ExcelFile.parse'),
245+
('pandas.io.excel.read_excel', 'pandas.read_excel'),
246+
('pandas.io.html.read_html', 'pandas.read_html'),
247+
('pandas.io.json.read_json', 'pandas.read_json'),
248+
('pandas.io.parsers.read_csv', 'pandas.read_csv'),
249+
('pandas.io.parsers.read_fwf', 'pandas.read_fwf'),
250+
('pandas.io.parsers.read_table', 'pandas.read_table'),
251+
('pandas.io.pickle.read_pickle', 'pandas.read_pickle'),
252+
('pandas.io.pytables.HDFStore.append', 'pandas.HDFStore.append'),
253+
('pandas.io.pytables.HDFStore.get', 'pandas.HDFStore.get'),
254+
('pandas.io.pytables.HDFStore.put', 'pandas.HDFStore.put'),
255+
('pandas.io.pytables.HDFStore.select', 'pandas.HDFStore.select'),
256+
('pandas.io.pytables.read_hdf', 'pandas.read_hdf'),
257+
('pandas.io.sql.read_sql', 'pandas.read_sql'),
258+
('pandas.io.sql.read_frame', 'pandas.read_frame'),
259+
('pandas.io.sql.write_frame', 'pandas.write_frame'),
260+
('pandas.io.stata.read_stata', 'pandas.read_stata'),
261+
]
262+
263+
# Again, tuples of (from_old, to_new)
264+
moved_classes = [
265+
('pandas.tseries.resample.Resampler', 'pandas.core.resample.Resampler'),
266+
('pandas.formats.style.Styler', 'pandas.io.formats.style.Styler'),
267+
]
268+
269+
for old, new in moved_classes:
270+
# the class itself...
271+
moved_api_pages.append((old, new))
272+
273+
mod, classname = new.rsplit('.', 1)
274+
klass = getattr(importlib.import_module(mod), classname)
275+
methods = [x for x in dir(klass)
276+
if not x.startswith('_') or x in ('__iter__', '__array__')]
277+
278+
for method in methods:
279+
# ... and each of its public methods
280+
moved_api_pages.append(
281+
("{old}.{method}".format(old=old, method=method),
282+
"{new}.{method}".format(new=new, method=method))
283+
)
284+
285+
html_additional_pages = {
286+
'generated/' + page[0]: 'api_redirect.html'
287+
for page in moved_api_pages
288+
}
289+
290+
html_context = {
291+
'redirects': {old: new for old, new in moved_api_pages}
292+
}
243293

244294
# If false, no module index is generated.
245295
html_use_modindex = True

0 commit comments

Comments
 (0)