Joshua
Joshua

Reputation: 11

Serve flask files from another drive

I am trying to take files from an external hard drive and use it in my HTML pages. Since i am using Flask as my frame work i am having great difficult accessing anything outside of the virtual environment. I have tried to change the static folder but i have found no effective way of completing this. It would be great if there is a way that i can route all HTML static files to my external hard drive. At the moment everything is working accept the video file.

So far my init.py looks like this

from flask import Flask


app = Flask(__name__)
app.config.from_object('config')

from app import views

And my html file looks like this

<!DOCTYPE html>
<html>
  <head>
    <style>
      body {background-image: url(static/B1.jpg);}
    </style>
    <title>Server</title>
  </head>
  <body>
    <video width="original" height="original" controls>
      <source src="static/movie.mp4" type="video/mp4">
    </video> 
  </body>
</html>

Thanks for the help everyone :) could i change the html to src to something like this

      <source src="../../../../../E:/Movies/movie.mp4" type="video/mp4">

And if i can't why does that work with just opening an html with that source?

I think my problem is that flask can't route a file to the html is there a way to route the file?

Upvotes: 1

Views: 2228

Answers (1)

Oliver
Oliver

Reputation: 29581

All static files must be in static folder. Some oprions you have:

  1. Copy the files that are elsewhere on your filesystem into that folder
  2. Add some code to your server script that copies the files from their origin to the static files folder
  3. Put your server script on your external drive, so your static folder is on that drive, and run it from there. If you need to serve content from several folders on that drive you might be able to create shortcuts to those folders under static folder but I can't test right now , on Windows I doubt this will work (on Linux very likely that symbolic links would work).
  4. You could have one flask app running for each different folder where you have static content, listening on a different port. Those apps would all be identical. Your HTML served by your main app would have links with those ports.
  5. Find where in the source flask looks for the source folder, and extend by a few lines of code to be able to look in other folders.
  6. Use flask.send_file and flask.send_file_from_directory: as long as your database contains a mapping of file ID's to file paths, and you put the file ID's in the HTML served to client, then when client requests the file, your flask app uses the ID in the URL to determine file location and uses flask.send_file_from_directory to serve the file.
  7. You might also be able to use Flask Blueprints since each blueprint can have its own static folder

Upvotes: 3

Related Questions