Skip to content

Instantly share code, notes, and snippets.

@poke

poke/app.py Secret

Created July 25, 2017 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poke/a4af58584d56d3d3c4e61db89455dd63 to your computer and use it in GitHub Desktop.
Save poke/a4af58584d56d3d3c4e61db89455dd63 to your computer and use it in GitHub Desktop.
Very simple and reduced bare-bones Flask API example
from flask import Flask, request, render_template
app = Flask(__name__, template_folder='.')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api', methods=['POST'])
def api():
return 'API received the following data: ' + request.data.decode()
if __name__ == '__main__':
app.run()
<!doctype html>
<html>
<head>
<meta charset="utf8" />
<title>Flask example</title>
</head>
<body>
<h1>Flask example</h1>
<input id="input" type="text" />
<button id="btn">Send</button>
<h3>Output</h3>
<pre id="output"></pre>
<script>
function callApi(data, callback) {
var xhr = new XMLHttpRequest();
xhr.open('post', '/api', true);
xhr.onload = function () {
callback(this.responseText);
};
xhr.send(data);
}
var input = document.getElementById('input');
var output = document.getElementById('output');
document.getElementById('btn').addEventListener('click', function () {
callApi(input.value, function (data) {
output.appendChild(document.createTextNode('Sent: ' + input.value + '\nReceived: ' + data + '\n\n'));
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment