Reputation: 45
Using flask. I have made an internal file browser/media player. This is for local network only so everyone who has access to the page has access to these files all ready. Nevertheless I am dealing with 1000s of files in 1000s of locations. Is it possible to source a file in a html video player, or a img src that is local. The source files cant be moved, so cant go to the static folder etc... like this
<video src="{{ clip }}" autoplay controls></video>
when clip is the file_path /projects/project_234/video/video_file.mov
I have all the variables needed just not able to get the file to play.
EDIT 01
It has come to my attention that mov files dont play in chrome only mp4's.
@app.route('/projects/<project>/<clip>', methods=['GET'])
def project_page_clip(project, clip):
file_path = request.args.get('file_path')
file_location = file_path
file_name = '90Sec_Approval.mp4'
if file_name:
return send_from_directory(file_location,file_name)
return render_template("project_selected_clip.html", file_path=file_path,
title=project, project=project, clip=clip)
So when clicked on the previous page this just opens the clip on a browser without rendering the project_selected_clip.html template
How can I get it to use the return send from directory as a src on the page instead?
Upvotes: 2
Views: 1565
Reputation: 577
Here is an answer to clarify how to use the send_file
approach in an app setting with app.route
and a url_for
.
import os
from flask import Flask, url_for, send_file
app = Flask(__name__)
# set the path where you store your assets - it can be outside of the root dir
app.config['CUSTOM_STATIC_PATH'] = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../archive'))
# have a route listening for requests
@app.route('/this/can/be/anything/<path:filename>')
def send_media(filename):
path = os.path.join(app.config['CUSTOM_STATIC_PATH'], filename)
return send_file(path)
# in your template, you can then use the following
# <img src="{{ url_for('send_media', filename='<dir>/<filename>') }}">
Upvotes: 1
Reputation: 21
So after that... what works for me is to include this;
from Flask import send_file
top_dir = 'top/'
mid_dir = 'mid/'
more_dir = '/and/this/'
filename = 'some_photo.jpg'
@wip.route('/photo')
def photo():
return send_file(top_dir+mid_dir+more_dir+filename)
serves up the file!
Upvotes: 1
Reputation: 45
After much deliberation, the best of many evils was to generate symlinks for the fles
Upvotes: 2