I'm trying to create a python package for a webapp (flask based) and I'd like to get some files installed in a known directory.
For example flask tries to find the templates
and static
directories inside the current package when running the integrated server (paste-based). But in production I'd rather like those directories installed in a more specific place like virtualenv/share/package-name/{static,templates}
. Otherwise they would be somewhere under lib/python-2.x/...
which doesn't seem appropriate.
I tried to install the files using the data_files
parameter in setup.py
, but that doesn't seem to be useful for whole trees (error: can't copy 'xxx': doesn't exist or not a regular file
).
What's the proper solution in this situation?
Ideally, you should use a dedicated HTTP server (like Apache or Nginx) in front of your Flask application to serve your static files in production. You need to create a folder called static in your application's root directory to store all your static files.
Folder structure for a Flask app That folder contains two folders, specifically named static and templates. The static folder contains assets used by the templates, including CSS files, JavaScript files, and images.
Flask – Static Files Usually, the web server is configured to serve them for you, but during the development, these files are served from static folder in your package or next to your module and it will be available at /static on the application. A special endpoint 'static' is used to generate URL for static files.
To deploy your Flask app, you can use PythonAnywhere. This puts your app online, for anyone to access. They maintain the server for you, so you don't have to. On top of that, it's free for small apps.
What you could do is package up your virtual env and static files into a native package (.deb or .rpm) and then you could place these anywhere you want. I wrote a blog post on this here. It uses a handy little ruby gem called fpm and although I dont seperate my static files from my python package you would be able to by adding another path mapping on the end of the fpm command like so
fpm -s dir -t deb -n food-truck -v 0.1 -d "python,python-dev,postgresql" /home/ubuntu/food_truck-build/=/home/ubuntu/foodtruck.com <path to static files>=<destination of static files>
You could use fpm to specify other config files such as your nginx config etc as well. But thats up to you, and might mean your deployment is a little less flexible.
Hope that helps!
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