I am serving a simple folium map using the following flask app running as an AWS Lambda function:
from flask import Flask
import serverless_wsgi
from Map import fullscreen_map
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
app = Flask(__name__,
static_folder="maps",
static_url_path="/maps")
@app.route("/")
def homepage():
return fullscreen_map().get_root().render()
def handler(event, context):
return serverless_wsgi.handle_request(app, event, context)
The folium app (imported into the above):
from branca.element import Template, MacroElement
import folium
from flask import request
def fullscreen_map():
m = folium.Map(
location=(40.746759, -74.042197), zoom_start=16, tiles="cartodb positron"
)
heights_building_footprints_url = f"{request.base_url}/maps/heights-building-footprints.geojson"
logging.info(f"heights_building_footprints_url: {heights_building_footprints_url}")
folium.GeoJson(
heights_building_footprints_url,
name="Building Footprints",
style_function=lambda feature: {
'fillColor': 'grey',
'color': 'black',
'weight': 0.5,
'dashArray': '3, 3'
},
).add_to(m)
return m
The problem I am having is that for some reason either flask or something in the Lambda handler seem to be encoding the geojson files as binary. I don't know why because it serves HTML files in the same static folder properly.
You can see this for yourself in a test deployment:
https://rna.chilltownlabs.com/maps/test.html — flask serves this fine, so the static directory is working.
https://rna.chilltownlabs.com/maps/boundaries-rna.geojson - this appears to have some kind of binary encoding.
Obviously the app dies, because the folium.GeoJson
constructor in fullscreen_map
tries to load this file via the web throws a JSON error.
Assuming this has something to do with http content-type header but not sure what the right approach is to fix. A new route handler only the geojson files?
This app is deployed as part of an AWS CDK v2 stack (python) but I don't think that's relevant.
This is a very low-traffic app and would prefer not to go through the hassle of setting up an nginx server just to serve 5 static files.
How can I fix this?
I expected a geojson file with the proper encoding to be served up by flask. So far I have not tried to solve this, but would likely create a new route handler with a Response set for just for the geojson.