Sphinx documentation inside a Flask running web application

后端 未结 4 1784
隐瞒了意图╮
隐瞒了意图╮ 2021-02-13 04:40

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

4条回答
  •  鱼传尺愫
    2021-02-13 05:00

    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('/')
    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 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
    │   │   ├── .doctree
    │   │   ├── ...
    │   │   └── .doctree
    │   └── html
    │       ├── _images
    │       ├── _modules
    │       │   ├── index.html
    │       │   └── 
    │       │       └── ...
    │       ├── _sources
    │       │   ├── .rst.txt
    │       │   ├── ...
    │       │   └── .rst.txt
    │       ├── _static
    │       │   ├── ajax-loader.gif
    │       │   ├── alabaster.css
    │       │   └── ...
    │       ├── genindex.html
    │       ├── index.html
    │       ├── objects.inv
    │       ├── py-modindex.html
    │       ├── search.html
    │       ├── searchindex.js
    │       ├── .html
    │       ├── ...
    │       └── .html
    ├── _static
    │   ├── custom.css
    │   └── .gif
    ├── _templates
    ├── app.py
    ├── conf.py
    ├── index.rst
    ├── make.bat
    ├── .rst
    ├── ...
    └── .rst
    

提交回复
热议问题