I am adding a swaggerUI to a Flask API. I have my app and inside the app I created a swagger dir where the templates live. I set the template_folder when the flask app is initialized in the __init__
file. This appears to be working since it prints out the correct directory inside the function. I even did an ls and the swaggerui.html file is there, but I am still getting and error that the template is not found. Also, if I add a random file testin123.txt in the templates_folder, when I refresh the site, testing123.txt prints out. What am I missing?
from flask import Flask, request, jsonify, render_template, Blueprint
from flask import current_app as current_app
import os
docs = Blueprint('docs', __name__)
@docs.route('/docs')
def get_docs():
#this prints out the correct path
print(f"template dir is {current_app.template_folder}")
#this prints "swaggerui.html"
for filename in os.listdir(current_app.template_folder):
print(filename)
print("After ls")
#even though everything check-out still cant find file
return render_template('swaggerui.html')
template dir is flask_project/swagger/templates/
swaggerui.html
test123.txt
After ls
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/app/./flask_project/docs.py", line 28, in get_docs
return render_template('swaggerui.html')
File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 148, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1068, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 997, in get_template
return self._load_template(name, globals)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 958, in _load_template
template = self.loader.load(self, name, self.make_globals(globals))
File "/usr/local/lib/python3.9/site-packages/jinja2/loaders.py", line 125, in load
source, filename, uptodate = self.get_source(environment, name)
File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 59, in get_source
return self._get_source_fast(environment, template)
File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 95, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: swaggerui.html
.
├── __init__.py
├── docs.py
├── swagger
│ ├── static
│ │ ├── css
│ │ │ └── swagger-ui.css
│ │ ├── img
│ │ │ ├── favicon-16x16.png
│ │ │ └── favicon-32x32.png
│ │ ├── js
│ │ │ ├── swagger-ui-bundle.js
│ │ │ ├── swagger-ui-standalone-preset.js
│ │ │ └── swagger-ui.js
│ │ └── openapi.json
│ └── templates
│ └── swaggerui.html