将Python信息发送到JavaScript文件

将Python信息发送到JavaScript文件,javascript,python,Javascript,Python,我不熟悉Python和JavaScript,并且(尝试)使用cola.js。我的HTML表单将信息发送到Python,Python将其转换为一个字典数组 Class_Name(ndb.Model): class_title = ndb.JsonProperty() def post(self): classname = self.request.get('classname') #user inputs 1 classname prereq = self.request.

我不熟悉Python和JavaScript,并且(尝试)使用cola.js。我的HTML表单将信息发送到Python,Python将其转换为一个字典数组

Class_Name(ndb.Model):
    class_title = ndb.JsonProperty()
def post(self):
    classname = self.request.get('classname') #user inputs 1 classname
    prereq = self.request.get('prereq') #user inputs 1 prereq
    new_dictionary = {}
    new_dictionary [classname] = prereq
    new_class = Class_Name(class_title = new_dictionary) #stores as a dictionary
    new_class.put()

    Class_data = Class_Name.query().fetch() #gets all instances of Class_Name

   *** code that goes in b/w sends back to input form if want to add more classes, else, goes to below code

        output = []
        for a in Class_data:
            jsonprop = a.class_title
            extracted_output = json.dumps(jsonprop)
            output.append(extracted_output)
        template_vars= {'Class_data': output}
        template = jinja2_environment.get_template('template/post.html')
        self.response.write(template.render(template_vars))
这是到目前为止我的基本代码。我想使用cola.js将我的信息转换成一个图表,基本上映射出每个类及其先决条件。但是,cola.js格式是如下所示的JavaScript文件:

graph = {
  nodes: [
  {
    id: 'A'
  }, {
    id: 'B'
  }, {
    id: 'C'
  }
],

links: [
  {
    id: 1,
    source: 'A',
    target: 'B'
  }, {
    id: 2,
    source: 'B',
    target: 'C'
  }, {
    id: 3,
    source: 'C',
    target: 'A'
  }
]
};
有什么方法可以让JavaScript获取我的Python数组,并像这样将信息输入JavaScript文件

graph = {
  nodes: [
  {
    id: 'Class1' **will put actual class name
  }, {
    id: 'Class2'
  }
],

links: [
  {
    id: 1,
    source: 'Class1',
    target: 'prereq'
  }, {
    id: 2,
    source: 'Class2',
    target: 'prereq'
  }
]
};
这是一段将Python信息转换为JavaScript格式的粗略代码

nodes_array = []
nodes_dict = {}
links_array = []
links_dict = {}
graph_array = []

#loops through every element
for a in Class_data:
    jsonprop = a.class_title
    extracted_output = json.loads(jsonprop)

for c_class, prereq in extracted_output.iteritems():
    # for the links dictionary  
    counter_link = 1
    # creates {'id':'class1'}
    nodes_dict['id'] = c_class
    #creates [ {'id':'class1'},  {'id':'class2'}]
    nodes_array.append(nodes_dictionary)
    # creates {'id': 'counter', 'source': 'class1', 'target': 'prereq'}
    links_dictionary[id] = counter_link
    counter_link++
    links_dictionary[source] = c_class
    links_dictionary[target] = prereq
    # creates [{'id': 'counter', 'source': 'class1', 'target': 'prereq'}]
    links_array.append(links_dictionary)
    #creating the format --> 'nodes': [  {id: class1}, {id:class2},  {id:class3} ]"
    #creating the format --> 'links': [  {id: 1, source :class2, target :class3} ]"
    graph[nodes] = nodes_array
    graph[links] = links_array

您的Python脚本可以使用JavaScript理解的格式将图形写入文件

如果在Python中执行此操作:

import json

graph = {
    'nodes': [ {'id': 'Class1'}, {'id': 'Class2'} ],
    'links': [
        { 'id': 1, 'source': 'Class1', 'target': 'prereq' },
        { 'id': 2, 'source': 'Class2', 'target': 'prereq' }
    ]
}

with open('graph.js', 'w') as out_file:
  out_file.write('var graph = %s;' % json.dumps(graph))
结果是一个名为
graph.js
的文件,其中包含以下内容:

var graph = {"links": [{"target": "prereq", "source": "Class1", "id": 1}, {"target": "prereq", "source": "Class2", "id": 2}], "nodes": [{"id": "Class1"}, {"id": "Class2"}]};

如果在加载自己的JavaScript文件之前加载
graph.js
,可以参考变量
graph
来使用图形。

可以通过编程方式生成JavaScript文件;但通常更好的解决方案是,JavaScript将使用例如
jQuery.ajax()
来获取数据,可能您正在寻找这个方法。感谢您之前的评论!我将尝试这样做,看看我是否遇到任何麻烦。如果你对我的答案满意,请接受它。我更新了我的代码,想知道如何将我的字典格式化为{id:'class'}而不是{id':'class'},这是我从我的文件中得到的。你对我的代码还有什么建议吗?谢谢{id:'class'}和{id:'class'}之间没有语义上的区别.JavaScript对它们的处理方式完全相同。就文件而言,这是JSON所需的语法。我不知道为什么双引号会困扰您,但如果双引号困扰您,您就不能使用这种简单的方法。您必须编写自己的Python代码以生成所需格式的输出。我怀疑这项工作不值得我这么做t、 你的代码在我看来还可以。如果你对此有具体问题,你可能应该发布另一个问题。我对这个问题的回答是否令你满意?我不懂任何JavaScript,所以我认为JavaScript必须有特定的语法才能工作。非常感谢你的帮助!