-
-
Save poke/a4af58584d56d3d3c4e61db89455dd63 to your computer and use it in GitHub Desktop.
Very simple and reduced bare-bones Flask API example
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 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() |
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
<!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