django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

embedding api docs into custom page

Open MM-cyi opened this issue 1 year ago • 3 comments

Hi.

I am using the autom.-generated docs function, from ninja API, to create a web-based, user friendly interface for the users visiting my page, as well as test facility.

I would like to embed that wonderful feature into my already existing custom web layout (header, footer, navbar, etc...). By so doing, I would have to reinvent the wheel and the user could use that standalone facility. Basically, I am missing a HOME button....

I am currently using django

Is that possible? Any examples?

Thanks. Marco

MM-cyi avatar Jan 27 '24 18:01 MM-cyi

Hi @MM-cyi

You can create a custom docs viewer and override default template:

https://github.com/vitalik/django-ninja/blob/master/ninja/templates/ninja/swagger_cdn.html

you can render it like this:

from ninja.openapi.docs import Swagger

class MyDocs(Swagger):
     template = 'path/to/my/template.html' # or template_cdn if you do not put 'ninja' to installed apps

api = NinjaAPI(docs=MyDocs())


or just use bare swagger ui (https://swagger.io/tools/swagger-ui/) as regular page and just use /api/openapi.json as SwaggerUIBundle.url

vitalik avatar Jan 30 '24 14:01 vitalik

Hi vitalik.

Thank you for your kind and swift reply. I tried that in the past already, but when running the first line, I got:


----> 1 from ninja.openapi.docs import Swagger

ModuleNotFoundError: No module named 'ninja.openapi.docs'

I checked my ninja version and I have:

conda list | grep ninja
django-ninja              0.22.2                   pypi_0    pypi

Importing Redocs wouldn't work neither.

Any suggestions?

MM-cyi avatar Jan 31 '24 08:01 MM-cyi

well pluggable docs are added in ninja 1.x

for 0.22 what you can do

  • add "ninja" to INSTALLED_APPS
  • create template ninja/swagger.html on your templates folder and override it

or

or just use bare swagger ui (https://swagger.io/tools/swagger-ui/) as regular page and just use /api/openapi.json as SwaggerUIBundle.url configuration

vitalik avatar Jan 31 '24 08:01 vitalik