[英]How to get this sounddv.py file to run inside the html file using flask?
如何将 sounddv.py 添加到 flask 然后在 html 文件中运行?
我想在弹出窗口中打印这个文本。html text = "你一定是在喃喃自语你已经被检查过"+str(check_cnt)+"次"这是py代码
我还想让 sounddv.py 在我的其他 html 文件中工作。
声音dv.py
import sounddevice as sd
from numpy import linalg as LA
import numpy as np
duration = 20
global cnt
global check_cnt
cnt = 0
check_cnt = 0
def print_sound(indata, outdata, frames, time, status):
volume_norm = np.linalg.norm(indata)*10
a = int(volume_norm)
global cnt
global check_cnt
if a <= 28:
cnt = cnt + 1
if cnt == 300:
check_cnt += 1
text = "You must be mumbling you have been checked "+str(check_cnt)+ "times"
cnt = 0
这是弹出窗口 html
popup.html
<!DOCTYPE html>
<html>
<head>Popup!</head>
<body>
<p>
</p>
</body>
</html>
这是 app.py 文件夹
应用程序.py
import sounddevice as sd
from flask import Flask, render_template, Response
from flask_bootstrap import Bootstrap
import pymysql
from sounddv.py import print_sound
app = Flask(__name__)
Bootstrap(app)
@app.route('/audio')
def audio_file():
with sd.Stream(callback=print_sound):
duration = 20
sd.sleep(duration * 1000)
return render_template("popup.html")
if __name__ == '__main__':
app.run(host='127.0.0.1',port=5000, debug=True)
我是 Python 的新手:(我愿意接受任何可能的评论!谢谢
在 print_sound 返回文本。 然后在您的 flask 音频文件 function 中调用它。 还在返回渲染模板中添加变量,如下所示:
return render_template('popup.html',text_var=text)
同样在 popup.html 使用 jinja ->{{text_var}}
def print_sound(indata, outdata, frames, time, status):
volume_norm = np.linalg.norm(indata)*10
text=''
a = int(volume_norm)
global cnt
global check_cnt
if a <= 28:
cnt = cnt + 1
if cnt == 300:
check_cnt += 1
text = "You must be mumbling you have been checked "+str(check_cnt)+ "times"
cnt = 0
return text
@app.route('/audio')
def audio_file():
with sd.Stream(callback=print_sound):
duration = 20
sd.sleep(duration * 1000)
text=print_sound(...)
return render_template("popup.html",text_var=text)
您的 print_sound function 没有任何返回值。 你应该在你的函数中添加返回值。 我认为。
import sounddevice as sd
from flask import Flask, render_template, Response
from flask_bootstrap import Bootstrap
import pymysql
from sounddv.py import print_sound
app = Flask(__name__)
Bootstrap(app)
@app.route('/audio')
def audio_file():
...
return render_template("popup.html",html_context)
if __name__ == '__main__':
app.run(host='127.0.0.1',port=5000, debug=True)
html:
<p> {{ html_context }} </p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.