-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
31 lines (25 loc) · 1.13 KB
/
calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from fastapi import FastAPI, Form, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
import os
app = FastAPI()
# Create a Jinja2Templates instance with the templates directory
templates = Jinja2Templates(directory=os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates"))
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
"""
Render the "form.html" template with the provided context
"""
return templates.TemplateResponse("form.html", {"request": request})
@app.post("/", response_class=HTMLResponse)
async def calculate(request: Request, expression: str = Form(...)):
"""
Evaluate the expression provided by the user
Render the "result.html" template with the result and context
Render the "error.html" template with the error message and context
"""
try:
result = eval(expression)
return templates.TemplateResponse("result.html", {"result": result, "request": request})
except Exception as e:
return templates.TemplateResponse("error.html", {"error_message": str(e), "request": request})