Skip to content

Instantly share code, notes, and snippets.

@cjauvin
Last active November 25, 2018 20:48
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 cjauvin/7ba5df44c508ea5cb69b73be8e678fd8 to your computer and use it in GitHub Desktop.
Save cjauvin/7ba5df44c508ea5cb69b73be8e678fd8 to your computer and use it in GitHub Desktop.
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