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

Use sphinx-theme-builder to build package #419

Merged
merged 8 commits into from
Jun 28, 2023

Conversation

Eric-Arellano
Copy link
Collaborator

@Eric-Arellano Eric-Arellano commented Jun 26, 2023

sphinx-theme-builder allows us to use JavaScript/NPM in our Python packaging pipeline. Whereas right now we have to write raw CSS and JavaScript files, we can now pre-process those files.

sphinx-theme-builder automates installing Node and NPM for users. It runs npm run build to pre-compile our code, which for now simply copies our CSS with cp to keep the code simple.

sphinx-theme-builder gives some benefits:

  1. Minify our CSS and JS, which is important for site loading performance. Minify theme assets? #283
  2. No performance hit to splitting our CSS into multiple files
  3. We can use Sass, which is more maintainable than CSS
  4. We can more easily vendor and tweak Furo's JS code, which allows fixing Furo: anchor links still broken for page table of contents #368

Changes folder structure

Before:

src
└── qiskit_sphinx_theme
    ├── ecosystem
    ├── furo  # the core qiskit theme
    └── pytorch

After:

src
└── qiskit_sphinx_theme
    ├── assets  # Sass and JS for core qiskit theme
    ├── ecosystem
    ├── pytorch
    └── theme  # the core qiskit theme

Eric-Arellano added a commit that referenced this pull request Jun 27, 2023
Python's official [packaging
guide](https://packaging.python.org/en/latest/tutorials/packaging-projects/)
now recommends using the `src/` layout, rather than having a top level
module folder called `qiskit_sphinx_theme`. For example, this layout
works Out Of The Box with setuptools—we no longer need to explicitly set
`packages` in `setup.py`.

This change unblocks us from migrating to qiskit-sphinx-theme in
#419 because it
expects projects to use the standard `src` layout.

This has no impact on end-users. It only impacts how we layout our code.
When running `python3 -m build`, the final result will strip out the
`src/`.

## Also renames top-level folders

Before:

```
qiskit_sphinx_theme
├── __init__.py
├── furo
│   ├── base  # the core qiskit theme
│   └── ecosystem
└── pytorch
```

After:

```
src
└── qiskit_sphinx_theme
    ├── ecosystem
    ├── furo  # the core qiskit theme
    └── pytorch
```

When I first set up the folders two months ago, I thought that we would
have Theme Variants with Pytorch.

I also didn't realize that the Ecosystem Furo theme variant will be so
heavily based on the core Furo Qiskit theme; that is, we won't have
`furo/base`, `furo/qiskit`, and `furo/ecosystem`. We'll simply have
something like `qiskit/` and `ecosystem/`, with the Ecosystem theme
based directly off of the Qiskit theme.
Eric-Arellano added a commit that referenced this pull request Jun 27, 2023
`pyproject.toml` is now the standardized way to set packaging metadata
for Python packages. When possible, setuptools recommends using it
rather than a dynamic `setup.py` time.

Switching to `pyproject.toml` specifically unblocks
#419.

Another benefit is we can deduplicate setting the version in two places.

## Gets rid of "universal" wheel

We were incorrectly claiming in `setup.cfg` that the package is
"universal", which means it works with both Python 2 and Python 3. That
is a lie. We don't support Python 2.
@Eric-Arellano Eric-Arellano changed the title [proof of concept] Use sphinx-theme-builder to build package Use sphinx-theme-builder to build package Jun 28, 2023
@Eric-Arellano Eric-Arellano marked this pull request as ready for review June 28, 2023 00:57
Comment on lines +20 to +21
- name: Fix permissions for sphinx-theme-builder
run: chown -R $(id -u):$(id -g) .
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ChatGPT 🙇

package.json Outdated Show resolved Hide resolved
Comment on lines +70 to +71
def activate_themes(app: sphinx.application.Sphinx, config: sphinx.config.Config) -> None:
if config.html_theme == "_qiskit_furo":
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So, apparently, the old code I had didn't actually work. Oops. The config.html_theme was always alabaster because it was being evaluated too early.

This works!

Comment on lines -91 to -101
from furo import (
WrapTableAndMathInAContainerTransform,
_builder_inited,
_html_page_context,
_overwrite_pygments_css,
)

app.add_post_transform(WrapTableAndMathInAContainerTransform)
app.connect("html-page-context", _html_page_context)
app.connect("builder-inited", _builder_inited)
app.connect("build-finished", _overwrite_pygments_css)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Apparently, none of this is necessary. It gets called already because our Furo theme inherits from Furo.

# Our stylesheet takes precedence.
stylesheet =
styles/furo.css,
styles/furo-extensions.css,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We don't need to explicitly register furo-extensions.css apparently. It already gets registered by Furo, thanks to us inheriting Furo.

frankharkins

This comment was marked as outdated.

Copy link
Member

@frankharkins frankharkins left a comment

Choose a reason for hiding this comment

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

Looks good! The Furo theme works correctly for me, and I didn't notice any problems with the diff. Will obviously need review from someone with more sphinx experience than me though.

@@ -102,13 +106,13 @@ To update the top nav bar web component:
1. In https://github.com/Qiskit/web-components, run `npm install` then `npm run build`.
2. There should be a file created at the root of the web components repository called `experimental-bundled-ui-shell.js`. Copy its contents into these files in this theme repository:
1. `src/qiskit_sphinx_theme/pytorch/static/js/web-components/top-nav-bar.js`
2. `src/qiskit_sphinx_theme/furo/static/js/web-components/top-nav-bar.js`
2. `src/qiskit_sphinx_theme/theme/qiskit-sphinx-theme/static/js/web-components/top-nav-bar.js`
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have a general q about the new folder structure. src/qiskit_sphinx_theme/theme/qiskit-sphinx-theme/etc feels very long and repetitive, do we really need so many subfolders? Could it be simplified to src/qiskit_sphinx_theme/theme/etc or something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

100% agreed. But yes, sphinx-theme-builder is extremely opinionated

Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm that's annoying 🤔 would it be an option to remove a folder layer higher up? So have something like this:

src
 ├── assets 
 ├── ecosystem
 ├── pytorch
 └── theme 
      └── qiskit-sphinx-theme

or would that mess things up as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That would break how Python packages work. We could no longer import qiskit_sphinx_theme. And I think sphinx-theme-builder would complain also too hah


### How to change CSS
Make CSS changes in the file `qiskit_changes.css`. It takes precedence over any CSS rules from Furo.
Make CSS changes in the file `assets/styles/qiskit-sphinx-theme.css`. It takes precedence over any CSS rules from Furo.
Copy link
Collaborator

Choose a reason for hiding this comment

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

is it a requirement that the css file is called qiskit-sphinx-theme? I quite liked the qiskit_custom.css bc it easily implied that this was where stuff differed from base Furo

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. sphinx-theme-builder is extremely opinionated

Copy link
Collaborator

@javabster javabster left a comment

Choose a reason for hiding this comment

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

LGTM, just left one more question about the folder structure 😅

@Eric-Arellano Eric-Arellano merged commit 2a646ec into Qiskit:main Jun 28, 2023
4 checks passed
@Eric-Arellano Eric-Arellano deleted the sphinx-theme-builder branch June 28, 2023 17:02
Eric-Arellano added a commit that referenced this pull request Jul 17, 2023
Closes #496.

#419 accidentally
broke Pytorch because it apparently doesn't work to call
`app.setup_extension` inside the `config-inited` event. So, we were no
longer activating jQuery by default.
Eric-Arellano added a commit to Eric-Arellano/qiskit_sphinx_theme that referenced this pull request Jul 17, 2023
Closes Qiskit#496.

Qiskit#419 accidentally
broke Pytorch because it apparently doesn't work to call
`app.setup_extension` inside the `config-inited` event. So, we were no
longer activating jQuery by default.
Eric-Arellano added a commit that referenced this pull request Jul 17, 2023
Closes #496.

#419 accidentally
broke Pytorch because it apparently doesn't work to call
`app.setup_extension` inside the `config-inited` event. So, we were no
longer activating jQuery by default.
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

Successfully merging this pull request may close these issues.

3 participants