4

I have generated sphinx documentation for my project into documentation folder. Now I want to serve that using a flask server.

This is my project structure. I want to serve example.html from the documentation folder. That folder also has relevant CSS files required for it.

app/
    module1/
    module2/
    documentation/
        build/
            example.html
            styles.css
            ...
    templates/
        other_files.html
        ...

app_controller.py

@application.route("/doc", methods=["GET"])
@log_exceptions_to_github
def doc():
    return render_template('/build/index.html')

Issue:

Flask looks for HTML files in the "templates" folder only by default.

So I am not able to serve this. Note that copying from documentation dir to templates dir is not what I want to do because of project requirements.

I want to add "documentation" folder as a templates directory along with the default one.

I have tried this but it is not working: https://stackoverflow.com/a/13598839/2286762

an0o0nym
  • 1,456
  • 16
  • 33
Kishan Mehta
  • 2,598
  • 5
  • 39
  • 61

1 Answers1

2

Have a look at Flask docs about how to organize templates in Flask.

So why not simply put your documentation directory under the templates directory like this:

templates/
    index.html
    documentation/
        build/
            example.html
    other_directories/

And then simply call return render_template('documentation/build/example.html')

If you really want to keep your structure like you proposed in your code example, you can have a look at the question about How to load from more then one template_folder for Flask blueprint?.

In Flask documentation you can find out more about what is a blueprint.

an0o0nym
  • 1,456
  • 16
  • 33
  • Already tried it. Documentation folder has related css files too. Those Css paths are breaking if i do this. – Kishan Mehta Jul 24 '17 at 19:05
  • @KishanMehta any stack trace? Also try [this](https://stackoverflow.com/a/9519004/2808371) in addition to the methods described in the answer above to serve static files from other locations than `static` directory – an0o0nym Jul 24 '17 at 23:00
  • Ok the process is, I am using sphinx documentation generation command `make html` the html is generated automatically based on .rst files. Issue is I can't use "custom_static" tag in the auto generated html by sphinx. Hope I am clear. – Kishan Mehta Jul 25 '17 at 05:15
  • 1
    @KishanMehta so it's a problem with sphinx if I understood correctly. That sounds like a topic for a new question then. – an0o0nym Jul 25 '17 at 20:54
  • Okay. I thought there might be a way to add multiple templates & static folders like we can do in django. Anyways, I will take another approach. – Kishan Mehta Jul 26 '17 at 05:37