api-postal-code-spain/app.py

47 lines
1.2 KiB
Python
Raw Normal View History

2018-04-04 20:42:10 +02:00
# -*- coding: utf-8 -*-
# =========================
# Librarys
# =========================
import os
from flask import Flask, request
from flask_cors import CORS
import pandas as pd
# =========================
# Extensions initialization
# =========================
app = Flask(__name__)
POSTALS = pd.read_csv('postal.csv', delimiter=',')
# =========================
# Configurations
# =========================
app.config['DEBUG'] = True if os.environ.get('DEBUG') == 'True' else False
PRE_URL = '/api/v1/'
CORS(app, resources={r"/api/*": {"origins": "*"}})
# =========================
# Routes
# =========================
2018-04-05 09:19:37 +02:00
# Get data from index
@app.route(PRE_URL + 'index/<int:id>')
def index(id):
match = POSTALS.loc[[id]]
2018-04-05 09:22:34 +02:00
results = match.filter(items=['postal_code', 'poblacion', 'lat', 'lng'])
return results.reset_index().to_json(orient='records')
2018-04-05 09:19:37 +02:00
2018-04-04 20:42:10 +02:00
# Get data from postal code
2018-04-05 09:19:37 +02:00
@app.route(PRE_URL + 'postal_code/<int:code>')
def postal_code(code):
2018-04-04 20:42:10 +02:00
match = POSTALS[POSTALS['postal_code'].isin([code])]
results = match.filter(items=['postal_code', 'poblacion', 'lat', 'lng'])
return results.reset_index().to_json(orient='records')
2018-04-05 09:19:37 +02:00
2018-04-04 20:42:10 +02:00
if __name__ == "__main__":
app.run()