I would like to use a class Track to have a function playsound() and this should play the sound on a html template.
This is my class :
class Track:
def __init__(self, name, path):
self.name = name
self.path = path
def playsound(self):
# does something
Now I know that I will need javascript and ajax as well probably. But I'm still new to the concept and I would like to see how I would go about achieving that. The sound file should play onload and should simply be invoked when you type:
songname = Track('name', 'music/filename.ogg')
#app.route('/playSong')
def playSong():
return songname.play()
If there is no way this can be done in this way, or there is a much better way, I'd be happy to be enlightened. Thanks in advance!
You can keep the src of in HTML as
<audio controls>
<source src="http://127.0.0.1:5000/a" type="audio/wav"/>
</audio>
where http://127.0.0.1:5000/a is the url which send_file the audio
python code :
#app.route('/a')
def returnAudioFile():
path_to_audio_file = pwd+"\\question_folder\\audio.wav" #audio from project dir
return send_file(
path_to_audio_file,
mimetype="audio/wav",
as_attachment=True,
attachment_filename="test.wav")
If I understood the question correctly, you should put an <audio> HTML element in your template and go from there. Flask will only serve the rendered template, it can't play the sound directly.
Related
I am looking for a way different possible image source in my html code, depending on result of a python function.
Exemple if:
state = isOnline()
is a function that can say if a device is online or not, it returns:
True
then I would obtain
IMG_URL = imgSource(state)
would return the source for online image
static 'project\img\true.jpg'
which I would then through my views.py used as:
def device(request):
return render(request, 'device.html', {'IMG_URL': IMG_URL})
and then I could use this variable in my html code.
<img src="{% IMG_URL %}" alt="Post">
I hope you guys will be capable to help me, thanks !
You can use something like:
def device(request):
# this could be generated in any number of ways, but this is a simple one
IMG_URL = 'http://example.com/online_image.jpg' if is_online() else '/local/img/offline_image.jpg'
return render(request, 'device.html', {'IMG_URL': IMG_URL})
There are some ideas for checking if you're online or not here.
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?
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>
This question already has an answer here:
Can't play HTML5 video using Flask
(1 answer)
Closed 7 years ago.
I have a simple flask server. I downloaded, using pafy, a video from a youtube link provided by the user.
#app.route('/')
def download():
return render_template('basic.html')
The basic.html template has a form that submits an action to download:
<form action="download_vid" method="post">
Link: <input type="text" name="download_path"><br>
<input type="submit" value="Submit">
</form>
I have another end point, /download_vid that looks like this.
#app.route('/download_vid', methods=['POST'])
def download_vid():
url = request.form['download_path']
v = pafy.new(url)
s = v.allstreams[len(v.allstreams)-1]
filename = s.download("static/test.mp4")
return redirect(url_for('done'))
The desired link is indeed downloaded as a .mp4 file in my static folder. I can watch it and I can also use it as a source for a tag in an HTML file, if I open it locally.
#app.route('/done')
def done():
return app.send_static_file('test.mp4')
From what I understand, 'send_static_file' serves files from the static directory. However, I get a 404 error when I run the server, even though the video is clearly there.
I have also tried a different version for done():
#app.route('/done')
def done():
return return render_template('vid.html')
Here, vid.html resides in templates and has a hard coded path to static/test.mp4. It is loaded after the download is complete. I do not have a 404 error in this case, but the tag don't do anything, it's just gray. If I open vid.html locally (double click on it), it works, it shows the video.
Can you please help me understand what is going on?
What I want to achieve is this:
Take an input from the user [ Done ]
Use that input to download a video [ Done ]
Serve that video back to the user [ ??? ]
I think you have something going on with file paths or file permissions.
Is the video being downloaded into static directory?
Is the static directory in the same directory, along with your main.py file?
Does your flask app have permissions to read the file?
I think the reason your file did not load in html template is because you referenced it as static/test.mp4 from an url - /done which translates the video path to be /done/static/test.mp4.
Instead of trying to push the file using Flask, you can redirect to the actual media file.
#app.route('/done')
def done():
return redirect('/static/test.mp4')
I'm trying just for learning how to serve a video with the blobstore without it takes all the screen the video, for example
here I imported Video as video_model
class ViewVideo(webapp.Reque...,blobstore_handlers.BlobstoreDownloadHandler):
def get(self):
video_id = self.request.get('video_id')
video_instance = None
if video_id:
video_instance = video_model().get_video_content(video_id)
self.response.headers['Content-Type'] = 'video/mp4'
self.send_blob(video_instance.content.key())
class Video(db.Model):
content = blobstore.BlobReferenceProperty()
title = db.StringProperty()
def get_video(self,video_id):
return Video.get_by_id(video_id)
def get_video_content(self,content):
query_str = "SELECT * FROM Video WHERE content =:content"
return db.GqlQuery(query_str,content=content).get()
Where the video_id came from a url given, but as you see I put it directly in send_blob() function and this one when I tested it takes all the screen just to see the video, I was wondering how can I serve the video from my application without happening this, I was thinking embedded HTML but I can't figure it out how the source will be
Any help will be grateful
If it lacks of content to answer the question I will edit it
Without HTML5, it's a tricky mess. With HTML5, it becomes easy & elegant. Serve to the user's browser, as part of whatever page you're serving, the following HTML (5) snippet:
<video width="320" height="240" controls>
<source src="/getmp4?video_id=whatever" type="video/mp4">
Your browser does not support the video tag: please upgrade it!
</video>
and use that ViewVideo handler to serve only the /getmp4 URL, not the URL that your user directly gets via their browser.
The 320, 240, and the choice to show controls, are all optional, of course -- as even more is the use of whatever for the video id!-)
Im trying to display the content of a text file in a template without any luck so far. This is
my code so far:
#route('/show_article/<filename>')
def show_article(filename):
stat_art=static_file(filename, root="articles")
return template('show_article', stat_art=stat_art)
And this is the paragraph in my template to display the content of the file
<p>
{{stat_art}}
</p>
I know that I could just return the static_file() but I will need to design the page with
some css and stuff later.
Thanks in advance and sorry for if my english is not correct!
You've misunderstood what static_file does.
Luckily, the fix is simple: just read the file yourself and pass its contents to the template, like so:
#route('/show_article/<filename>')
def show_article(filename):
with open(filename) as f: # <-- you'll need the correct path here, possibly including "articles"
stat_art = f.read()
return template('show_article', stat_art=stat_art)
That should do the trick.
[Btw, nice first question!]