2016-05-22 3 views
3

Je souhaite insérer une carte follium dans le modèle jinja.Insérer les cartes folium dans le modèle jinja

run.py

from flask import Flask, render_template 

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() 
    return render_template('index.html', folium_map=folium_map) 


    if __name__ == '__main__': 
    app.run(debug=True) 

template/index.html - modèle de Jinja pour Flask

{% extends "layout.html" %} 
{% block title %}Test{% endblock %} 
{% block head %} 
{{ super() }} 
{% endblock %} 
{% block body %} 
**<div><!--Folium map here-->{{ folium_map }}</div>** 
{% endblock %} 

Mon site montre la ligne courante:

<folium.folium.Map object at 0x00000000069D5DA0> 

Mais je besoin de carte qui génère m ethod follium_map.save ('map.html') dans ce bloc div.

Comment est-ce que je peux faire ceci?

+0

en fonction de ce que vous faites, vous pouvez utiliser simplement: https://github.com/rochacbruno/Flask-GoogleMaps. – wgwz

Répondre

1

Vous pouvez enregistrer votre code html généré avec folium_map.save('templates/map.html'). Ensuite, vous pouvez utiliser jinja2 au {% include "map.html" %}. Le html généré ne restitue pas une carte lorsqu'il est entouré de balises div comme indiqué, si l'encapsulation est nécessaire, pensez à utiliser iframes ou custom folium templates.

structure de fichier

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

<!DOCTYPE HTML> 
<head> 
    <title>{% block title %}{% endblock %}</title> 
</head> 
<body> 
    <header>{% block head %}{% endblock %}</header> 
    {% block body %}{% endblock %} 
</body> 
</html> 

index.html

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