How to serve files generated via python script?

Hi all,

I’m developing a monitoring app using Django and Dash, the thing is I need to display a couple images and gifs that I generate using a python script, but I don’t know where this files should be for my django app to access them.

This files are generated every 20 minutes or so via the script. The script generates images and gifs and saves them in a directory.

html.Div([
                      gif.GifPlayer(
                      id= 'gif',
                      gif=  "http://url_to_gif/gif.gif",  
                      still= app.get_asset_url('image.png') 
)

That’s the code that needs to have access to the images the code can access them using http or the app.get_asset_url().

Right now I tried to make it work with static files but it doesn’t because every time I generate a file inside the static folder if the app is already running it doesn´t recognize new files.

Also the script generates the gif and overrides it when is time to generate a new one. I followed this approach in order to save space, please let me know if this is a good approach or if I always should create a different file.

As for now, I’m thinking in maybe setup a http file server to serve the files since static files are not working, if anybody knows how to approach this problem I’ll be glad to read it.

You should probably use the media files interface for this, not the static files. See Managing files | Django documentation | Django.

Yes you can do it that way, but you would need to ensure that that file is never cached by the browser. It’s more reliable to generate the file with a different name each time to ensure that the browser requests it. (You then need to delete the previous file when the new file is created.

Regardless, it’s your web server that should be serving the files in either case, not your Django application.

1 Like

@KenWhitesell Thank you so much for your reply.

I’ve been looking into media files and they seem like a good option, the only problem that I have is that the component only receives http url or files that are in the static folder, that’s just how the component is coded. So I’ll try using MEDIA_URL which I think should let me access the files using http.

Well also I would like to know your opinion on using a http file server, I’m new to this technologies and the http file server seems like a similar way of implementing the media file interface, so I would really appreciate your opinion on this. I might deploy the project to an ubuntu machine so the file server shouldn’t be an issue.

Can you elaborate on that? I don’t understand how that can matter.

I don’t think I understand what you’re trying to reference here, either.

Django itself shouldn’t be serving files. In a production-quality deployment, it’s your web server - nginx, apache, lightspeed, etc that should be responding to the http requests being issued by the browser.