Created
April 16, 2016 18:59
-
-
Save renanpalmeira/c582bce345bb2e32e5ec7ff2312d4940 to your computer and use it in GitHub Desktop.
This file contains 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
from __future__ import unicode_literals | |
import json | |
import random | |
import urllib | |
import jinja2 | |
from collections import OrderedDict | |
from flask import Flask, jsonify, render_template | |
data = [{'id':2, 'name':'Geroudo'}, | |
{'id':1, 'name':'Ana'}, | |
{'id':3, 'name':'Renan'},] | |
app = Flask(__name__, static_folder='client/static') | |
loader = jinja2.ChoiceLoader([ | |
app.jinja_loader, | |
jinja2.FileSystemLoader('client/templates'), | |
]) | |
app.jinja_loader = loader | |
@app.route('/') | |
def home(): | |
return render_template('index.html') | |
@app.route('/api', methods=['POST']) | |
def api_post(): | |
global data | |
url = 'https://randomuser.me/api/' | |
content = urllib.urlopen(url).read() | |
name = json.loads(content)['results'][0]['name']['first'] | |
data += [{ | |
'id': random.randint(0,3000), | |
'name': name | |
}] | |
return '{\'response\': \'OK\'}' | |
@app.route('/api') | |
def api_get(): | |
global data | |
response = OrderedDict() | |
response['count'] = 3 | |
response['next'] = None | |
response['previus'] = None | |
response['data'] = data | |
return jsonify(response) | |
if __name__=='__main__': | |
app.run(host='0.0.0.0', debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment