Last active
November 18, 2018 15:53
-
-
Save IvanSok/75c7e8934f24883a95b8f6e21faf07f0 to your computer and use it in GitHub Desktop.
Storage Problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Basic Instructions: | |
# make sure that both main.py and ids.csv are in the same folder | |
# to install flask: | |
# pip install Flask | |
# to run on Windows: | |
# C:\path\to\app>set FLASK_APP=main.py | |
# C:\path\to\app>flask run | |
# to start the server: | |
# $ export FLASK_APP=main.py | |
# $ flask run | |
# to call server from browser: | |
# 127.0.0.1:5000/promotions?id=78bfd79d-53c4-421d-b054-013ad294deab | |
from werkzeug.contrib.cache import SimpleCache | |
from flask import Flask | |
from flask import request | |
import pandas as pd | |
# initialize app and cache | |
app = Flask(__name__) | |
cache = SimpleCache() | |
@app.route('/promotions/') | |
def get_csv_val(): | |
# get data from cache | |
df = cache.get('csv_df') | |
# get id from request | |
ID = request.args.get('id') | |
# if cache empty - initialize it | |
if df is None: | |
# read data to pandas dataframe - order records by id (index) for faster search | |
df = pd.read_csv('ids.csv', sep=',', header=0, index_col='id', names = ["id", "price", "expiration_date"]) | |
# saving to cache | |
cache.set('csv_df', df, timeout=25 * 60) | |
# check if ID exists and return data, else return None | |
if df.index.contains(ID): | |
return str(df.iloc[[df.index.get_loc(ID)]].to_dict(orient='index')) | |
else: | |
return 'None' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment