FastAPITest1 / main.py
BFManza's picture
ammend main.py
e3f9d7d
Raw History Blame Contribute Delete
1.89 kB
from fastapi import FastAPI, HTTPException, Body
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from starlette.requests import Request
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
# Mount the static directory for serving static files like CSS, JS, etc.
app.mount("/static", StaticFiles(directory="static"), name="static")
# Setting up the Jinja2 templating with the templates directory
templates = Jinja2Templates(directory="templates")
@app.get("/")
async def read_root(request: Request):
# Using the template for the homepage and passing some dynamic data (in this case, "name": "John")
return templates.TemplateResponse("index.html", {"request": request, "name": "John"})
@app.post("/question/")
async def read_question(question: str = Body(..., embed=True)):
return {"Received Question": question}
@app.post("/opportunity/")
async def read_opportunity(opportunity: str = Body(..., embed=True)):
return {"Received Opportunity": opportunity}
@app.post("/run-script/")
async def run_script():
try:
# Execute the python script
from utilities import step1
# The script executed successfully
print(f' \n ****** \n GOOOOOOODDD')
step1.run_step1()
print(f' \n ****** \n GOOOOOOODDD -----------2')
return {"status": "success", "message": "Script executed successfully"}
except Exception as e:
# There was an error during script execution
print(f' \n ****** \n BADDDDD \n {str(e)}')
return {"status": "error", "message": str(e)}
origins = [
"http://localhost",
"http://localhost:8000",
"http://localhost:4000",
"https://bfmanza-fastapitest1.hf.space"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)