Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

link to github source w/ line number? #202

Closed
dzheng256 opened this issue May 3, 2020 · 4 comments
Closed

link to github source w/ line number? #202

dzheng256 opened this issue May 3, 2020 · 4 comments

Comments

@dzheng256
Copy link

dzheng256 commented May 3, 2020

Is it possible to use https://www.sphinx-doc.org/en/master/usage/extensions/linkcode.html to generate a github link including the specific line numbers for each function? Currently I've got it working more or less, but every function links to the github page for that module's source. I'm wondering if it is possible to have autoapi figure out the corresponding line in the file so I can include that in the github link.

Essentially in

def linkcode_resolve(domain, info):
    if domain != 'py':
        return None
    if not info['module']:
        return None
    filename = info['module'].replace('.', '/')
    return "https://somesite/sourcerepo/%s.py" % filename

which is the example from the above link, I guess info would have to include the line numbering

Thanks!

@AWhetter
Copy link
Collaborator

The viewcode support has to do something similar. You should be able to adapt what it does to work for the linkcode extension:

locations[full_name] = (
type_,
obj.obj["from_line_no"],
obj.obj["to_line_no"],
)

What it does isn't exactly part of the public API of AutoAPI but it'll do what you want.
Let me know how you get on.

@aaugustin
Copy link

In case that helps, I implemented a version this here:

https://github.com/aaugustin/websockets/blob/main/docs/conf.py#L61-L105

This implementation supports functions, classes, and methods, which is all I care about here.

@ArmageddonKnight
Copy link

aaugustin 's solution is pretty elegent. It is just the link appears to be out-of-date due to code changes. The permalink is here: https://github.com/aaugustin/websockets/blob/778a1ca6936ac67e7a3fe1bbe585db2eafeaa515/docs/conf.py#L100-L134

@kddubey
Copy link

kddubey commented Mar 28, 2023

@aaugustin's solution worked great! I just had to add that the object should be unwrapped, which I learned from sklearn here.

Add

obj = inspect.unwrap(obj)

right before getting the object's sourcefile

Edit: a hack I had to for my directory structure was

file = file.lstrip('../')

right before returning. I identified the issue by inspecting the source for the [source] link, which was missing the branch name 'main'

johnnv1 added a commit to johnnv1/kornia that referenced this issue Jan 12, 2024
This replaces the viewcode extension to use the linkcode extension.

Instead of showing the code on the docs, the `source` link will redirect to the github code.

Based on readthedocs/sphinx-autoapi#202
edgarriba pushed a commit to kornia/kornia that referenced this issue Jan 13, 2024
This replaces the viewcode extension to use the linkcode extension.

Instead of showing the code on the docs, the `source` link will redirect to the github code.

Based on readthedocs/sphinx-autoapi#202
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants