下面是我的代码。 在这里,我要返回一个json数据对象。
def db_dropdown(): # Execute query
db_path = "C:\sqlite\marketoperation.db"
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute('''SELECT id,name FROM Generator''')
row_headers = [x[0] for x in c.description] # this will extract row headers
rv = c.fetchall()
print(rv)
json_data = []
for result in rv:
json_data.append(dict(zip(row_headers, result)))
print('executed')
print(json.dumps(json_data))
data=json.dumps(json_data)
return data
“数据”的格式为: [{“ id”:1,“ name”:“ a1”},{“ id”:2,“ name”:“ a2”}] 这是我的烧瓶代码,我在其中调用try.html并传递json数据。
@app.route('/trying', methods=['POST', 'GET'])
def index1():
list_tested = db_dropdown()
print(list_tested)
return render_template("try.html", trial1=list_tested)
这是我的try.html
<body>
<p>Hello World</p>
<select id="gendropdown" name="gendropdown">
</select>
<button type="submit" onclick="clicked()">Submit</button>
<script>
var trail1 = {{trail1}}
var jobj = JSON.parse(trail1);
let dropdown = document.getElementById('gendropdown');
dropdown.length = 0;
let defaultOption = document.createElement('option');
defaultOption.text = 'Choose State/Province';
dropdown.add(defaultOption);
dropdown.selectedIndex = 0;
let option;
for (let i = 0; i < jobj.length; i++) {
option = document.createElement('option');
option.text = data[i].name;
option.value = data[i].id;
dropdown.add(option);
</script>
</body>
我试图以下拉列表的形式发布json数据。但它不起作用。 请提出更改建议。我想以下拉格式显示json数据。
答案 0 :(得分:1)
这里在工作Example
var promises = text_chunk_array.map(text_chunk => {
return new Promise((resolve, reject) => {
var textToSpeech = new TextToSpeechV1({
iam_apikey: local_settings.TEXT_TO_SPEECH_IAM_APIKEY,
url: local_settings.TEXT_TO_SPEECH_URL
});
var synthesizeParams = {
text: text_chunk,
accept: 'audio/mp3',
voice: 'en-US_AllisonV3Voice'
};
textToSpeech.synthesize(synthesizeParams, (err, audio) => {
if (err) {
console.log("oopsie, an error occurred: " + err);
return reject(err);
}
resolve(audio);
});
});
});
// wait till all promises have finished
try {
var audio_files = await Promise.all(promises);
var audio_files_length = audio_files.length;
console.log("audio_files_length: " + audio_files_length);
if (audio_files_length === 1) {
var audio = audio_files[0];
} else {
// here's the magic
var combinedStream = CombinedStream.create();
audio_files.forEach((audio) => {
combinedStream.append(audio);
});
}
// BEGIN write audio to file, send it to client, then delete it from server
var absPath = path.join(__dirname, "/my_files/", file_name);
var relPath = path.join("./my_files", file_name); // path relative to server root
var write_stream = fs.createWriteStream(relPath);
combinedStream.pipe(write_stream);
write_stream.on('finish', function() {
// download the file (using absPath)
res.download(absPath, (err) => {
if (err) {
console.log(err);
}
// delete the file (using relPath)
fs.unlink(relPath, (err) => {
if (err) {
console.log(err);
}
console.log("FILE [" + file_name + "] REMOVED!");
});
});
});
// END write audio to file, send it to client, then delete it from server
} catch (err) {
console.log("hola, there was an error: " + err);
}