Author: Megan Brown
This repo contains code for running a small API to return
primary results based on a local file primary_results.json
.
Requirements for this package beyond the standard Python library are
flask
, flask_restful
, and pandas
.
To install, run:
pip install Flask flask-restful pandas
To run the app, download this GitHub repo by running
git clone git@github.com:mabrownnyu/cta_walkthrough.git
cd cta_walkthrough
python app.py
/county
Parameter | Type | Required | Description |
---|---|---|---|
State | string | True | US State to get county results from (case sensitive) |
County | string | True | US County to get primary results from (case sensitive) |
/state
Parameter | Type | Required | Description |
---|---|---|---|
State | string | True | US State to get county results from (case sensitive) |
/all
No parameters
To test the county endpoint, in your local browser, navigate to http://127.0.0.1:5000/county?state=Pennsylvania&county=Chester. This should return:
{
"data": {
"Democrats": "Humphrey",
"Republicans": "Nixon"
}
}
To test the state endpoint, in your local browser, navigate to http://127.0.0.1:5000/county?state=Pennsylvania. This should return:
{
"data": {
"Chester": {
"Democrats": "Humphrey",
"Republicans": "Nixon"
}
}
}
To test the all endpoint, in your local browser, navigate to http://127.0.0.1:5000/all. This should return:
{
"data": {
"Pennsylvania": {
"Chester": {
"Democrats": "Humphrey",
"Republicans": "Nixon"
}
}
}
}
- Working on more general purpose code so the code traversing the JSON is not repeated
- Thinking more deeply about the payloads returned, specifically what variable names would be useful for the client/end user and what formats would be more useful (e.g. the nested JSONs as shown or a record-oriented response JSON)
- Consider loading the data into a pandas dataframe to take advantage of the lightweight querying/aggregating capabilities.
- More robust error responses/possibly fuzzy matches or query suggestions based on misspellings
- Error handling for fields missing in the JSON (e.g. if a county is in the JSON but had no reported election results)
- Formatting the endpoints to work like
<state>/<county>
rather than through query parameters. - Think about scalability re: how large the file would potentially be/whether it is reasonable to hold the data in memory while the app is running.