Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sphinx documentation inside a Flask running web application

Tags:

I've locally built static Sphinx documentations (using make html).

I wish now to integrate the Sphinx files into my webapp that runs with Flask. From the Flask running application, I simply mean to include an hyperlink towards the Sphinx documentation, that will serve as help for the application.

Websupport seems the way to follow, but it's not clear to me how I should bridge the Flask framework to the Sphinx files.

Thanks for any help,

Regards

like image 394
mannaia Avatar asked Feb 16 '13 17:02

mannaia


2 Answers

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
like image 127
Gustavo Bezerra Avatar answered Sep 25 '22 14:09

Gustavo Bezerra


You could just handle it with your web server, the same way you handle the /static directory in Flask. For example if you used Apache as your production web server, you might add

Alias /documentation /location/of/sphinx/html
<Directory /location/of/sphinx/html>
    Order deny,allow
    Allow from all
</Directory>

to your Apache site configuration, so then you could just link directly to http://yoursite.com/documentation to access the Sphinx files, avoiding Flask altogether.

like image 30
Doobeh Avatar answered Sep 21 '22 14:09

Doobeh