I can serve the audio as long as they are under .. /project/static. But let's say I want to serve an audio called "tune.wav" from /home/name/Music. How do I do it?
<audio>
<source src="/static", filename="some.wav">
</audio>
The above snippet is from the template. Additionally what do I need to modify in the snippet?
To reference the static files using the url_for() function, pass in the name of the directory – static in this case – and the keyword argument of filename= followed by your static file name. Flask automatically creates a static view that serves static files from a folder named static in your application's directory.
Folder structure for a Flask app The static folder contains assets used by the templates, including CSS files, JavaScript files, and images. In the example, we have only one asset file, main. css. Note that it's inside a css folder that's inside the static folder.
The Flask Framework looks for HTML files in a folder called templates. You need to create a templates folder and put all your HTML files in there.
Create flask route with send_file() or send_from_directory()
:
@app.route('/music/<path:filename>')
def download_file(filename):
return send_from_directory('/home/name/Music/', filename)
Your HTML code will look like this:
<audio>
<source src="/music/tune.wav">
</audio>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With