I want to refresh my web page every time a file is uploaded to the folder.

I have a web service written in flask having the following handler

def getlatest():
    import os
    import glob
    newset = max(glob.iglob('static/*'),key=os.path.getctime)
    Return newest;

This gives me the name of the latest file in the folder.

I have an Ajax call in my JS (client side) to constantly get data from above function.

function GetLatest()
                    url: "http://localhost:5000/getlatest", 
                    success: function(result)
                        if(previousName != result){
                            previousName = result;

function calling server every second.

(function myLoop (i) {          
    setTimeout(function () {   
    if (--i) myLoop(i);
       }, 1000)

This Works fine (well almost). My Question is: Is there any better way to do it (there must be)?

I'm open to technology choices what every they may be (node, angualr etc).


1 Answer 1


Server side:

  • There's no point in importing os and glob inside the function. Local import is used for very specific cases and this is not one of them.
  • You're defining newset and then returning newest, did you test your code?
  • There's no point in defining newest (after fixing the typo), and then returning it in a separate line. Just return it in one line.
  • Add a space before key=..., as per Python conventions.

Client side:

What you've implemented here isn't very smart. You're constantly asking the server for updates, instead of having the server push updates to you when ever it detects them. You've essentially implemented a REST API, but for the wrong reasons. Take a look at websockets (flask-socketio is pretty good).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.