Insert the folium maps into the jinja template

前端 未结 3 461
误落风尘
误落风尘 2021-02-02 15:34

I want to insert follium map into the jinja template.

run.py

from flask import Flask, render_template

app = Flask(__name__)



@app.rou         


        
3条回答
  •  死守一世寂寞
    2021-02-02 16:27

    You could save your generated html with folium_map.save('templates/map.html'). Then you can use jinja2 to {% include "map.html" %}. The generated html does not render a map when wrapped in div tags as indicated, if encapsulation is necessary consider using iframes or custom folium templates.

    file structure

    myapp
    ├── run.py
    └── templates
        ├── index.html
        └── layout.html
    

    run.py

    from flask import Flask, render_template
    import folium
    
    app = Flask(__name__)
    
    @app.route('/')
    def index():
        start_coords = (46.9540700, 142.7360300)
        folium_map = folium.Map(location=start_coords, zoom_start=14)
        folium_map.save('templates/map.html')
        return render_template('index.html')
    
    if __name__ == '__main__':
        app.run(debug=True)
    

    layout.html

    
    
      {% block title %}{% endblock %}
    
    
      
    {% block head %}{% endblock %}
    {% block body %}{% endblock %}

    index.html

    {% extends "layout.html" %}
    {% block title %} Test {% endblock %}
    {% block head %} {{ super() }} {% endblock %}
    {% block body %}
        {% include "map.html" %}
    {% endblock %}
    

提交回复
热议问题