pluginlab-ai / fastapi-facts-plugin-template

Use FastAPI on Vercel and PluginLab to instantly create and deploy your ChatGPT plugin.
2 stars 0 forks source link

FastAPI html templates #1

Open Maxiviper117 opened 1 year ago

Maxiviper117 commented 1 year ago

Nice repo, been looking for a setup to deploy to Vercel, would you happen to know how to use FastAPI templates to display static html?

When I use this kind of setup, the page works fine locally, but deploying on vercel gives unknown error to GET /favicon.ico

templates = Jinja2Templates(directory="templates")
@app.get("/", include_in_schema=False)
async def home(request: Request):
    context = {"request": request, "version": __version__}
    return templates.TemplateResponse("home.html", context)
kevinpiac commented 1 year ago

Hey!

The GET /favicon.ico is probably unrelated to your real issue, it's just that your browser is trying to fetch the favicon which is not prevent. But that issue should not prevent from using the app properly.

Do you have any other error message?

Maxiviper117 commented 1 year ago

These are the only errors on vercel I get when I deploy.

Logs: image

Error on Page: image

Code in src/main.py

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import os

app = FastAPI()

current_path = os.path.dirname(os.path.abspath(__file__))
templates_path = os.path.join(current_path, 'templates')

templates = Jinja2Templates(directory=templates_path)

app.mount("/static", StaticFiles(directory="static"), name="static")

@app.get("/home")
async def home(request: Request):
    return templates.TemplateResponse("home.html", {"request": request, "name": "Jonhn Doe"})

Project Tree

templates route in src/templates

python-fastapi-template
├── .dockerignore
├── .gitignore
├── .vercel
│   ├── project.json
│   └── README.txt
├── api
│   ├── index.py
│   └── __pycache__
│       └── index.cpython-311.pyc
├── Dockerfile
├── main.py
├── README.md
├── requirements.txt
├── src
│   ├── data
│   │   └── facts.json
│   ├── index.py
│   ├── templates
│   │   └── home.html
│   ├── __main__.py
│   └── __pycache__
│       └── index.cpython-311.pyc
├── static
│   ├── favicon.ico
│   └── logo.jpg
├── vercel.json
└── __pycache__
    └── main.cpython-311.pyc
Maxiviper117 commented 1 year ago

If I use the HTMLResponse instead, then that works, but would rather use templates.

@app.get("/home")
async def home(request: Request):
    message = f"""
    <html>
    <head>
        <title>Home</title>
    </head>
    <body>
        <h1>Hello!</h1>
        <p>This is a web page rendered with a multiline string variable and FastAPI.</p>
    </body>
    </html>
    """
    return HTMLResponse(content=message, status_code=200)