Skip to content

Commit e379356

Browse files
svekarsfacebook-github-bot
authored andcommitted
Add docs README.md and a custom directive (#208)
Summary: Pull Request resolved: #208 Add a basic README for the documentation folder that gives a basic overview of how to build locally and contribute, as well as provide tips on how to author better pages with MyST parser. Add a custom Sphinx directive that adds a code block with a nightly PyTorch version from the nightly.txt file. Reviewed By: mergennachin, dbort Differential Revision: D48757229 fbshipit-source-id: 6a83d46ae65a694bcc24bced88d4a999b998a94b
1 parent b2fa508 commit e379356

File tree

9 files changed

+394
-145
lines changed

9 files changed

+394
-145
lines changed

.ci/docker/requirements-ci.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ matplotlib
1717
-e git+https://github.com/pytorch/pytorch_sphinx_theme.git#egg=pytorch_sphinx_theme
1818
myst-parser
1919
sphinx_design
20+
sphinx-copybutton

docs/README.md

Lines changed: 126 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,134 @@
1-
# Contributing to the Executorch Website
1+
# ExecuTorch Documentation
22

3-
# Layout
3+
Welcome to the ExecuTorch documentation! This README.md will provide an
4+
overview of the ExecuTorch docs and its features, as well as instructions on
5+
how to contribute and build locally.
46

5-
Directory structure:
7+
<!-- toc -->
8+
9+
- [Toolchain Overview](#toolchain-overview)
10+
- [Building Locally](#building-locally)
11+
- [Using Custom Variables](#using-custom-variables)
12+
- [Including READMEs to the Documentation Build](#including-readmes-to-the-documentation-build)
13+
- [Contributing](#contributing)
14+
- [Auto-generated API documentation](#auto-generated-api-documentation)
15+
- [Python APIs](#python-apis)
16+
- [C++ APIs](#c-apis)
17+
18+
<!-- tocstop -->
19+
20+
21+
## Toolchain Overview
22+
23+
We are using [sphinx](https://www.sphinx-doc.org/en/master/) with
24+
[myst_parser](https://myst-parser.readthedocs.io/en/latest/),
25+
[sphinx-gallery](https://sphinx-gallery.github.io/stable/index.html), and
26+
[sphinx_design](https://sphinx-design.readthedocs.io/en/latest/) in this
27+
documentation set.
28+
29+
We support both `.rst` and `.md` files but prefer the content to be authored in
30+
`.md` as much as possible.
31+
32+
## Building Locally
33+
34+
Documentation dependancies are stored in
35+
[.ci/docker/requirements-ci.txt](https://github.com/pytorch/executorch/blob/main/.ci/docker/requirements-ci.txt).
36+
37+
To build the documentation locally:
38+
39+
1. Clone the executorch repo to your machine.
40+
41+
1. Switch to the `docs/` directory.
42+
43+
1. Start a virtual environment:
44+
45+
```bash
46+
virtualenv venv
47+
source venv/bin/activate
48+
```
49+
50+
1. Install dependancies:
51+
52+
```bash
53+
pip3 install -r ../.ci/docker/requirements-ci.txt
54+
```
55+
56+
1. Build the documentation set:
57+
58+
```
59+
make html
60+
```
61+
62+
This should build both documentation and tutorials. The build will be
63+
placed in the `_build` directory.
64+
65+
1. You can preview locally by using [sphinx-serve](https://pypi.org/project/sphinx-serve/).
66+
To install sphinx-serve, run: `pip3 install sphinx-serve`. To serve
67+
your documentation:
68+
69+
```
70+
sphinx-serve -b _build
71+
```
72+
73+
Open http://0.0.0.0:8081/ in your browser to preview your updated
74+
documentation.
75+
76+
## Using Custom Variables
77+
78+
You can use custom variables in your `.md` and `.rst` files.
79+
The variables take their values from the files listed in the
80+
`./.ci/docker/ci_commit_pins/` directory.
81+
For example, to insert a variable that specifies the latest Buck2 version,
82+
use the following syntax:
683

784
```
8-
docs
9-
│ README.md
10-
11-
└─── website
12-
│ │ docusaurus.config.json
13-
│ │ sidebar.js
14-
| └─── fb
15-
│ | └─── sidebar.js
16-
│ │ ...
17-
| └─── docs
18-
│ | .md files
19-
│ │ ...
20-
| └─── fb
21-
| └─── .md files
22-
23-
└─── source_py
24-
│ │ conf.py
25-
│ └─── .rst files
26-
|
27-
└─── source_cpp
28-
│ Doxyfile
29-
│ conf.py
30-
└─── index.rst files
85+
The current version of Buck2 is ${executorch_version:buck2}.
3186
```
3287

33-
The Executorch website is hosted on
34-
https://www.internalfb.com/intern/staticdocs/executorch/.
35-
36-
# General Documentation
37-
38-
General documentation can be written in Markdown and live within the
39-
`website/docs/` directory. Through Static Docs, the Markdown files will be
40-
automatatically rendered into a pretty static website on
41-
https://www.internalfb.com/intern/staticdocs/executorch/.
42-
43-
To add a page to the website, create a .md file under the `website/docs/`
44-
directory. If the page is internal-only, create the file under
45-
`website/docs/fb/`. Afterwards, go to `website/sidebars.js` or
46-
`website/fb/sidebars.js` (depending on if your document is public or
47-
internal-only) and add your document there so that it will show up on the
48-
sidebar.
49-
50-
To view your changes:
51-
1. `ssh -L 3000:localhost:3000 <devserver>` from your laptop to your devserver.
52-
2. `cd fbcode/executorch/docs/website`
53-
3. `yarn` to install any packages
54-
4. `yarn build` to generate static HTML files
55-
5. `yarn start-fb` to start a local development server and view the internal
56-
variant of the website at `localhost:3000`. `yarn start` to view the public
57-
variant of the website. Most changes are reflected live without having to
58-
restart the server.
59-
60-
Additionally, once you make changes to the website and create a diff containing
61-
your changes, a Static Docs Bot will automatically add an ephemeral version of
62-
the Static Docs website containing your changes to the test plan of the diff
63-
created. It may take around 10-20 minutes for the Static Docs Bot to populate
64-
the ephemeral site with your changes and update the test plan of the diff on
65-
Phabricator. It should look something like this:
66-
67-
![static_docs_preview](static_docs_preview.png)
68-
69-
# Auto-generated APIs
70-
71-
Auto-generated API documentation are currently generated into
72-
https://www.internalfb.com/intern/staticdocs/executorch/py_api/ for Python APIs,
73-
and https://www.internalfb.com/intern/staticdocs/executorch/cpp_api/ for C++
74-
APIs.
88+
This will result in the following output:
7589

76-
On a high level (will go into detail later), we use Sphinx to generate both
77-
Python and C++ documentation in the form of HTML pages. These pages are
78-
generated into `website/static` and are linked on the navigation bar (which is
79-
configured in `website/docusaurus.config.js`).
90+
<img src="./source/_static/img/s_custom_variables_extension.png" width="300">
91+
92+
We have the following custom variables defined in this docset:
93+
94+
* `${executorch_version:buck2}`
95+
* `${executorch_version:nightly}`
96+
* `${executorch_version:pytorch}`
97+
* `${executorch_version:vision}`
98+
* `${executorch_version:audio}`
99+
100+
101+
You can use the variables in both regular text and code blocks.
80102

81-
## Setup (in fbcode)
82103

83-
Since we are using Sphinx, which is an external library, we need to set up a
84-
`TARGETS` file within your working repository in order to run sphinx commands
85-
that you might typically be able to run just on the command line. Specifically
86-
the ones we used were:
104+
## Including READMEs to the Documentation Build
87105

88-
* [`sphinx-build`](https://www.sphinx-doc.org/en/master/man/sphinx-build.html) in order to build the Sphinx project and generate the HTML files
89-
* [`sphinx-apidoc`](https://www.sphinx-doc.org/en/master/man/sphinx-apidoc.html) in order to generate an initial set of Sphinx sources
106+
You might want to include some of the `README.md` files from various
107+
directories in this repositories in your documentation build.
108+
To do that, create an `.md` file and use the `{include}` directive to
109+
insert your `.md` files. Example:
90110

91-
A sample `TARGETS` file looks like:
92111
```
93-
python_binary(
94-
# @autodeps-skip
95-
name = "sphinx-build",
96-
main_module = "sphinx.cmd.build",
97-
deps = [
98-
"fbsource//third-party/pypi/breathe:breathe",
99-
"fbsource//third-party/pypi/exhale:exhale",
100-
"fbsource//third-party/pypi/pytorch-sphinx-theme:pytorch-sphinx-theme",
101-
"fbsource//third-party/pypi/sphinx:sphinx",
102-
],
103-
)
104-
105-
python_binary(
106-
# @autodeps-skip
107-
name = "sphinx-apidoc",
108-
main_module = "sphinx.ext.apidoc",
109-
deps = [
110-
"fbsource//third-party/pypi/pytorch-sphinx-theme:pytorch-sphinx-theme",
111-
"fbsource//third-party/pypi/sphinx:sphinx",
112-
],
113-
)
112+
```{include} ../README.md
114113
```
115114

116-
Any libraries that are used within your doc generation should be placed within
117-
the dependency list of the `sphinx-build` binary in the `TARGETS` file created.
118-
If the library you are using does not exist within `fbsource/third-party`,
119-
follow this
120-
[link](https://www.internalfb.com/intern/wiki/Third-Party_for_Megarepo/Python_(PyPI)/)
121-
to fork that library into `fbsource/third-party` so that you can import it here.
115+
**NOTE:** Many `README.md` files are written as placeholders with limited information
116+
provided. Some of that content you might want to keep in the repository rather
117+
than on the website. If you still want to add it, make sure to check
118+
the content for accuracy, structure, and overall quality.
122119

123-
## Generating the documentation
120+
## Contributing
121+
122+
Use the [PyTorch contributing guidelines](https://github.com/pytorch/pytorch/blob/main/CONTRIBUTING.md#writing-documentation)
123+
to contribute to the documentation.
124+
125+
In addition to that, see [Markdown in Sphinx Tips and Tricks](https://pytorch.org/executorch/markdown-sphinx-tips-tricks.html)
126+
for tips on how to author high-quality markdown pages with Myst Parser.
127+
128+
# Auto-generated API documentation
129+
130+
On a high level (will go into detail later), we use Sphinx to generate both
131+
Python and C++ documentation in the form of HTML pages.
124132

125133
### Python APIs
126134

@@ -132,8 +140,9 @@ The setup for Python documentation lies within `source_py/`. To set up Sphinx, a
132140
`conf.py` configuration file is required. We can specify ways to generate
133141
documentation here. Specifically, the most important/relevant parts are:
134142

135-
* Make sure to add a path to the directory above the directory you're trying to generate documentation for. For example, since we want to generate documenation for the `executorch/` directory, the `sys.path` is `.../fbsource/fbcode/`. This tells Sphinx where to find the code to generate docs for.
136-
* `extensions` contains extension modules. For auto-generating APIs make sure to include `sphinx.ext.autodoc`.
143+
* Make sure to add a path to the directory above the directory you're trying to generate documentation for. For example, since we want to generate documenation for the `executorch/` directory. This tells Sphinx where to find the code to generate docs for.
144+
145+
* `extensions` contains extension modules. For auto-generating APIs, make sure to include `sphinx.ext.autodoc`.
137146
* `autodoc_mock_imports` is where you put imports that Sphinx is unable to access. Sphinx runs your code in order to autogenerate the docs, so for any libraries that it unable to access due to it being outside of the directory, or containing c++ bindings, we need to specify it in the `autodoc_mock_imports` list. You can see what modules Sphinx is confused by when you run into importing errors when generating docs.
138147

139148
Additionally, RST files are needed in order to specify the structure of the
@@ -165,11 +174,10 @@ To view your changes on the executorch website, you can follow the same steps
165174
listed in the "General Documentation" section.
166175

167176
To view just the auto-generated pages:
168-
1. `ssh -L 8000:localhost:8000 <devserver>` from your laptop to your devserver.
169-
2. `cd fbcode/executorch/docs/`
170-
3. `buck2 run //executorch/docs:sphinx-build -- -M html source_py sphinxbuild_py`
177+
1. `cd executorch/docs/`
178+
2. `sphinx-build -M html source_py sphinxbuild_py`
171179
to build Sphinx and generate APIs any packages.
172-
4. `python3 -m http.server 8000 --directory sphinxbuild_py/html` to view your HTML
180+
3. `python3 -m http.server 8000 --directory sphinxbuild_py/html` to view your HTML
173181
files at `localhost:8000`.
174182

175183
### C++ APIs
@@ -203,35 +211,9 @@ structure can be found in `source_cpp/`.
203211
A diagram of how the files work together:
204212
![image](cpp_docs.png)
205213

206-
To view your changes on the executorch website, you can follow the same steps
207-
listed in the "General Documentation" section.
214+
To view the C++ API documentation locally, run:
208215

209-
To view just the auto-generated pages:
210-
1. `ssh -L 8000:localhost:8000 <devserver>` from your laptop to your devserver.
211-
2. `cd fbcode/executorch/docs/`
212-
3. `buck2 run //executorch/docs:sphinx-build -- -M html source_cpp sphinxbuild_cpp`
213-
to build Sphinx and generate APIs any packages.
214-
4. `python3 -m http.server 8000 --directory sphinxbuild_cpp/html` to view your HTML
216+
1. `cd executorch/docs/`
217+
2. `sphinx-build -M html source_cpp sphinxbuild_cpp`
218+
3. `python3 -m http.server 8000 --directory sphinxbuild_py/html` to view your HTML
215219
files at `localhost:8000`.
216-
217-
## Integration with Static Docs
218-
219-
To integrate the generated files with Static Docs, we created a `gen_docs.sh`
220-
script under `website/`. The script contains commands to generate HTML files
221-
into the `website/static/` folder, which are linked on the navigation bar (which
222-
is configured in `website/docusaurus.config.js`).
223-
224-
To have Static Docs run this script each time the website is built, we inserted
225-
a command to run this script into `website/package.json` under `scripts/build`:
226-
```
227-
{
228-
...
229-
"scripts": {
230-
"docusaurus": "docusaurus",
231-
"start": "docusaurus start",
232-
"build": "./gen_docs.sh && docusaurus build",
233-
...
234-
}
235-
...
236-
}
237-
```

docs/source/_static/example.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
"""
8+
A sample python file
9+
"""
10+
11+
12+
class Person:
13+
def __init__(self, name, age):
14+
self.name = name
15+
self.age = age
16+
17+
# start
18+
19+
def introduce(self):
20+
print("Hello, my name is", self.name)
21+
print("I am", self.age, "years old")
22+
23+
# end
24+
25+
26+
person = Person("Alice", 25)
27+
person.introduce()
Loading
13.1 KB
Loading
Loading

docs/source/conf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"sphinx.ext.autosummary",
5050
"sphinx.ext.coverage",
5151
"sphinx.ext.doctest",
52+
"sphinx_copybutton",
5253
"sphinx.ext.napoleon",
5354
"sphinx.ext.todo",
5455
"sphinx.ext.viewcode",
@@ -57,6 +58,7 @@
5758
"myst_parser",
5859
"sphinx_design",
5960
"sphinx_gallery.gen_gallery",
61+
"executorch_custom_versions",
6062
]
6163

6264
templates_path = ["_templates"]
@@ -128,7 +130,7 @@
128130
"torch": ("https://pytorch.org/docs/stable/", None),
129131
}
130132

131-
# Custom directives defintions to create cards on main TorchServe page
133+
# Custom directives defintions to create cards on main landing page
132134

133135
from custom_directives import (
134136
CustomCardEnd,

0 commit comments

Comments
 (0)