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

DOC [PST] conf, setup, general styling #28132

Merged
merged 22 commits into from
Jan 31, 2024

Conversation

Charlie-XIAO
Copy link
Contributor

@Charlie-XIAO Charlie-XIAO commented Jan 15, 2024

Towards #28084. Please understand that in order to keep this PR as small as possible, it only serves as a foundation and does not really aim at making anything pretty. For instance, the landing page looks absolutely disastrous at this moment, but it is meant to be fixed in some further PR.

Here is the list of things left to be done in this PR (if any). Maintainers please feel free to modify.

Ping @glemaitre: this PR is ready for the first round of review.

Copy link

github-actions bot commented Jan 15, 2024

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 93ada41. Link to the linter CI: here

@Charlie-XIAO
Copy link
Contributor Author

Charlie-XIAO commented Jan 15, 2024

Why pinning pydata-sphinx-theme<0.15.0?

pydata-sphinx-theme 0.15.0 breaks the top navbar. To get a better idea you can compare the user guide page of pydata-sphinx-theme documentation in 0.14.4 and 0.15.1 (stable) and scroll down. The problem has been fixed in pydata/pydata-sphinx-theme#1630 but there has not been a new release yet. I'm thinking about directly bumping minimum version of pydata-sphinx-theme to 0.15.2 when it is released. Another option might be to specify pydata-sphinx-theme!=0.15.0,!=0.15.1?

pydata-sphinx-theme 0.15.2 has been released, thus I am currently not pinning pydata-sphinx-theme but limiting the minimum version to 0.15.2 instead.

Why sphinxcontrib.sass and why under pip?

Though I'm no expert at scss (in particular there are plenty of syntaxes of scss that I do not know), I loved its support for nested syntax (i.e., selectors sharing the same "prefix" can be nested). This not only reduces the lines of codes but also makes the structure much clearer (compared with using comments to separate different parts).

To convert scss files to css that can be directly used by sphinx, sphinxcontrib.sass is the only extension that I found (the other one cannot be installed directly from PyPI). Also sphinxcontrib.sass does not seem to be available through conda thus placed under pip dependencies.

Well, this is after all my personal taste. If maintainers do not like the scss or do not think the benefits of scss is worth the overhead of setting these stuff up, please feel free to let me know.

Why the add_js_css_files function?

By app.connect("html-page-context", add_js_css_files) I trigger this function just before the HTML for an individual page is created, so that by doing something like

if pagename == "install"app.add_css_file("styles/install.css")
    app.add_js_file("scripts/install-instructions.js")

we will be able to add CSS/JS to only certain pages to avoid loading scripts and style sheets while not using them at all. This function is currently left empty, but in further PRs I will likely make use of it.

@Charlie-XIAO
Copy link
Contributor Author

Charlie-XIAO commented Jan 15, 2024

build_tools/doc/ and sklearn/_min_dependencies.py

Adding pydata-sphinx-theme, sphinxcontrib.sass, and sphinx-remove-toctrees. The reason why pydata-sphinx-theme is pinned is explained in #28132 (comment). The reason why pydata-sphinx-theme has minimum version 0.15.2 is explained in #28132 (comment). The reason for using sphinxcontrib.sass is also in #28132 (comment). sphinx-remove-toctrees is for hiding the metadata routing section in the user guide from the toctree. It is recommended by pydata-sphinx-theme1, though not for this particular use case.

doc/scss/, doc/css/, and doc/js/

doc/js/ is the new path for keeping scripts, under which details-permalink.js is just an exact copy of the original one subject to formatting by prettier. doc/css/ is the new path for keeping style sheets. The scss files under doc/scss/ are converted to css files and placed under this directory when building the doc. The .gitkeep file is used to keep the doc/css/ directory there, otherwise sphinx will warn that we include a non-existent static path. The two style sheets under doc/scss/ are for general purpose thus created here in advance to avoid creating duplicated ones in further PRs.

.pre-commit-config

Add prettier into the pre-commit hooks for formatting JS and SCSS files. HTML files are not included because prettier does not seem to recognize jinja.

doc/Makefile and doc/make.bat

make clean should remove contents under doc/css/ as well, thus this change. For make.bat, the changes were to make it behave consistently with Makefile (though I'm not good at writing batch scripts either so I cannot guarantee it is 100% correct).

doc/contents.rst, doc/preface.rst, and doc/index.rst

The sections shown in the navbar are dependent on the toctree of root_doc (or at least I have not found out a way to let them be independent, except for manually overwriting the navbar-nav component). In short, the navbar takes the first header_links_before_dropdown sections in root_doc and the rest are placed in the navbar dropdown. Moreover, contents and preface are removed and index takes the job of contents, i.e., root_doc. More information please see #28132 (comment).

Footnotes

  1. PyData Theme - Build Performance and Size - Selectively Remove Pages From Your Sidebar

@Charlie-XIAO
Copy link
Contributor Author

Charlie-XIAO commented Jan 15, 2024

doc/conf.py

Here I will explain the each changed block in doc/conf.py in order.

  • Updated extensions, explained in the first part of #28132 (comment). Also reorganized a bit for clarity.
  • Changed root_doc from contents to index (and contents is removed). See #28132 (comment).
  • Specified encoding because Windows + Chinese = UnicodeDecodeError.
  • No need for pygments_style because pydata-sphinx-theme has specific configuration options for pygments in both light and dark modes and will override pygments_style.
  • Switched the theme and updated theme-specific configurations. I have included all options (only except those that are or will be deprecated) explicitly, in case pydata-sphinx-theme changes default values in a way we do not want. The options that I think worth mentioning already have comments so I will not repeat here, yet I may need to explain the options that are removed:
    • legacy_google_analytics and analytics: Replaced with the new analytics, hopefully equivalent but I have not validated yet.
    • mathjax_path: Emm I think this has no effect in the new setup. The mathjax_path variable is already sufficient to tell sphinx which MathJAX to use, and if NO_MATHJAX=1 the sphinx.ext.mathjax extension is not even included so should not matter either. Not sure if this analysis is correct.
    • link_to_live_contributing_page: Replaced with is_devrelease in html_context.
  • No need for html_theme_path since we are not using a customized theme.
  • No need for html_logo since pydata-sphinx-theme provides a more detailed and overriding configuration option.
  • Included doc/js/ and doc/css/ in html_static_path to make sure they are copied. doc/scss/ does not need to be copied because we only want its converted style sheets. Configured html_js_files and html_css_files correspondingly. sass_targets is used by sphinxcontrib.sass, see the example. Essentially we compile all scss into css, but we may include only a subset of the compiled css files, because some of them may be desired to only be included in certain files. As for the add_js_css_files function, it is explained in the third part of #28132 (comment).
  • Redirected contents and preface both to index.
  • Added is_devrelease to html_context, which is essentially the converse of the original link_to_live_contributing_page in html_theme_options.
  • Configuration for sphinx-remove-toctrees, hiding the metadata routing section in the user guide from the toctree.

@Charlie-XIAO Charlie-XIAO marked this pull request as ready for review January 15, 2024 21:43
Copy link
Member

@adrinjalali adrinjalali left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem has been fixed in pydata/pydata-sphinx-theme#1630 so we might be able to remove the pin soon.

Probably a good idea to keep pinning a specific version anyway.

Well, this is after all my personal taste. If maintainers do not like the scss or do not think the benefits of scss is worth the overhead of setting these stuff up, please feel free to let me know.

I'd like to see what @thomasjpfan and @GaelVaroquaux think on this. I personally don't know css at all anyway. The main question for me would be maintainability, in the sense that how many people know enough scss so that they can maintain this.

sphinx-remove-toctrees is for hiding the metadata routing section in the user guide from the toctree

Not sure why we need to do this.

Comment on lines +897 to +899
# Config for sphinx-remove-toctrees

remove_from_toctrees = ["metadata_routing.rst"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused as why.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because the current user guide hides this section from toctree, see screenshot below:

Screenshot of the current user guide page

image

See also @glemaitre's #26809 (comment):

Metadata routing has the wrong numbering in the "Section navigation" of the user guide. The reason is probably because it is supposed to be hidden in the old page.

@Charlie-XIAO
Copy link
Contributor Author

Charlie-XIAO commented Jan 16, 2024

Thanks for your review @adrinjalali!

The problem has been fixed in pydata/pydata-sphinx-theme#1630 so we might be able to remove the pin soon.

Probably a good idea to keep pinning a specific version anyway.

Emm yes this is also true, especially if we tweak css a lot. But on the other hand I do see some nice improvements in 1.5.x. So my personal opinion is, we can postpone the decision (whether to pin a specific version or not) when finalizing, depending on how many customizations we made at the end.

Well, this is after all my personal taste. If maintainers do not like the scss or do not think the benefits of scss is worth the overhead of setting these stuff up, please feel free to let me know.

I'd like to see what @thomasjpfan and @GaelVaroquaux think on this. I personally don't know css at all anyway. The main question for me would be maintainability, in the sense that how many people know enough scss so that they can maintain this.

Yep this is reasonable. I'm no expert either, thus only using the nested syntax but nothing else. If needed, here is an illustration:

index.scss for styling the landing page

/** This is the styling for the index pages, intended to retain the "iconic look" of
  * the scikit-learn homepage. Hate it or love it, it is memorable. */

div.sk-landing-container {
  max-width: 1400px;

  .text-white {
    text-shadow: 0px 0px 8px var(--sk-cyan-darker);
  }
}

/* Top bar */

div.sk-landing-top-bar {
  background-image: linear-gradient(
    160deg,
    var(--sk-cyan-darker) 0%,
    var(--sk-cyan) 17%,
    var(--sk-orange-lightest) 59%,
    var(--sk-orange-light) 100%
  );

  .sk-landing-header {
    font-size: 3.2rem;
    margin-bottom: 0.5rem;
  }

  .sk-landing-subheader {
    letter-spacing: 0.17rem;
    margin-top: 0;
  }

  a.sk-landing-btn {
    background-color: var(--sk-orange);
    color: var(--sk-text-dark);
    font-size: 1.1rem;
    font-weight: 500;

    &:hover {
      background-color: var(--sk-orange-light);
    }
  }

  ul.sk-landing-header-body {
    margin-top: auto;
    margin-bottom: auto;
    font-size: 1.2rem;
    font-weight: 500;
    color: var(--sk-text-dark);
  }
}

/* Body */

div.sk-landing-body {
  div.card {
    background-color: var(--pst-color-background);
    border-color: var(--pst-color-border);
  }

  .sk-px-xl-4 {
    @media screen and (min-width: 1200px) {
      padding-left: 1.3rem!important;
      padding-right: 1.3rem!important;
    }
  }

  .sk-card-title {
    font-weight: 700;
    margin-top: 0;
  }

  .sk-btn-primary {
    background-color: var(--sk-cyan-dark);
    border-color: var(--sk-cyan-dark);
    color: white;

    &:hover, &:active {
      background-color: var(--sk-cyan);
      border-color: var(--sk-cyan);
    }
  }

  img {
    max-width: 100%;
    height: unset!important;  // needed because sphinx sets the height

    .sk-index-img {
      max-height: 240px;
      margin: auto;
      margin-bottom: 1em;
      width: auto;

      @media screen and (min-width: 768px) {
        width: 100%;
      }
    }
  }
}

/* More info */

div.sk-landing-more-info {
  font-size: 0.96rem;

  html[data-theme="light"] & {
    background-color: var(--sk-background-dim-light);
  }

  html[data-theme="dark"] & {
    background-color: var(--sk-background-dim-dark);
  }

  .sk-landing-call-header {
    color: var(--sk-orange-dark);
    font-weight: 700;
    margin-top: 0;
  }

  ul.sk-landing-call-list > li {
    margin-bottom: 0.25rem;
  }

  a.sk-accent-btn {
    background-color: var(--sk-orange);
    border-color: var(--sk-orange);
    color: var(--sk-text-dark);

    &:hover {
      background-color: var(--sk-orange-light);
    }
  }

  .sk-who-uses-carousel {
    min-height: 200px;

    .carousel-item img {
      max-height: 100px;
      max-width: 50%;
    }
  }

  .sk-more-testimonials {
    text-align: right!important;
  }
}

/* Footer */

div.sk-landing-footer {
  a.sk-footer-funding-link {
    text-decoration: none;

    p.sk-footer-funding-text {
      color: var(--pst-color-link);

      &:hover {
        color: var(--pst-color-secondary);
      }
    }

    div.sk-footer-funding-logos > img {
      max-height: 36px;
      max-width: 80px;
      margin: 0 8px;
      margin-bottom: 8px;
    }
  }
}

sphinx-remove-toctrees is for hiding the metadata routing section in the user guide from the toctree

Not sure why we need to do this.

Addressed in #28132 (comment).

Copy link
Member

@adrinjalali adrinjalali left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my point of view this is good to go.

@Charlie-XIAO
Copy link
Contributor Author

Charlie-XIAO commented Jan 17, 2024

I pushed a few additional commits into this PR: 91d7371..3cdedf8. They do the following:

  • Remove contents and preface, and let them all redirect to index. The reason is, I really do not think anyone is navigating using those pages, especially since the section structure is directly accessible in the navbar (and its dropdown). More importantly, the fact that root_doc is not index, or more specifically that index is only in html_additional_pages, causes pydata-sphinx-theme to be unable to redirect the navbar logo correctly1. This is no longer an issue with the new structure.

    Note that however, we cannot remove index from html_additional_pages because the logic is, index.html is first generated from index.rst and then overwritten by templates/index.html.

  • Move the toctree (originally under content) to index, but hidden (may not be necessary, depending on how me modify templates/index.html. This is because index now serves as the root_doc. The reason for putting .. title:: Index is that otherwise the "previous" button in the article footer would show <no title> (something like that).

@adrinjalali Since these changes are made after your approval, I want to confirm your opinion with these new changes.

Footnotes

  1. pydata/pydata-sphinx-theme#1647

@adrinjalali
Copy link
Member

#28132 (comment) makes sense to me.

Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, I prefer scss over css, but I did not use it in scikit-learn because "its another thing to learn" for maintainers.

Currently, the landing page is not styled correctly. Is this expected?

@Charlie-XIAO
Copy link
Contributor Author

Charlie-XIAO commented Jan 17, 2024

Thanks for the feedback @thomasjpfan!

Generally, I prefer scss over css, but I did not use it in scikit-learn because "its another thing to learn" for maintainers.

Makes sense, but I still want to argue that, if only using the nested selectors, I think most people can understand within a few minutes. Admittedly things like mixins and control directives are not that intuitive, but I actually am not using those either. And after all I think not many contributors would even touch this part of code.

Of course, from another perspective, since I only uses nested rules when playing around it is easy for me to convert what I've done back into css, so either way works for me.

Currently, the landing page is not styled correctly. Is this expected?

Yes, I plan to fix it in a follow-up PR, hoping to keep this one as small as possible.

@Charlie-XIAO
Copy link
Contributor Author

Charlie-XIAO commented Jan 19, 2024

pydata-sphinx-theme 0.15.2 has been released a few hours ago. I'm trying to unpin and bump minimum version to 0.15.2 to see if things work as expected.

Update. Again there's something wrong: the primary sidebar would show up even when there is nothing to display. I will keep this as is because 0.15.2 contains some desired fixes, and hope to resolve the issue upstream so that we can bump again minimum to a higher version.

@glemaitre
Copy link
Member

I'm running a bit behind the regression of 1.4.0. But I promised that I'll take a look pretty soon.

@Charlie-XIAO
Copy link
Contributor Author

Charlie-XIAO commented Jan 26, 2024

Thanks and no hurry @glemaitre. I'm currently keeping stashes of further changes so actually I'm not completely blocked by this one.

@Charlie-XIAO
Copy link
Contributor Author

Charlie-XIAO commented Jan 29, 2024

So, after careful consideration I still included the general styling in this PR instead of leaving it to a further one, because when doing further work I found multiple patches relying on these. See the diff 5426549..21e369b (approximately 100 lines of additions but independent of other parts), and I will explain a bit more:

colors.scss is used for customized scikit-learn colors. The two base colors, --sk-cyan and --sk-orange, are taken from doc/logos/README.md and the rest (tints and shades) are automatically generated by this website. I'm really not sure what the best practice is here. Admittedly the only place where we use a lot of customized colors is the landing page. Only rarely may we use customized colors in other places (for instance, some buttons). Therefore, maybe we can just not care about a shared color sheet and directly write hex codes where needed. This needs further discussion.

custom.scss is mainly includes styles that override pydata-sphinx-theme defaults and scikit-learn-specific styles that may affect multiple pages.

Global

+code.literal {
+  border: 0;
+}
Before After
image image

Here I'm removing the border of inline code. This is in fact completely personal taste. The border may help distinguish inline code if background color is the same as that for inline code. However, I think the monospace font already suffices for such corner case. The main reason for me removing the border is that when there is a lot of such inline codes (especially on API pages) the borders are somehow very distracting for me. You may look at this page from pandas to see what I mean.

Primary sidebar

+.bd-sidebar-primary {
+  width: 22.5%;
+  min-width: 16rem;
+
+  // The version switcher button in the sidebar is ill-styled
+  button.version-switcher__button {
+    margin-bottom: unset;
+    margin-left: 0.3rem;
+    font-size: 1rem;
+  }
+
+  // The section navigation part is to close to the right boundary (originally an even
+  // larger negative right margin was used)
+  nav.bd-links {
+    margin-right: -0.5rem;
+  }
+}
Before After
image image
image image

Previously the primary sidebar seems a bit too large (taking 25% of page width) and I reduced a bit. Also the arrows (if exist) in the primary sidebar are too close to the boundary from my perspective, so I decrease the negative right margin (originally -1rem).

The version switcher problem happens for mobile screen size, where there is a strange bottom margin. I also moved it away a bit from other icons and decreased the font size because of its large border. Please do not mind the "Other versions and download" section overflowing out of the sidebar. My current plan is to remove this section and integrate it into the version switcher.

Article content

+.bd-article {
+  h1 {
+    font-weight: 500;
+    margin-bottom: 2rem;
+  }
+
+  h2 {
+    font-weight: 500;
+    margin-bottom: 1.5rem;
+  }
+
+  // Avoid changing the aspect ratio of images; add some padding so that at least
+  // there is some space between image and background in dark mode
+  img {
+    height: unset !important;
+    padding: 1%;
+  }
+
+  // Override the pydata-sphinx-theme styling of .. topic:: directives
+  div.topic,
+  aside.topic {
+    background-color: transparent;
+    border: none;
+    padding: 0;
+    box-shadow: none !important;
+
+    p {
+      color: var(--pst-color-text-base) !important;
+    }
+
+    ul.simple {
+      padding-left: 2rem;
+    }
+  }
+}
Before After
48208dd86fdcc9f2b27d3d0cea63592 0e10387cf2a30fa51434ac79a025c26
image image

A little bit restyling of the top two levels of headings to make them stand out. Again just some personal taste. Unsetting image height in the main article content is critical because otherwise the aspect ratio might be changed, but I'm not sure if this selector is too general (even if limiting only within the article body). The little padding used for dark mode: even if images themselves may not fit well into dark mode at least there's a small padding for transition (because pydata-sphinx-theme seems to set the background).

The styling of the .. topic:: directive is mainly because the old theme does not have a background for it. In particular, we sometimes have very long topics which may even include other directives that have background, making things weird (well, though dropdowns also have background on top of background). If maintainer think this change is unnecessary and we are happy with the default topic styling of pydata-sphinx-theme, feel free to let me know so I can revert.

Customized buttons

+a.btn {
+  &.sk-btn-orange {
+    background-color: var(--sk-orange-tint-1);
+    color: black !important;
+
+    &:hover {
+      background-color: var(--sk-orange-tint-3);
+    }
+  }
+
+  &.sk-btn-cyan {
+    background-color: var(--sk-cyan-shades-2);
+    color: white !important;
+
+    &:hover {
+      background-color: var(--sk-cyan-shades-1);
+    }
+  }
+}
+      color: var(--pst-color-text-base) !important;
+    }
+
+    ul.simple {
+      padding-left: 2rem;
+    }
+  }
+}
Dark Dark hover Light Light hover
image image image image
image image image image

Some buttons. The landing page uses both the orange and cyan buttons. The about page uses an orange button. I did not yet find another place where we may want to use this, so not sure if it is proper to put it in the general styling sheet. The colors are chosen such that (1) they are close to the colors originally used on the landing page, and (2) they look well both in light and dark modes.

This may also serve as an example of how I intend to use scss (mainly just the nested syntax, possibly very simple mixin when useful).

@Charlie-XIAO Charlie-XIAO changed the title DOC switch to pydata-sphinx-theme [1] conf and setup DOC switch to pydata-sphinx-theme [1] conf, setup, general styling Jan 29, 2024
@thomasjpfan
Copy link
Member

thomasjpfan commented Jan 29, 2024

Yes, I plan to fix it in a follow-up PR, hoping to keep this one as small as possible.

Historically, I do not think we ever made the dev landing page unusable. With this PR, the nav bar on the landing page is also gone, so it is a bit harder to navigate to the docs from the landing page.

I can see two ways forward:

  1. Update the HTML and CSS landing page and make it look nice.
  2. Add the nav bar (doc/themes/scikit-learn-modern/nav.html) to doc/templates/index.html so the landing page is at least functional. Have a new WIP PR that finishes the migration. We will have to merge the new PR fairly quickly.
  3. Keep the landing page broken, but also have a new WIP PR that builds on top of this PR and updates the landing page. We will have to merge the new PR fairly quickly.

@Charlie-XIAO
Copy link
Contributor Author

Charlie-XIAO commented Jan 30, 2024

@thomasjpfan Thanks for the follow up. I see what you mean.

  1. Personally I do not prefer this one since it will add quite a lot to this PR. But if you think this is the best I indeed have local changes ready that can be moved here directly.

  2. This is reasonable, and I'm thinking of a simpler alternative: since I'm now having the index.rst I can temporarily remove index from html_additional_pages so that it will not get overridden by our customized index template. This way we can get the nabvar as well.

    With this change the landing page should look literally empty:

    image

Historically, I do not think we ever made the dev landing page unusable.

This is targeting the new_web_theme branch, so it will not really be deployed to scikit-learn.org as long as we do not merge into main, if I'm not mistaken.

Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not notice that this was on a new_web_theme branch.

Ah I see. Okay, lets go with this PR.

@thomasjpfan thomasjpfan merged commit 7bcadac into scikit-learn:new_web_theme Jan 31, 2024
22 checks passed
@Charlie-XIAO Charlie-XIAO deleted the pydata-1-conf branch January 31, 2024 23:58
@Charlie-XIAO
Copy link
Contributor Author

Thanks @thomasjpfan for merging. I will make the follow up PRs soon.

@Charlie-XIAO Charlie-XIAO changed the title DOC switch to pydata-sphinx-theme [1] conf, setup, general styling DOC [PST] conf, setup, general styling Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants