wll8 / redoc-try

Add `Try it out` function like swagger on redoc.
https://wll8.github.io/redoc-try
117 stars 33 forks source link

FastApi integration #13

Closed Trinkes closed 3 years ago

Trinkes commented 3 years ago

Hello, I'm currently using python's library FastAPI to make the endpoints and generate its Redoc documentation, is there any way to integrate your project using FastApi library?

wll8 commented 3 years ago

You may need to slightly modify the source code of fastapi, similar to the following form:

html = f"""
<!DOCTYPE html>
<html>
<head>
<title>{title}</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
"""
if with_google_fonts:
    html += """
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
"""
html += f"""
<link rel="shortcut icon" href="{redoc_favicon_url}">
<style>
  body {{
    margin: 0;
    padding: 0;
  }}
</style>
</head>
<body>
- <redoc spec-url="{openapi_url}"></redoc>
- <script src="{redoc_js_url}"> </script>
+ <div id="redoc-container"></div>
+ <script src="//cdn.jsdelivr.net/npm/redoc@2.0.0-rc.48/bundles/redoc.standalone.min.js"> </script>
+ <script src="//cdn.jsdelivr.net/gh/wll8/redoc-try@1.3.4/dist/try.js"></script>
+ <script>
+   initTry({openapi_url})
+ </script>
</body>
</html>
"""
Trinkes commented 3 years ago

For future references, to make it work, I had to change your code: initTry({openapi_url}) to initTry(\"{openapi_url}\") Other than that, it worked like a charm, thank you very much! :ok_hand:

wll8 commented 3 years ago

Hahahaha, I'm very sorry, I gave the untested reference code. I am very happy to be able to help you!