Spaces:
Runtime error
Runtime error
from fastapi import FastAPI, Request, Response | |
filenames = ["js/poseMaker.js"] | |
contents = '\n'.join([open(x).read() for x in filenames]) | |
app = FastAPI() | |
async def insert_js(request: Request, call_next): | |
path = request.scope['path'] # get the request route | |
response = await call_next(request) | |
if path == "/": | |
response_body = "" | |
async for chunk in response.body_iterator: | |
response_body += chunk.decode() | |
some_javascript = f""" | |
<script type="text/javascript" defer> | |
{contents} | |
</script> | |
""" | |
response_body = response_body.replace("</body>", some_javascript + "</body>") | |
del response.headers["content-length"] | |
return Response( | |
content=response_body, | |
status_code=response.status_code, | |
headers=dict(response.headers), | |
media_type=response.media_type | |
) | |
return response |