Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play audio from outside static folder in Flask?

Tags:

python

flask

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?

like image 872
Aayush Karki Avatar asked May 24 '17 11:05

Aayush Karki


People also ask

How do I access my static folder in Flask?

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.

What is static folder in Flask?

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.

In which folder we have to store all pages which are user viewable pages for Flask?

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.


1 Answers

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>
like image 109
Bartek Jablonski Avatar answered Sep 25 '22 20:09

Bartek Jablonski