OneCompiler

Fastapi

108

Example heading with h2 size

Example heading with h3 size

Following is sample java code.

int i = 10;
if(i>0){
    System.out.println('positive');
}

from fastapi import FastAPI, Depends
from fastapi.responses import HTMLResponse
from jinja2 import Template
import asyncpg

app = FastAPI()

Database Connection

DATABASE_URL = "postgresql://username:password@localhost:5432/billdb"

async def get_db():
return await asyncpg.connect(DATABASE_URL)

HTML Template for Rendering Bill

TEMPLATE = """

<!DOCTYPE html> <html> <head> <title>Bill Details</title> </head> <body> <h2>Bill Details for Account No: {{ account_no }}</h2> {% if bill %} <p><strong>Name:</strong> {{ bill['name'] }}</p> <p><strong>Amount Due:</strong> {{ bill['amount_due'] }}</p> <p><strong>Due Date:</strong> {{ bill['due_date'] }}</p> {% else %} <p>No bill details found for this account number.</p> {% endif %} </body> </html> """

@app.get("/viewbill/{account_no}", response_class=HTMLResponse)
async def view_bill(account_no: str, db=Depends(get_db)):
query = "SELECT name, amount_due, due_date FROM bills WHERE account_no = $1"
bill = await db.fetchrow(query, account_no)

template = Template(TEMPLATE)
return template.render(account_no=account_no, bill=dict(bill) if bill else None)