File tree Expand file tree Collapse file tree 5 files changed +45
-1
lines changed
Expand file tree Collapse file tree 5 files changed +45
-1
lines changed Original file line number Diff line number Diff line change 1+ .env
2+ __pycache__
Original file line number Diff line number Diff line change 11FROM python:3.9-slim-bullseye
22
3- RUN python -m pip install fastapi "uvicorn[standard]" jinja2 aiosql
3+ RUN apt update -y && apt install build-essential libpq-dev -y
4+
5+ RUN python -m pip install fastapi "uvicorn[standard]" jinja2 aiosql psycopg2-binary --no-binary psycopg2-binary python-dotenv
46
57WORKDIR /workspace
68
Original file line number Diff line number Diff line change 1+ -- name: get-all-companies
2+ -- Get a list of all of the companies.
3+ SELECT company_name FROM companies;
Original file line number Diff line number Diff line change 1+ < html >
2+ < head >
3+ < title > HOME</ title >
4+ </ head >
5+ < body >
6+ < ul >
7+ {% for company in payload1 %}
8+ < li > {{ company }}</ li >
9+ {% endfor %}
10+ </ ul >
11+ </ body >
12+ </ html >
Original file line number Diff line number Diff line change 11from fastapi import FastAPI
22from fastapi .templating import Jinja2Templates
33from fastapi .requests import Request
4+ from dotenv import load_dotenv
5+ import psycopg2
6+ import aiosql
7+ import os
8+
49
510app = FastAPI ()
611templates = Jinja2Templates (directory = 'templates/' )
@@ -17,3 +22,23 @@ async def root(request: Request):
1722 response = templates .TemplateResponse ("index.html" , {"request" : request , "payload1" : hello_world })
1823 return response
1924
25+ @app .get ("/companies" )
26+ async def get_companies (request : Request ):
27+ load_dotenv ()
28+
29+ conn = psycopg2 .connect (f"dbname=tdm_main user=rdonly password={ os .getenv ('PG_PASSWORD' )} host=db.tdm.geddes.rcac.purdue.edu" )
30+ queries = aiosql .from_path ("queries.sql" , "psycopg2" )
31+
32+ companies = [v [0 ] for v in queries .get_all_companies (conn )]
33+ print (type (companies ))
34+ print (companies )
35+
36+ accept = request .headers .get ("accept" )
37+ hello_world = {"message" : "hello world" }
38+
39+ if accept .split ("/" )[1 ] == 'json' :
40+ return hello_world
41+
42+ if len (accept .split ("," )) > 1 or accept .split ("/" )[1 ] == 'html' :
43+ response = templates .TemplateResponse ("companies.html" , {"request" : request , "payload1" : companies })
44+ return response
You can’t perform that action at this time.
0 commit comments