Setup website with docusaurus (#11)

Summary:
Set up landing page, docs page, and html versions of the ipython notebook tutorials.
Pull Request resolved: https://github.com/fairinternal/pytorch3d/pull/11

Reviewed By: gkioxari

Differential Revision: D19730380

Pulled By: nikhilaravi

fbshipit-source-id: 5df8d3f2ac2f8dce4d51f5d14fc336508c2fd0ea
This commit is contained in:
Nikhila Ravi
2020-02-04 17:25:51 -08:00
committed by Facebook Github Bot
parent e290f87ca9
commit 15d3a4557e
48 changed files with 1900 additions and 72 deletions

60
scripts/build_website.sh Normal file
View File

@@ -0,0 +1,60 @@
#!/bin/bash
# Copyright (c) Facebook, Inc. and its affiliates.
# run this script from the project root using `./scripts/build_docs.sh`
usage() {
echo "Usage: $0 [-b]"
echo ""
echo "Build PyTorch3D documentation."
echo ""
echo " -b Build static version of documentation (otherwise start server)"
echo ""
exit 1
}
BUILD_STATIC=false
while getopts 'hb' flag; do
case "${flag}" in
h)
usage
;;
b)
BUILD_STATIC=true
;;
*)
usage
;;
esac
done
echo "-----------------------------------"
echo "Building PyTorch3d Docusaurus site"
echo "-----------------------------------"
cd website || exit
yarn
cd ..
echo "-----------------------------------"
echo "Generating tutorials"
echo "-----------------------------------"
cwd=$(pwd)
mkdir -p "website/_tutorials"
mkdir -p "website/static/files"
python scripts/parse_tutorials.py --repo_dir "${cwd}"
cd website || exit
if [[ $BUILD_STATIC == true ]]; then
echo "-----------------------------------"
echo "Building static site"
echo "-----------------------------------"
yarn build
else
echo "-----------------------------------"
echo "Starting local server"
echo "-----------------------------------"
yarn start
fi

111
scripts/parse_tutorials.py Normal file
View File

@@ -0,0 +1,111 @@
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
import argparse
import json
import os
import nbformat
from bs4 import BeautifulSoup
from nbconvert import HTMLExporter, ScriptExporter
TEMPLATE = """const CWD = process.cwd();
const React = require('react');
const Tutorial = require(`${{CWD}}/core/Tutorial.js`);
class TutorialPage extends React.Component {{
render() {{
const {{config: siteConfig}} = this.props;
const {{baseUrl}} = siteConfig;
return <Tutorial baseUrl={{baseUrl}} tutorialID="{}"/>;
}}
}}
module.exports = TutorialPage;
"""
JS_SCRIPTS = """
<script
src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js">
</script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js">
</script>
""" # noqa: E501
def gen_tutorials(repo_dir: str) -> None:
"""Generate HTML tutorials for PyTorch3d Docusaurus site from Jupyter notebooks.
Also create ipynb and py versions of tutorial in Docusaurus site for
download.
"""
with open(os.path.join(repo_dir, "website", "tutorials.json"), "r") as infile:
tutorial_config = json.loads(infile.read())
tutorial_ids = {x["id"] for v in tutorial_config.values() for x in v}
for tid in tutorial_ids:
print("Generating {} tutorial".format(tid))
# convert notebook to HTML
ipynb_in_path = os.path.join(repo_dir, "docs", "tutorials", "{}.ipynb".format(tid))
with open(ipynb_in_path, "r") as infile:
nb_str = infile.read()
nb = nbformat.reads(nb_str, nbformat.NO_CONVERT)
# displayname is absent from notebook metadata
nb["metadata"]["kernelspec"]["display_name"] = "python3"
exporter = HTMLExporter()
html, meta = exporter.from_notebook_node(nb)
# pull out html div for notebook
soup = BeautifulSoup(html, "html.parser")
nb_meat = soup.find("div", {"id": "notebook-container"})
del nb_meat.attrs["id"]
nb_meat.attrs["class"] = ["notebook"]
html_out = JS_SCRIPTS + str(nb_meat)
# generate html file
html_out_path = os.path.join(
repo_dir, "website", "_tutorials", "{}.html".format(tid)
)
with open(html_out_path, "w") as html_outfile:
html_outfile.write(html_out)
# generate JS file
script = TEMPLATE.format(tid)
js_out_path = os.path.join(
repo_dir, "website", "pages", "tutorials", "{}.js".format(tid)
)
with open(js_out_path, "w") as js_outfile:
js_outfile.write(script)
# output tutorial in both ipynb & py form
ipynb_out_path = os.path.join(
repo_dir, "website", "static", "files", "{}.ipynb".format(tid)
)
with open(ipynb_out_path, "w") as ipynb_outfile:
ipynb_outfile.write(nb_str)
exporter = ScriptExporter()
script, meta = exporter.from_notebook_node(nb)
py_out_path = os.path.join(
repo_dir, "website", "static", "files", "{}.py".format(tid)
)
with open(py_out_path, "w") as py_outfile:
py_outfile.write(script)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate JS, HTML, ipynb, and py files for tutorials."
)
parser.add_argument(
"--repo_dir", metavar="path", required=True, help="Pytorch3D repo directory."
)
args = parser.parse_args()
gen_tutorials(args.repo_dir)

View File

@@ -0,0 +1,46 @@
#!/bin/bash
# Copyright (c) Facebook, Inc. and its affiliates.
usage() {
echo "Usage: $0 [-b]"
echo ""
echo "Build and push updated PyTorch3D site."
echo ""
exit 1
}
# Current directory (needed for cleanup later)
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Make temporary directory
WORK_DIR=$(mktemp -d)
cd "${WORK_DIR}" || exit
# Clone both master & gh-pages branches
git clone git@github.com:facebookresearch/pytorch3d.git pytorch3d-master
git clone --branch gh-pages git@github.com:facebookresearch/pytorch3d.git pytorch3d-gh-pages
cd pytorch3d-master/website || exit
# Build site, tagged with "latest" version; baseUrl set to /versions/latest/
yarn
cd .. || exit
./scripts/build_website.sh -b
yarn run build
cd "${WORK_DIR}" || exit
rm -rf pytorch3d-gh-pages/*
touch pytorch3d-gh-pages/CNAME
echo "pytorch3d.org" > pytorch3d-gh-pages/CNAME
mv pytorch3d-master/website/build/pytorch3d/* pytorch3d-gh-pages/
cd pytorch3d-gh-pages || exit
git add .
git commit -m 'Update latest version of site'
git push
# Clean up
cd "${SCRIPT_DIR}" || exit
rm -rf "${WORK_DIR}"