Generating maps with folium/flask only works the first time, subsequent requests return the same map

22 Views Asked by At

Cut out some parts but this is how i'm trying to save it I would really want to be able to use the same file name and resave over the map_file_path which I have defined already. It works the first time but any subsequent times when making requests, it takes me back to the original map that I created..

@app.route("/", methods=['GET', 'POST'])
def landing_page():
    if request.method == 'POST':
        address = request.form.get('address', '')
        print(address)
        county_name, lat, lng = geocode_address(address)
        if not county_name:
            return "County geocoding failed, please try again.
        if counties_data:
            folium_map = folium.Map(location=[lat, lng], zoom_start=13)
            # Add GeoJSON layers for counties
            for county_data in counties_data:
                GEOID, county_name, geometry_wkt, crime_percentile = county_data
                if geometry_wkt:
                    geojson = wkt_to_geojson(geometry_wkt)
                    folium.GeoJson(
                        geojson,
                        name=county_name,
                        style_function=lambda feature, crime_percentile=crime_percentile: {
                            'fillColor': color_scale(crime_percentile),
                            'color': 'rgba(0, 0, 0, 0)',
                            'weight': 1,
                            'fillOpacity': 0.3
                        },
                    ).add_to(folium_map)
            # add a marker for the provided address
            folium.Marker([lat, lng], popup=f'<b>{address}</b>').add_to(folium_map)
        else:
            return f"No matching county found in the database for '{county_name}'."
        folium_map.save(map_file_path)
        return redirect(url_for('show_map'))
    return render_template('landing_page.html')

@app.route("/map")
def show_map():
    return render_template('map_view.html')

if __name__ == "__main__":
    app.run()
0

There are 0 best solutions below