Other solutions nicely omit the Flask
object initialization, which lead me to bang my head against the wall for a while.
Without touching the Sphinx project structure at all, here's the solution that worked for me:
from flask import Flask
app = Flask(__name__, static_url_path='/', static_folder='_build/html/')
@app.route('/')
@app.route('/<path:path>')
def serve_sphinx_docs(path='index.html'):
return app.send_static_file(path)
if __name__ == '__main__':
app.run(debug=True)
And below is the file structure of the project, where <doc>
represents the rst files I actually wrote for the documentation, and app.py
is the file containing the Flask app code above.
.
├── Makefile
├── _build
│ ├── doctrees
│ │ ├── index.doctree
│ │ ├── <doc>.doctree
│ │ ├── ...
│ │ └── <doc>.doctree
│ └── html
│ ├── _images
│ ├── _modules
│ │ ├── index.html
│ │ └── <package name>
│ │ └── ...
│ ├── _sources
│ │ ├── <doc>.rst.txt
│ │ ├── ...
│ │ └── <doc>.rst.txt
│ ├── _static
│ │ ├── ajax-loader.gif
│ │ ├── alabaster.css
│ │ └── ...
│ ├── genindex.html
│ ├── index.html
│ ├── objects.inv
│ ├── py-modindex.html
│ ├── search.html
│ ├── searchindex.js
│ ├── <doc>.html
│ ├── ...
│ └── <doc>.html
├── _static
│ ├── custom.css
│ └── <myimage>.gif
├── _templates
├── app.py
├── conf.py
├── index.rst
├── make.bat
├── <doc>.rst
├── ...
└── <doc>.rst