Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How can I serve static files (html, js) easily? #130

Description

@3lpsy

Description

How can I serve static files (html, js) easily? Even though I don't need to inject python data, I attempted to do it with jinja and encountered issues.

Additional context
I tried something like:

@router.get("/webui/{file_name}", name="webui.show")
async def webui_show(file_name: str):
    template = jinja2.get_template(file_name)
    content = template.render()
    return HTMLResponse(content=content, status_code=200)

Where jinja2 was

loader = FileSystemLoader(web_dir("dist"))
# no escaping while deubing
jinja2 = Environment(loader=loader, autoescape=False)

But I received an error message "jinja2.exceptions.TemplateSyntaxError: Expected an expression, got 'end of print statement'" when returning JavaScript files. What's sanctioned way to implement simple static file serving in fastapi? If so, it'd be nice to have some documentation around it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions