forked from BOsterbuhr/FEC-Data-Wranglin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
101 lines (81 loc) · 3.25 KB
/
main.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
import uvicorn
import json
from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel
from src.data.data_fetcher import DataFetcher
# Instantiate fastAPI with appropriate descriptors
app = FastAPI(
title='FEC Data Visualization',
description="For displaying data gathered from FEC's API",
version="1.0",
docs_url='/docs'
)
# Instantiate templates path
templates = Jinja2Templates(directory="src/viz/templates/")
# Mount static files
app.mount(
'/assets', StaticFiles(directory='src/viz/templates/assets/'), name='assets')
app.mount(
'/images', StaticFiles(directory='src/viz/templates/images/'), name='images')
# Define routes
@app.get('/', response_class=HTMLResponse)
async def display_index(request: Request):
"""
Displays the index page
"""
return templates.TemplateResponse('index.html', {"request": request})
@app.get('/landing', response_class=HTMLResponse)
async def display_landing(request: Request):
"""
Displays the landing page
"""
return templates.TemplateResponse('landing.html', {"request": request})
@app.get('/generic', response_class=HTMLResponse)
async def display_generic(request: Request):
"""
Displays the generic page
"""
return templates.TemplateResponse('generic.html', {"request": request})
@app.get('/elements', response_class=HTMLResponse)
async def display_elements(request: Request):
"""
Displays the elements page
"""
return templates.TemplateResponse('elements.html', {"request": request})
@app.get('/map', response_class=HTMLResponse)
async def display_elements(request: Request):
"""
Displays the map page
"""
return templates.TemplateResponse('map.html', {"request": request, "MAPS_API_KEY": os.environ.get("MAPS_API_KEY")})
@app.post('/generic', response_class=HTMLResponse)
async def display_map_results(request: Request,
election_year: str = Form(...),
election_type: str = Form(...),
ship_address: str = Form(...),
locality: str = Form(...),
state: str = Form(...),
postcode: str = Form(...),
country: str = Form(...)):
"""
Displays the generic page with map results
"""
fetcher = DataFetcher(election_year, election_type, None, state, None)
fetcher.api_starting_url_container
fetcher.gimmie_data(record_limit=100)
fetcher.save_df_data()
return templates.TemplateResponse('generic.html',
{"request": request,
"election_year": election_year,
"election_type": election_type,
"ship_address": ship_address,
"locality": locality,
"state": state,
"postcode": postcode,
"country": country})
if __name__ == '__main__':
uvicorn.run("main:app", reload=True)