So I am doing a homework about using leaflet to make a map, and I need to use flask to render it. But when I try to get the data in html, I will just get an empty page. If I delete it, everything seems fine, where's the problem? Am I using the wrong way to get the data? The locations and their info are stored in a text file, which looks like this:
"University of Oregon",44.050,-123.0750,"1585 E 13th Ave", "Eugene, OR", "(541) 346-1000"
"The Old Pad",44.020569,-123.080647, "3355 East Amazon Dr", "Eugene OR", "(541) 686-5022"
"Destination Events",44.053141,-123.134144,"544 Conger Street","Eugene OR","(541) 345-4476"
"Docs Pad",44.052003,-123.092848,"710 Willamette Street","Eugene OR", "(541) 343-0224"
And here is the flask code:
import flask
from flask import render_template
from flask import url_for
from flask import request
import logging
app = Flask(__name__)
import CONFIG
app.secret_key = CONFIG.COOKIE_KEY
@app.route('/')
@app.route("/index")
def index():
destination_list=[]
lan_list =[]
lon_list =[]
street_list =[]
city_list=[]
phone_num_list =[]
#open the file and store them, pass the result to map
flask.session["destination"]=destination_list
flask.session["lan"]=lan_list
flask.session["lon"]=lon_list
flask.session["street"]=street_list
flask.session["city"] = city_list
flask.session["phone_num"]=phone_num_list
with open('my_file.txt', 'r') as infile:
new_line = infile.split(',')
#destination becomes-> pop_dict["destination"] = [university of oregon, some name,some name]
flask.session["destination"].append(new_line[0])
flask.session["lan"].append(new_line[1])
flask.session["lon"].append(new_line[2])
flask.session["street"].append(new_line[3])
flask.session["city"].append(new_line[4])
flask.session["phone_num"].append(new_line[5])
return flask.render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
app.logger.setLevel(logging.DEBUG)
print("Opening for global access on port {}".format(CONFIG.PORT))
app.run(port=CONFIG.PORT, host="0.0.0.0")
else:
app.debug=False
And here is my HTML Code, without getting the data from sessions, everything looks fine:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>A Leaflet map!</title>
<link rel="stylesheet" href="http://ift.tt/1KzQLRC" />
<script src="http://ift.tt/20iI1kL"> </script>
<style>
#map{ height: 100% }
</style>
</head>
<body>
<div id="map"></div>
<script>
//it will be a integer
// initialize the map, starting view at university of oregon
var map = L.map('map').setView([44.0519, -123.0867], 15);
//example marker on U of O
var marker = L.marker([44.0519, -123.0867]).addTo(map);
var det = {{session.destination_list | safe}};
marker.bindPopup("<b>University of Oregon<br>123456-111"+"num of poi:").openPopup();
// load a tile layer
L.tileLayer('http://ift.tt/1KYs1SY}', {
attribution: 'Map data © <a href="http://ift.tt/jbBAGS">OpenStreetMap</a> contributors, <a href="http://ift.tt/h5Zety">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18,
id: 'vdwhite.p3cag7d2',
accessToken:'pk.eyJ1IjoidmR3aGl0ZSIsImEiOiJjaWtjOTFoOWwwbHc0dm9tNGptaWZ5dHF6In0.F77aBVvYBbItCBed4PD1uQ'}).addTo(map);
</script>
</body>
</html>
If I am adding a variable:
var destination = {{session.destination}};
into the script, I will get an empty page, if I delete that, the page will be fine.
Why?
Aucun commentaire:
Enregistrer un commentaire