Skip to content

Commit 37f95ce

Browse files
chris-b1jorisvandenbossche
authored andcommitted
DOC: add source links to api docs (#14200)
1 parent 54ab5be commit 37f95ce

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

doc/source/conf.py

+51
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import sys
1414
import os
1515
import re
16+
import inspect
1617
from pandas.compat import u, PY3
1718

1819
# If extensions (or modules to document with autodoc) are in another directory,
@@ -47,6 +48,7 @@
4748
'sphinx.ext.coverage',
4849
'sphinx.ext.pngmath',
4950
'sphinx.ext.ifconfig',
51+
'sphinx.ext.linkcode',
5052
]
5153

5254

@@ -424,6 +426,55 @@ def get_items(self, names):
424426
return items
425427

426428

429+
# based on numpy doc/source/conf.py
430+
def linkcode_resolve(domain, info):
431+
"""
432+
Determine the URL corresponding to Python object
433+
"""
434+
if domain != 'py':
435+
return None
436+
437+
modname = info['module']
438+
fullname = info['fullname']
439+
440+
submod = sys.modules.get(modname)
441+
if submod is None:
442+
return None
443+
444+
obj = submod
445+
for part in fullname.split('.'):
446+
try:
447+
obj = getattr(obj, part)
448+
except:
449+
return None
450+
451+
try:
452+
fn = inspect.getsourcefile(obj)
453+
except:
454+
fn = None
455+
if not fn:
456+
return None
457+
458+
try:
459+
source, lineno = inspect.getsourcelines(obj)
460+
except:
461+
lineno = None
462+
463+
if lineno:
464+
linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1)
465+
else:
466+
linespec = ""
467+
468+
fn = os.path.relpath(fn, start=os.path.dirname(pandas.__file__))
469+
470+
if '+' in pandas.__version__:
471+
return "http://github.com/pydata/pandas/blob/master/pandas/%s%s" % (
472+
fn, linespec)
473+
else:
474+
return "http://github.com/pydata/pandas/blob/v%s/pandas/%s%s" % (
475+
pandas.__version__, fn, linespec)
476+
477+
427478
# remove the docstring of the flags attribute (inherited from numpy ndarray)
428479
# because these give doc build errors (see GH issue 5331)
429480
def remove_flags_docstring(app, what, name, obj, options, lines):

0 commit comments

Comments
 (0)