Forums

Another person struggling with static files

I know this is a common problem with new users like me and I know it's been addressed many times, I've searched a lot, but still unsure how to fix my problem.

This is my first time trying to host my application on python anywhere, it works like a charm on my local machine, but it just does not want to cooperate here.

I have a small application that asks the user to upload an image, and this image will be stored in:

/home/me/mysite/static/uploads/

The user selects the image file, hits an upload button, and the image gets stored in "uploads". This is working fine.

If I upload the "example.jpg" image and then open the "static/uploads" folder, I can see the example.jpg image there.

After that, there is a second button that the user can press that will do some image transformations with the uploaded image, and this is where my problem starts. Basically, I have

@app.route('/readimage/<filename>', methods=['POST', 'GET'])
def readimage(filename):

img_path = '/home/me/mysite/static/uploads/'+filename

image = Image.open(img_path).convert('RGB')

in_transform = transforms.Compose([
                    transforms.Resize(size=(244, 244)),
                    transforms.ToTensor()])

image = in_transform(image)[:3,:,:].unsqueeze(0)

return filename

Here I have filename passed from the previous page and I'm returning "filename" just for debugging, I know I'm not rendering anything yet. The problem is the Image.open(img_path) part. For some reason, this is not getting executed no matter what I try. The page keeps loading forever until I get this:

Something went wrong :-( Something went wrong while trying to load this site; please try again later.

Debugging tips If this is your site, and you just reloaded it, then the problem might simply be that it hasn't loaded up yet. Try refreshing this page and see if this message disappears.

If you keep getting this message, you should check your site's server and error logs for any messages.

Error code: 504-backend

If I go to the logs

Access log:

200.205.135.90 - - [13/Dec/2021:14:47:17 +0000] "GET /readimage/example.jpg HTTP/1.1" 504 1663 "http://andrevargas22.pythonanywhere.com/loading/example.jpg" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36" "200.205.135.90" response-time=300.002

Error log: Nothing

Server log:

2021-12-13 14:47:19 Mon Dec 13 14:47:18 2021 - *** HARAKIRI ON WORKER 2 (pid: 9, try: 1) ***
2021-12-13 14:47:19 Mon Dec 13 14:47:18 2021 - HARAKIRI !!! worker 2 status !!!
2021-12-13 14:47:19 Mon Dec 13 14:47:18 2021 - HARAKIRI [core 0] 10.0.0.66 - GET /readimage/example.jpg since 
1639406537
2021-12-13 14:47:19 Mon Dec 13 14:47:18 2021 - HARAKIRI !!! end of worker 2 status !!!
2021-12-13 14:47:19 DAMN ! worker 2 (pid: 9) died, killed by signal 9 :( trying respawn ...
2021-12-13 14:47:19 Respawned uWSGI worker 2 (new pid: 14)
2021-12-13 14:47:19 spawned 2 offload threads for uWSGI worker 2

If I run just this:

@app.route('/readimage/<filename>', methods=['POST', 'GET'])
def readimage(filename):
return filename

I get the filename as expected.

It's clear to me that is something related to the static file path, but I can't tell what is it. The file is there, I'm passing the full path, it works on my local machine. Please, how can I fix this?

How does your static files mapping look like?

How does your static files mapping look like?

This is something that confuses me a lot, but I tried this

URL = /static/ DIRECTORY = /home/me/mysite/static/

And it didn't work

Have you tried running the code in your view in a console to see how long it takes on the file that you're using? Perhaps it's something about that file that just causes that load to be really slow. Also, static files do not appear to be involved here, the problem is in your view and does not seem to have anything to do with static files.

Have you tried running the code in your view in a console to see how long it takes on the file that you're using? Perhaps it's something about that file that just causes that load to be really slow. Also, static files do not appear to be involved here, the problem is in your view and does not seem to have anything to do with static files.

Indeed you are correct, I was wrong, it has nothing to do with the static files. The problem seems to be this line here

image = in_transform(image)[:3,:,:].unsqueeze(0)

For whatever reason, this takes forever in my flask_app.py. But it works super fast on my computer, and I opened another file and ran the same function alone and it worked super fast. What could it be?

Were you able to run the code in the console on your account?