你如何將 sounddv.py 添加到燒瓶然后在 html 檔案中運行?
我想在 popup.html 中列印此文本 text = "你一定在喃喃自語你已經被檢查過 " str(check_cnt) "times" 這是 py 代碼
我也想讓 sounddv.py 在我的其他 html 檔案中作業。
聲音檔案
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
彈出視窗.html
<!DOCTYPE html>
<html>
<head>Popup!</head>
<body>
<p>
</p>
</body>
</html>
這是 app.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 新手 :( 我愿意接受任何可能的評論!謝謝
uj5u.com熱心網友回復:
在 print_sound 中回傳文本。然后在您的燒瓶音頻檔案函式中呼叫它。還要在回傳渲染模板中添加變數,如下所示:
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)
uj5u.com熱心網友回復:
您的 print_sound 函式沒有任何回傳值。您應該在 func 中添加回傳值。我認為。
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/364830.html
下一篇:創建資料庫時回圈匯入
