2016-05-22 8 views
3

Ich möchte Follium-Map in die Jinja-Vorlage einfügen.Legen Sie die Folium-Karten in die Jinja-Vorlage

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 - jinja Vorlage für Flask

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

Meine Seite zeigt aktuelle Zeile:

<folium.folium.Map object at 0x00000000069D5DA0> 

Aber ich brauche Karte, die m erzeugt ethod follium_map.save ('map.html') in diesem Div-Block.

Wie kann ich das tun?

+0

je nachdem, was Sie möchten, tun könnte nur verwenden: https://github.com/rochacbruno/Flask-GoogleMaps. – wgwz

Antwort

1

Sie könnten Ihre generierte HTML mit folium_map.save('templates/map.html') speichern. Dann können Sie jinja2 zu {% include "map.html" %} verwenden. Das generierte HTML rendert eine Karte nicht, wenn sie wie angegeben in div Tags eingepackt wird. Wenn eine Kapselung erforderlich ist, ziehen Sie die Verwendung von iframes oder custom folium templates in Betracht.

Dateistruktur

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 %} 
Verwandte Themen