2015-08-29 55 views
1

我是Python和JavaScript的新手,尝试使用cola.js.我的HTML表单将信息发送给Python,从而将它变成一个字典数组。发送Python信息到JavaScript文件

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 
+0

您可以编程生成JavaScript文件;但通常更好的解决方案是,JavaScript将获取数据使用例如'jQuery.ajax()' –

+0

可能你正在寻找这个http://stackoverflow.com/questions/7020135/passing-json-data-to- the-front-end-using-django –

回答

0

你的Python脚本可以使用the json module写的图形到一个文件中的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,你可以参考到变量来使用你的图。

+0

谢谢您的评论!我会试图做到这一点,看看我是否遇到任何麻烦。 – amay20

+0

如果您对我的回答满意,请您接受吗? –

+0

我更新了我的代码,并想知道如何将我的字典格式化为这样{id:'class'}而不是{“id':”class“},这正是我从我的文件中得到的。你也有我的代码的任何提示吗?谢谢 – amay20