flask html按钮调用python函数



我正在使用Flask制作一个web服务。我想播放前端烧瓶静态文件夹中的midi文件。下面是app.py代码的一部分。我想运行的函数是python pygame库的一个函数,这个函数播放midi文件。我已经在background_process_test((中实现了函数。我写这段代码是参考这篇文章的。Flask-在按钮OnClick事件上调用python函数

@app.route('/uploader', methods=['GET', 'POST'])
def wav_transform():
if request.method == 'POST':
f = request.files['file']
if(request.form['length']):
random_length = request.form['length']
random_length = int(random_length)
else:
random_length = 100
f.save(f'static/original_song.wav')
...
midi = controller.MakeMidi(random_song, bpm, "static/random.midi")
midi.makemidi()
return render_template('json.html')
@app.route('/background_process_test')
def background_process_test():
pygame.mixer.init()
freq, size, chan = pygame.mixer.get_init()
BUFFER = 3072
filename = "static/random.midi"
pygame.mixer.init(freq, size, chan, BUFFER)
pygame.init()
pygame.mixer.init()
clock = pygame.time.Clock()
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
print("play song")
while pygame.mixer.music.get_busy():
clock.tick(1000)
return "nothing"

下面是json.html代码。

<!DOCTYPE html>
<html>
<head>
<title> play and download </title>
<meta charset="UTF-8">
<style>
li label{
width:121px;
float:center;
font-size:20px;
font-weight:bold;
line-height:25px;
}
body{
backgound-size: cover;
font-family: 'Open Sans', sans-serif;
background: #a27fb2 fixed center;
text-align:center
}
h1, h2, h3, h4, h5, h6{
font-family: 'Montserrat', sans-serif;
margin-top:30px;
background: #fff;
font-size:40px;
color:#a27fb2
}
fieldset{
margin: 40px 15px;
}
li{
list-style-type:none;
color: #fff;
}
button{
width:200px;
background-color:#fff;
border:none;
color:#a27fb2;
padding:10px 0;
text-align:center;
text-decoration:none;
display:inline-block;
font-size:20px;
font-weight:bold;
margin:4px;
cursor:pointer;
}
</style>
</head>
<body>
<fieldset>
<legend> </legend>
<h3>play and download</h3>
<li>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
$(function(){
$('a#test').bind('click', function() {
$.getJSON('/bacground_process_test',
function(data){
// do nothing
});
return false;
});
});
</script>
</li>
<li>
<div class='container'>
<form>
<a herf=# id=test><button class='btn btn-default'>Test<button></a>
</form>
</div>
</li>
</fieldset>
</body>
</html>

总之,我希望background_process_test((函数在前端单击按钮时运行,以便播放midi文件。当我在本地服务器上运行此代码时,midi文件没有播放127.0.0.1-[13/11/2019 21:50:56]"GET/bacground_process_test HTTP/1.1"404-。

事实上,我的目的是播放一个midi文件,我之所以尝试这样做,是因为在Python中很难将midi文件转换为wav文件。或者让我知道是否有其他方法可以在Flask的静态文件夹中以html格式播放midi文件。

您拼错了background。注意你写了bacground_process_test:

$(function() {
$('a#test').bind('click', function() {
$.getJSON('/bacground_process_test', // << HERE
function(data) {
// do nothing
});
return false;
});
});

在CCD_ 3文件中。

最新更新