gpt4 book ai didi

javascript - 将 Python 信息发送到 JavaScript 文件

转载 作者:行者123 更新时间:2023-11-28 06:59:34 26 4
gpt4 key购买 nike

我是 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 脚本可以使用 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,则可以引用变量 graph 来使用您的图表。

关于javascript - 将 Python 信息发送到 JavaScript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32284317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com