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

Add examples where this function is used in docs #237

Open
chaithyagr opened this issue Feb 24, 2025 · 4 comments
Open

Add examples where this function is used in docs #237

chaithyagr opened this issue Feb 24, 2025 · 4 comments
Labels
documentation Improvements or additions to documentation

Comments

@chaithyagr
Copy link
Member

We currently have a lot of examples that it could be hard to know an example exists which calls a specific function of interest.
It could help a lot to add links to examples where a particular function is used. This should be automatic, as I see it in matplolib and scikit learn.

ChatGPT gave this:

import os
import glob

# Enable required Sphinx extensions
extensions = [
    "sphinx.ext.autodoc",
    "sphinx.ext.napoleon",
    "sphinx.ext.linkcode",  # Enables linking to external sources
]

EXAMPLES_DIR = "examples"  # Path where your example scripts are stored

def find_examples(func_name):
    """Search example scripts for function usage."""
    examples = []
    for file in glob.glob(os.path.join(EXAMPLES_DIR, "*.py")):
        with open(file, "r") as f:
            content = f.read()
            if func_name in content:
                # Convert file path to a Sphinx doc reference
                doc_link = f":doc:`{file.replace('.py', '')}`"
                examples.append(doc_link)
    return examples

def link_examples(app, what, name, obj, options, lines):
    """Automatically inject example references in docstrings."""
    examples = find_examples(name)
    if examples:
        lines.append("\n**Examples:**")
        lines.extend(["- " + e for e in examples])

def setup(app):
    app.connect("autodoc-process-docstring", link_examples)
@chaithyagr chaithyagr added the documentation Improvements or additions to documentation label Feb 24, 2025
@paquiteau
Copy link
Member

Sphinx Gallery has everything already:)

https://sphinx-gallery.github.io/stable/configuration.html#references-to-examples

@chaithyagr
Copy link
Member Author

Wonderful, it could also help to have forward references to API in examples. I think this is fairly easy, just that we should not linik it to numpy or scipy docs which will lead to a lot of confusion in links.

@Daval-G
Copy link
Collaborator

Daval-G commented Feb 24, 2025

For trajectories I was planning to move all initializations into files by groups. I can add references to examples at this occasion, a few at a time

@chaithyagr
Copy link
Member Author

Nah, i think it should be automatic, don't waste your time manually doing it. I think as @paquiteau mentioned we should be able to do it easily

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants