how do you add sounddv.py to flask and then run in html file?
I want to print this text in popup.html text = "You must be mumbling you have been checked " str(check_cnt) "times" this is the py code
Also I want to make that sounddv.py work inside my other html file.
sounddv.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
This is the popup html
popup.html
<!DOCTYPE html>
<html>
<head>Popup!</head>
<body>
<p>
</p>
</body>
</html>
this is the app.py folder
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)
I'm new to Python :( I'm opened for any possible comments! Thanks
CodePudding user response:
Your print_sound function does not have any return value. You should add return value in your func. I think.
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>
CodePudding user response:
In print_sound return text. Then call it inside your flask audio file function. Also add the variable in return render template like this:
return render_template('popup.html',text_var=text)
Also in popup.html use 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)