Debian Codegraph @

docs/community/setup.md

raw
1 # Get started with development
2
3 This section covers the simplest way to get started developing this theme locally so that you can contribute.
4 It uses automation and as few steps as possible to get things done.
5
6 If you are comfortable with and prefer a more manual setup refer to the [](topics/manual-dev.md) section.
7
8 ## Testing pre-release and nightly
9
10 You can test the alpha, beta and release candidates of the PyData Sphinx theme on your projects.
11 To do so install with pip using the `--pre` flag:
12
13 ```console
14 $ pip install --pre pydata-sphinx-theme
15 ```
16
17 If an `alpha`, `beta` or `rc` is available, pip will install it.
18
19 You can use the `--pre` flag in your project's continuous integration test suite
20 to catch regressions or bugs before their release.
21
22 If you are even more adventurous `pydata-sphinx-theme` has nightly builds, you can try following the
23 instructions provided [on the scientific-python/upload-nightly-action
24 README](https://github.com/scientific-python/upload-nightly-action?tab=readme-ov-file#using-nightly-builds-in-ci)
25 on installing nightly wheels.
26
27 Installing nightly wheels in your project's CI jobs is a great way to help theme developers catch bugs ahead of
28 time.
29
30 ## Workflow for contributing changes
31
32 We follow a [typical GitHub workflow](https://docs.github.com/en/get-started/using-github/github-flow)
33 of:
34
35 - create a personal fork and local clone of this repo
36 - create a branch for your contribution
37 - open a pull request
38 - fix findings of various linters and checks
39 - work through code review
40
41 For each pull request (PR), the documentation is built and deployed to make it easier to review the changes in the PR.
42 To access this preview, click on the {{ rtd }} preview in the CI/CD jobs (GitHub checks section
43 at the bottom of a PR, note you might need to click on "Show all checks" to access the job).
44
45 The sections below cover the contribution steps in more detail.
46
47 ## Clone the repository
48
49 First off you'll need your copy of the `pydata-sphinx-theme` codebase.
50 You can clone it for local development like so:
51
52 1. **Fork the repository**, so you have your own copy on GitHub.
53 See [the GitHub forking guide](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/fork-a-repo) for more information.
54 2. **Clone the repository locally** so that you have a local copy to work from:
55
56 ```console
57 $ git clone https://github.com/{{ YOUR USERNAME }}/pydata-sphinx-theme
58 $ cd pydata-sphinx-theme
59 ```
60
61 ## Install your tools
62
63 Building a Sphinx site uses a combination of Python and `Jinja` to manage `HTML`, `scss`, and `JavaScript`.
64 To simplify this process, we use a few helper tools:
65
66 - [The Sphinx Theme Builder](https://sphinx-theme-builder.readthedocs.io/en/latest/) compiles web assets in an automated way.
67 - [pre-commit](https://pre-commit.com/) for automatically enforcing code standards and quality checks before commits.
68 - [tox](https://tox.wiki/en/latest/index.html) for automating common development tasks.
69 - [pandoc](https://pandoc.org/) the universal document converter.
70
71 In particular, `tox` can be used to automatically create isolated local development environments with all the correct packages installed to work on the theme.
72 The rest of this guide focuses on using `tox` to start with a basic environment.
73
74 ```{seealso}
75 The information on this page covers the basics to get you started, for information about manually compiling assets, see [](topics/manual-dev.md).
76 ```
77
78 ### Setup `tox`
79
80 To start, install `tox`:
81
82 ```console
83 $ pip install tox
84 ```
85
86 You can call `tox` from the command line to perform common actions that are needed in building the theme.
87 `tox` operates with isolated environments, so each action has its packages installed in a local directory (`.tox`).
88 For common development actions, you'll only need to use `tox` and won't need to set up any other packages.
89
90 ### Setup `pre-commit`
91
92 `pre-commit` allows us to run several checks on the codebase every time a new Git commit is made.
93 This ensures standards and basic quality control for our code.
94
95 Install `pre-commit` with the following command:
96
97 ```console
98 $ pip install pre-commit
99 ```
100
101 then navigate to this repository's folder and activate it like so:
102
103 ```console
104 $ pre-commit install
105 ```
106
107 This will install the necessary dependencies to run `pre-commit` every time you make a commit with Git.
108
109 ```{note}
110 Your `pre-commit` dependencies will be installed in the environment from which you're calling `pre-commit`, `tox`, etc.
111 They will not be installed in the isolated environments used by `tox`.
112 ```
113
114 Alternatively, if you do not want to install pre-commit and its dependencies globally, you can use `tox` to run the checks:
115
116 ```python
117 tox -e run lint
118 ```
119
120 The caveat to using `tox` is that this will not install the required hooks to run the checks automatically before each commit, so you need to run this manually.
121
122 ## Build the documentation
123
124 Now that you have `tox` installed and cloned the repository, you will need to install Graphviz to build the documentation.
125 To install Graphviz, follow the instructions in the [Graphviz documentation for your operating system](https://graphviz.org/download/).
126
127 Once you have `tox` and Graphviz installed, you can build the documentation.
128 To build the documentation with `tox`, run the following command:
129
130 ```console
131 $ tox run -e docs-dev
132 ```
133
134 This will install the necessary dependencies and build the documentation located in the `docs/` folder.
135 The generated documentation will be placed in a `docs/_build/html` folder.
136 If the docs have already been built, it will only rebuild the pages that have been updated.
137 You can open one of the HTML files there to preview the documentation locally.
138
139 Alternatively, you can invoke the built-in Python [http.server](https://docs.python.org/3/library/http.server.html#module-http.server) with:
140
141 ```console
142 $ python -m http.server -d docs/_build/html/
143 ```
144
145 This will print a local URL that you can open in a browser to explore the HTML files.
146
147 You can also serve the documentation with live-reload using the following command:
148
149 ```console
150 $ tox run -e docs-live
151 ```
152
153 This command will build the documentation and watch for any changes to the `doc` folder and rebuild the documentation automatically.
154
155 ### Change content and re-build
156
157 Now that you've built the documentation, edit one of the source files to see how the documentation updates with new builds.
158
159 1. **Make an edit to a page**. For example, add a word or fix a typo on any page.
160 2. **Rebuild the documentation** with `tox run -e docs-dev`
161
162 It should go much faster this time because `tox` is re-using the previously created environment, and because Sphinx has cached the pages that you didn't change.
163
164 ## Compile the CSS/JS assets
165
166 The source files for CSS and JS assets are in `src/pydata_sphinx_theme/assets`.
167 These are then built and bundled with the theme (e.g., `scss` is turned into `css`).
168
169 To compile the CSS/JS assets with `tox`, run the following command:
170
171 ```console
172 $ tox run -e compile-assets
173 ```
174
175 This will compile all assets and place them in the appropriate folder to be used with documentation builds.
176
177 ```{note}
178 Compiled assets are **not committed to git**.
179 The `sphinx-theme-builder` will bundle these assets automatically when we make a new release, but we do not manually commit these compiled assets to Git history.
180 ```
181
182 ## Run a development server
183
184 You can combine the above two actions (build the docs and compile JS/CSS assets) and run a development server so that
185 changes to `src/` are automatically bundled with the package, and the documentation is immediately reloaded in a live preview window.
186
187 To run the development server with `tox`, run the following command:
188
189 ```console
190 # note the -m flag vs. other commands in this guide
191 $ tox run -m docs-live-server
192 ```
193
194 When working on the theme, making changes to any of these directories:
195
196 - `src/js/index.js`
197 - `src/scss/index.scss`
198 - `docs/**/*.rst`
199 - `docs/**/*.md`
200 - `docs/**/*.py`
201
202 will cause the development server to do the following:
203
204 - bundle/copy the CSS, JS, and vendored fonts
205 - regenerate the Jinja2 macros
206 - re-run Sphinx
207
208 ## Run the tests
209
210 This theme uses `pytest` and `playwright` for testing. There is a lightweight fixture defined
211 in the `test_build.py` script that makes it straightforward to run a Sphinx build using
212 this theme and inspect the results. There are also several automated accessibility checks in
213 `test_a11y.py`.
214
215 ```{warning}
216 Currently, the automated accessibility tests check the Kitchen Sink page only.
217 We are working on extending coverage to the rest of the theme.
218 ```
219
220 In addition, we use
221 [pytest-regressions](https://pytest-regressions.readthedocs.io/en/latest/) to
222 ensure that the HTML generated by the theme is what we'd expect. This module
223 provides a `file_regression` fixture that will check the contents of an object
224 against a reference file on disk. If the structure of the two differs, then the
225 test will fail. If we _expect_ the structure to differ, then delete the file on
226 disk and run the test. A new file will be created, and subsequent tests will
227 pass.
228
229 To run the build tests with `tox`, run the following command:
230
231 ```console
232 # this will compile the assets and run the tests (with test coverage)
233 # note the use of the `-m` flag vs. other commands in this guide
234 $ tox run -m tests
235
236 # to run the tests only without pre-compiling the assets and without coverage (for example if you recently compiled the assets)
237 $ tox run -e tests-no-cov
238 ```
239
240 To run the accessibility checks:
241
242 ```console
243 # this will compile the assets, build the documentation, and run the accessibility tests
244 $ tox run -m a11y
245
246 # to run the tests without pre-compiling the assets and without re-building the docs (for example if you recently compiled the assets or built the docs)
247 $ tox run -e a11y-tests
248 ```
249
250 ## GitHub Codespaces
251
252 If you have good internet connectivity and want a temporary set-up, it is often faster to work on the PyData Sphinx Theme
253 in a Codespaces environment.
254 Once your Codespaces instance is set up, you can run the `tox` commands above to build the documentation, compile the assets, and run the tests.
255 For documentation on how to get started with Codespaces, see [the Codespaces documentation](https://docs.github.com/en/codespaces).