0

I'm using apidoc to generate a documentation website. On running, it creates a folder called doc, which contains an index.html file and accompanying css and js.

I'd like to be able to serve this folder with a Flask server, but can't work out how to do it.

My folder structure looks like this

-root
--- doc/ #contains all of the static stuff
--- server.py

I've tried this, but can't get it to work:

app = Flask(__name__, static_url_path="/doc")

@app.route('/')
def root():
    return app.send_from_directory('index.html')

One of the problems is that all of the static files referenced in the index.html generated by apidoc are relative to that page, so /js/etc. doesn't work, since it's actually /doc/js...

It would be great if someone could help me with the syntax here. Thanks.

Raph117
  • 3,441
  • 7
  • 29
  • 50

1 Answers1

0

I spot three problems in code.

a) you do not need to use static_url_path, as send_from_directory is independent of it

b) when I try to run above code, and go to /, I get a AttributeError: 'Flask' object has no attribute 'send_from_directory' - this means translates to app.send_from_directory is wrong - you need to import this function from flask, ie from flask import send_from_directory

c) when I then try to run your code, I get a TypeError: send_from_directory() missing 1 required positional argument: 'filename', which means send_from_directory needs another argument; it needs both a directory and a file

Putting this all together you get something like this:

from flask import Flask
from flask import send_from_directory


app = Flask(__name__)


@app.route("/")
def index():
    return send_from_directory("doc", "index.html")

As a takeway (for myself):

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jürgen Gmach
  • 5,366
  • 3
  • 20
  • 37
  • 1
    Thank you very much - I'll try this now; as an aside, I do try to read both the docs and the errors, but every now and then it's not enough. Many thanks – Raph117 Oct 14 '20 at 10:04
  • Sadly this doesn't work - all of the static files in the doc dir return 404s, because the index.html file can't find them – Raph117 Oct 14 '20 at 10:27
  • So you mean the index.html works, but not the included js and css and e.g. pictures? I tried the above example locally before answering. Is your generated html (apidoc) somewhere public available? When I try to server a lot of static files, I do this not via Flask but with my webserver, e.g. in Nginx config, I create two location sections, one for flask (reverse proxy), and one static for the html files. Still, the offer is valid - if you can make the html files available, I try to solve this within Flask. – Jürgen Gmach Oct 14 '20 at 12:01
  • 1
    Hi, thanks - I ultimately have done just that - setup an Nginx server and deployed the files there. It made more sense for what I was doing I think. Thanks for your help anyway. I'll mark your answer as correct since the index.html was being returned correctly. – Raph117 Oct 14 '20 at 12:32