Last active
November 25, 2018 20:48
-
-
Save cjauvin/7ba5df44c508ea5cb69b73be8e678fd8 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 flask import Flask, send_file | |
from io import BytesIO | |
app = Flask(__name__) | |
@app.route('/get_placements') | |
def get_placements(): | |
placements = '<Placemark>bla</Placemark>' # replace with your own placement creation logic | |
f = BytesIO() # memory buffer, acting as a file | |
# f-strings are new in py3.6+ | |
content = f'''<?xml version="1.0" encoding="utf-8"?> | |
<kml xmlns="http://www.opengis.net/kml/2.2"> | |
{placements} | |
</kml>''' | |
# the content string must be of type `bytes` in this context (not `unicode`, which is the default with py3+) | |
f.write(content.encode('utf8')) | |
f.seek(0) | |
# not sure if `as_attachment` is needed in your context | |
return send_file(f, attachment_filename="placement.kml", as_attachment=True) | |
if __name__ == '__main__': | |
app.run(host='127.0.0.1', port=8080, debug=True) | |
# To test: | |
# python3 seb.py | |
# | |
# curl http://127.0.0.1:8080/get_placements | |
# <?xml version="1.0" encoding="utf-8"?> | |
# <kml xmlns="http://www.opengis.net/kml/2.2"> | |
# <Placemark>bla</Placemark> | |
# </kml>% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment