如何将python中的列表提供给javascript?
我对Javascript相当陌生。我想知道,导入Python生成的列表是如何工作的?下面是我的想法的大致版本,我正在尝试使用javascript将3个列表导入到HTML文件中(如果任何语法不正确,请提前道歉):如何将python中的列表提供给javascript?,javascript,python,for-loop,Javascript,Python,For Loop,我对Javascript相当陌生。我想知道,导入Python生成的列表是如何工作的?下面是我的想法的大致版本,我正在尝试使用javascript将3个列表导入到HTML文件中(如果任何语法不正确,请提前道歉): 函数createMarkers(){ //导入由headline.py生成的文章列表 //从headline.py生成的导入URL //导入由headline.py生成的坐标 对于(i=0;i
函数createMarkers(){
//导入由headline.py生成的文章列表
//从headline.py生成的导入URL
//导入由headline.py生成的坐标
对于(i=0;i<25;){
物品名称=物品清单[i]
url=url[i]
坐标=坐标[i]
var marker[i]=WE.marker([coordinate]).addTo(earth);
bindpoop(“article_name
,{maxWidth:520,closeButton:true});
}
}
我是否需要实现某种web框架来执行python代码并将其输入到javascript代码中?我将如何进行此操作?您研究过模板语言吗?我使用Jinja2解决了类似的问题 这里有一些。 这里有一个教程 下面是一个例子 使用Jinja2将变量标记注入HTML Python代码:
import webapp2 # Le framework
import json # This is used to return the dictionary in a native (to JS) way
import jinja2 # The glue
# This example uses webapp2, but could be easily adapted to other frameworks.
jinja_environment = jinja2.Environment(
# sets the folder "templates" as root folder for html (can be tweaked to match you layout):
loader=jinja2.FileSystemLoader('templates'))
class Marker():
"""
creates marker objects that can then be rendered on the map.
"""
def __init__(self, latlng, title):
# below we split the comma-separated latlng into two values and store in an array.
coords = str(latlng).split(',')
self.lat = coords[0]
self.lng = coords[1]
self.title = title
class MainHandler(webapp2.RequestHandler):
def get(self):
# assembling the coordinates and descriptions into an array of Marker objects
markers = []
for marker in list_of_markers:
markers.append(Marker(latlng="coordinates go here in format 'latitude, longitude'",
title="Marker title goes here")
for marker in markers:
t_dict = {}
t_dict['m_title'] = marker.title
t_dict['m_lat'] = marker.lat
t_dict['m_lng'] = marker.lng
temp.append(t_dict)
json_markers = json.dumps(temp)
template_values = {'markers' :json_markers}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
# Initializing the webapp:
application = webapp2.WSGIApplication([('/', MainHandler)], debug=False)
// To be added to the initialize() function of the maps JS:
data = {{ markers }}
// Iterating over list of points to create markers:
data.forEach(function(p) {
var point = new google.maps.LatLng(p.m_lat, p.m_lng);
var marker = new google.maps.Marker({position: point,
map: map,
title: p.m_title,
//animation: google.maps.Animation.DROP,
});
});
Javascript代码:
import webapp2 # Le framework
import json # This is used to return the dictionary in a native (to JS) way
import jinja2 # The glue
# This example uses webapp2, but could be easily adapted to other frameworks.
jinja_environment = jinja2.Environment(
# sets the folder "templates" as root folder for html (can be tweaked to match you layout):
loader=jinja2.FileSystemLoader('templates'))
class Marker():
"""
creates marker objects that can then be rendered on the map.
"""
def __init__(self, latlng, title):
# below we split the comma-separated latlng into two values and store in an array.
coords = str(latlng).split(',')
self.lat = coords[0]
self.lng = coords[1]
self.title = title
class MainHandler(webapp2.RequestHandler):
def get(self):
# assembling the coordinates and descriptions into an array of Marker objects
markers = []
for marker in list_of_markers:
markers.append(Marker(latlng="coordinates go here in format 'latitude, longitude'",
title="Marker title goes here")
for marker in markers:
t_dict = {}
t_dict['m_title'] = marker.title
t_dict['m_lat'] = marker.lat
t_dict['m_lng'] = marker.lng
temp.append(t_dict)
json_markers = json.dumps(temp)
template_values = {'markers' :json_markers}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
# Initializing the webapp:
application = webapp2.WSGIApplication([('/', MainHandler)], debug=False)
// To be added to the initialize() function of the maps JS:
data = {{ markers }}
// Iterating over list of points to create markers:
data.forEach(function(p) {
var point = new google.maps.LatLng(p.m_lat, p.m_lng);
var marker = new google.maps.Marker({position: point,
map: map,
title: p.m_title,
//animation: google.maps.Animation.DROP,
});
});
如何运行javascript?如果它是在客户端浏览器中执行的(如果不使用node.js或rhino或其他东西,通常是这样)您无法从那里运行python代码,因为您无法访问客户端计算机。在这种情况下,您需要对web服务器进行AJAX调用,以便为您执行代码并将其反馈给客户端。这当然也取决于您的服务器端实现。您如何为您的应用程序提供服务?我希望使用类似VPS的e Digitalocean托管并运行Python代码。javascript位于HTML文档中。这是否意味着我需要进行AJAX调用(不管是什么)?非常感谢!我以前从未听说过这种语言…所以它非常新,但我会试一试。听起来和我需要的完全一样!这与Python或Flask不同吗?@PythonScraper此示例使用webapp2而不是Flask,但两者高度可互换(至少对于此任务而言)。如果您想要使用烧瓶的示例,只需留下注释。:)