Skip to content

How do you use the right "Edit in Github" url for generated files? #1269

Open

Description

@drammock

Discussed in #1266

Originally posted by vnmabus March 24, 2023
For generated files, such as examples generated with sphinx-gallery or API reference pages generated with autosummary, the "Edit in Github" link generates a 404 error.

It is possible to redirect them to the original file somehow? In that case, how?

Activity

  1. pllim commented on Jul 10, 2023

    @pllim
    Contributor

    (Thanks for the tips! I am re-posting here so I can find it in the future.)

    @OriolAbril

    In case it helps, we use the custom template approach for ArviZ: https://github.com/arviz-devs/arviz/blob/main/doc/_templates/edit-this-page.html. It should be the same as the one in the pydata-sphinx-theme repo but with the extra "generated" not in sourcename bit, we always use "generated" as toctree dir for autosummary. This way all pages get the button (e.g. https://python.arviz.org/en/stable/user_guide/label_guide.html) except for api generated ones (e.g. https://python.arviz.org/en/stable/api/generated/arviz.plot_posterior.html)

  2. drammock commented on Jul 11, 2023

    @drammock
    MemberAuthor

    @pllim there's also a very old issue #477 that has a couple other approaches.

  3. SergejKr commented on Oct 26, 2023

    @SergejKr

    I use autosummary and would like to link to the source code rather than the automatically generated files. Any progress on this topic?

  4. 12rambau commented on Oct 26, 2023

    @12rambau
    Collaborator

    @Nauin2 whatever lib you use to generate API documentation (auto-api or autosummary) the mechanism is the same:
    the extention build files on the fly in .rst that will then be interpreted at build time. I don't think I have access to source file at any step so I don't have any way to reach the github source file.

    As a workaround you can still use the solution referenced in #477 by building a specific directive that will override the link using javascript.

    What we are missing here is a good idea to capture the source file information whatever the VCS system (github, gitlab, gitea...).

  5. added
    help wantedExtra attention is needed
    tag: componentIssues or improvements associated with a given component in the theme
    on Oct 26, 2023
  6. SergejKr commented on Oct 26, 2023

    @SergejKr

    If I understand correctly, you need a link from the autogenerated .rst file to the source code, which could be anywhere in the repository.

    In case of autosummary, it could be possible to alter the template with which the .rst files are generated and save the file directory within the .rst.

  7. 12rambau commented on Oct 26, 2023

    @12rambau
    Collaborator

    yes but it will be a special case solution, we won't be able to generalize it to all our userbase as we cannot edit the template from our end.

  8. SergejKr commented on Oct 26, 2023

    @SergejKr

    Okay. Maybe another idea, again autosummary specific. The names of the autogenerated .rst files follow the dot notation of python. Using, e.g., the getfile function of the inspect module the filepath to the source could be retrievable.

  9. 12rambau commented on Oct 26, 2023

    @12rambau
    Collaborator

    If someone changes the api folder name or if the template is already altered by the user himself I will have no guarantee that the hack will work. This is a theme, we don't adapt files generated by other extensions it would be impossible to keep up with their release cycle.

    what could be done is:

    • you change the template on your side
    • I help you build a directive with few parameters
    • we document it in the "extend this theme section"

    So it will force user to code something yes but it will be useful for all user whatever the api extention they are using. what do you think ?

  10. SergejKr commented on Oct 26, 2023

    @SergejKr

    Sure, we can try this approach.

  11. pllim commented on Dec 23, 2023

    @pllim
    Contributor

    Re: #1269 (comment)

    @drammock , before I jump into the rabbit hole for this one, has there been any progress that would supersede the comment linked above?

    p.s. Happy holidays!

  12. drammock commented on Dec 23, 2023

    @drammock
    MemberAuthor

    AFAIK no real progress. There is an open PR that allows to set the secondary sidebar templates on a per-page basis, which might make it easy to simply omit the source link on generated pages (using a glob pattern). But no progress on getting them to link to the actual source files

  13. pllim commented on Sep 20, 2024

    @pllim
    Contributor

    Is there a way to disable the edit but only for generated pages? 🤔

  14. vivodi commented on Mar 21, 2025

    @vivodi
    Contributor

    Let me summarize: There are three methods currently available.

    1. The first method hides the edit button for automatically generated APIs: How do you use the right "Edit in Github" url for generated files? #1269 (comment). The downside is that it hides the link and does not point to the source file corresponding to the API. In fact, it merely avoids the problem.
    2. The second method dynamically modifies the webpage at runtime using JavaScript: How do you use the right "Edit in Github" url for generated files? #1266 (comment). This approach is overly complex and introduces unnecessary runtime overhead.
    3. The third method generates the correct links during Sphinx's HTML build process, as used by the author of Suggested way to customize "Edit this page" link based on document #477. This approach ensures correct links are generated at build time, avoids unnecessary runtime overhead, and is relatively simple. I recommend this method the most, and I think it's perfect.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is neededkind: enhancementNew feature or requesttag: componentIssues or improvements associated with a given component in the theme

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions