Skip to content

Commit

Permalink
Setup Sphinx and ReadTheDocs (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharth-krishna authored Feb 9, 2024
1 parent 161b2f0 commit 630872c
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ speedscope.json
*.pkl
.venv/
benchmarks/
docs/_build/
docs/api/
34 changes: 34 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Read the Docs configuration file for Sphinx projects
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the OS, Python version and other tools you might need
build:
os: ubuntu-22.04
tools:
python: "3.9"

# Build documentation in the "docs/" directory with Sphinx
sphinx:
configuration: docs/conf.py
# You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs
# builder: "dirhtml"
# Fail on all warnings to avoid broken references
fail_on_warning: true

# Optionally build your docs in additional formats such as PDF and ePub
formats:
- pdf
# - epub

# Optional but recommended, declare the Python requirements required
# to build your documentation
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
python:
install:
- requirements: docs/requirements.txt
# Install our python package before building the docs
- method: pip
path: .
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ After installation, run the following command to see the basic usage and availab
xl2times --help
```

## Development Setup
## Documentation

The tool's documentation is at http://xl2times.readthedocs.io/ and the source is in the [`docs/`](docs/) directory.

The documentation is generated by Sphinx and hosted on ReadTheDocs. We use the following extensions:
- `myst-parser`: to be able to write documentation in markdown
- `sphinx-book-theme`: the theme
- `sphinx-copybutton`: to add copy buttons to code blocks
- `sphinxcontrib-apidoc`: to automatically generate API documentation from the Python package

## Development

### Setup

We recommend installing the tool in editable mode (`-e`) in a Python virtual environment:
```bash
Expand All @@ -53,18 +65,11 @@ If you want to skip these pre-commit steps for a particular commit, if for insta
git commit --no-verify
```

### Publishing the Tool
### Running Benchmarks

To publish a new version of the tool on PyPI, update the version number in `pyproject.toml`, and then run:
```bash
python -m pip install --upgrade build
python -m pip install --upgrade twine
rm -rf dist
python -m build
python -m twine upload dist/*
```
See our GitHub Actions CI `.github/workflows/ci.yml` and the utility script `utils/run_benchmarks.py` to see how to run the tool on the DemoS models.

## Debugging Regressions
### Debugging Regressions

If your change is causing regressions on one of the benchmarks, a useful way to debug and find the difference is to run the tool in verbose mode and compare the intermediate tables. For example, if your branch has regressions on Demo 1:
```bash
Expand All @@ -78,9 +83,16 @@ code -d before after
```
VS Code will highlight the changes in the two files, which should correspond to any differences in the intermediate tables.

## Running Benchmarks
### Publishing the Tool

See our GitHub Actions CI `.github/workflows/ci.yml` and the utility script `utils/run_benchmarks.py` to see how to run the tool on the DemoS models.
To publish a new version of the tool on PyPI, update the version number in `pyproject.toml`, and then run:
```bash
python -m pip install --upgrade build
python -m pip install --upgrade twine
rm -rf dist
python -m build
python -m twine upload dist/*
```

## Contributing

Expand Down
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
57 changes: 57 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = "xl2times"
copyright = "2024, xl2times authors"
author = "xl2times authors"
# TODO version = xl2times.__version__ ?

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = [
"myst_parser",
"sphinx.ext.duration",
"sphinx.ext.autodoc",
"sphinx.ext.autosummary",
"sphinx.ext.napoleon",
"sphinx_copybutton",
"sphinxcontrib.apidoc",
]

templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "_old"]

apidoc_module_dir = "../xl2times"
apidoc_output_dir = "api"
# apidoc_excluded_paths = ['tests']
# apidoc_separate_modules = True

# -- Options for autodoc ----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#configuration

# Automatically extract typehints when specified and place them in
# descriptions of the relevant function/method.
autodoc_typehints = "description"

# Don't show class signature with the class' name.
autodoc_class_signature = "separated"

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = "sphinx_book_theme"
html_static_path = ["_static"]

html_theme_options = {
"repository_url": "https://github.com/etsap-TIMES/xl2times",
"use_repository_button": True,
"path_to_docs": "docs",
"use_edit_page_button": True,
"home_page_in_toc": True,
}
21 changes: 21 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

.. toctree::
:maxdepth: 2
:hidden:

API Documentation <api/modules.rst>

..
Include the README as the index page of the docs to avoid duplication of essential information
https://stackoverflow.com/a/69134918/3189420
.. include:: ../README.md
:parser: myst_parser.sphinx_


Indices and tables
------------------

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
4 changes: 4 additions & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
myst-parser >= 2.0
sphinx-book-theme >= 1.1
sphinx-copybutton >= 0.5
sphinxcontrib-apidoc >= 0.5
39 changes: 22 additions & 17 deletions xl2times/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,29 @@ def extract_table(
sheetname: str,
filename: str,
) -> datatypes.EmbeddedXlTable:
"""
For each individual table tag found in a worksheet, this function aims to extract
the associated table. We recognise several types of tables:
- Single cell tables: Tables with only one value, either below or to the right of
the table tag. We interpret these as a single data item with
a column name VALUE.
- Multiple cell tables: Tables with multiple values, possibly extending accross
several rows and columns. We delimitate them using empty
spaces around them and the column names are determined by the
values in the row immediately below the table tag
"""For each individual table tag found in a worksheet, this function aims to extract
the associated table.
:param tag_row: Row number for the tag designating the table to be extracted
:param tag_col: Column number for the tag designating the table to be extracted
:param uc_sets: Sets (regions and timeslices) for user constraints
:param df: Dataframe object containing all values for the worksheet being evaluated
:param sheetname: Name of the worksheet being evaluated
:param filename: Path to the excel file being evaluated.
:return: Table object in the EmbeddedXlTable format.
We recognise several types of tables:
- Single cell tables: Tables with only one value, either below or to the right of
the table tag. We interpret these as a single data item with
a column name VALUE.
- Multiple cell tables: Tables with multiple values, possibly extending accross
several rows and columns. We delimitate them using empty
spaces around them and the column names are determined by the
values in the row immediately below the table tag
Args:
tag_row: Row number for the tag designating the table to be extracted
tag_col: Column number for the tag designating the table to be extracted
uc_sets: Sets (regions and timeslices) for user constraints
df: Dataframe object containing all values for the worksheet being evaluated
sheetname: Name of the worksheet being evaluated
filename: Path to the excel file being evaluated.
Returns:
Table object in the EmbeddedXlTable format.
"""
# If the cell to the right is not empty then we read a scalar from it
# Otherwise the row below is the header
Expand Down

0 comments on commit 630872c

Please sign in to comment.