Serving static html pages with flask on heroku - python

Im trying to figure out how to serve static pages from heroku with a flask app. I found this with some searching:
#app.route('/foo/<path:filename>')
def send_foo(filename):
return send_from_directory('/path/to/static/files', filename)
But, this would be quite inefficient. Is there a way to have the front facing server directly serve these files?

Normally you would do this with mod_rewrite with apache or similar, but afaik heroku doesn't let's you change the http server configuration.
You'll need to use the rack middleware, it lets you write url rewrite rules with ruby. (check this out : http://icelab.com.au/articles/useful-heroku-friendly-rewrites-with-rack-rewrite/ )

Related

How to host deploy my django project to firebase

I made this little django project, it shows weather of next three days of given city, its just a single page project, it looks like this
i want to deploy/host it on firebase
my project link here
But i have no idea how to do it, please help.
Edit
Ok, now i know that i can use cloud run for my backend and firebase for my frontend, can someone give me step by step procedure how to put my django files in cloudrun and firebase, and how to connect them, please
Firebase Hosting only hosts static content, which means it doesn't run your Python/Django code. But you can run the code on Cloud Run, and then integrate that with Firebase Hosting. See https://firebase.google.com/docs/hosting/cloud-run
You can upload dynamic or static content on firebase, in the case of a Django app it's dynamic but your stylesheets / scripts are static content.
In settings.py you have to specify a route for your static files and store them in, like : STATIC_ROOT = '/path/to/static'
Then in your server you have to specify that all the static files are stored in the above path.
Find more informations here : https://cloud.google.com/appengine/docs/standard/python3/serving-static-files

Using aws chalice to build a single page application?

Has anyone here ever worked with chalice? Its an aws tool for creating api's. I want to use it to create a single page application, but Im not sure how to actually serve html from it. I've seen videos where its explored, but I can't figure out how they actually built the thing. Anyone have any advice on where to go, how to start this?
You wouldn't serve HTML from Chalice directly. It is explicitly designed to work in concert with AWS Lambda and API Gateway to serve dynamic, API-centric content. For the static parts of an SPA, you would use a web server (nginx or Apache) or S3 (with or without CloudFront).
Assuming you are interested in a purely "serverless" application model, I suggest looking into using the API Gateway "Proxy" resource type, forwarding to static resources on S3.
Worth noting that it's probably possible to serve HTML from Chalice, but from an architecture perspective, that's not the intent of the framework and you'd be swimming upstream to get all the capabilities and benefits from tools purpose-built for serving static traffic (full HTTP semantics w/ caching, conditional gets, etc)
Add Response from Chalice and the use it to set the response headers and you're g2g.
from chalice import Chalice, Response
return Response(template, status_code=200, headers={"Content-Type": "text/html", "Access-Control-Allow-Origin": "*"})
I read about it here;
https://medium.com/#tim_33529/creating-a-serverless-blog-with-chalice-bdc39b835f75

Serve dynamic media in Flask with Nginx+uWSGI

I am really stuck with serving dynamically created content in Flask.
If I understand everything right, the only way to do it in Python code is to use Flask native send_file(). Is there a way to perform send_file not through Flask itself? It's extremely slow, I can't afford it :(
I know how to serve statics via nginx, but it seems to be not suitable in my case, cause it links a web address with the real path on my server. When I perform send_file(), the file does not have any web address (am I right?).
So, what should I do?
Go read about X-Accel-Redirect response header and how that can be used in conjunction with a nginx front end to have nginx serve up a file which has been written to the file system by a backend web application.
http://wiki.nginx.org/X-accel

Serving files through flask + redis

This might be a bit of a noob question, so I apologize in advance.
How do I make a web server running flask+redis serve binary files as a response to a link/query?
I want the response to the link to be either some AJAX action such as changing a div, or popping up an "unavailable" response, or to serve back some binary file.
I would like help both with the client side (jQuery / other Javascript) and the server side.
Thanks!
Side question: Would you choose redis for this task? Or maybe something else such as MongoDB, or a regular RDBMS? And why?
Normally you would configure your webserver so that URLs that refer to static files are handled directly by the server, rather than going through Flask.

Is there any way to upload a very large file use django on nginx?

I use django to run my website and nginx for front webserver ,
but when i upload a very large file to my site,
it take me very long time ,
there is some thing wrong when nginx hand upload large file;
the nginx will send the file to django after receive all my post file;
so this will take me more time;
i want to find some other webserver to replace the nginx;
wish your suggest?
You problem not in nginx you problem in nginx settings.
If you want handle files with django - you should change some params
Timeout when uploading a large file?
Else nginx may handle files itself
http://www.grid.net.ru/nginx/upload.en.html
Nginx is probably the best http server, there is no need to replace it. I will advise you to upload very large files via ftp or nfs share.
If you want to not pass file to your django application, then you should use:
fastcgi_pass_request_body off;
Also you may want to use the upload module: http://www.grid.net.ru/nginx/upload.en.html
Look at tornado at http://www.tornadoweb.org/ You may use it beside the django and handle file upload.
On my project I successfully use django with tornado, that handles API calls and long ajax requests.

Categories

Resources