0

I am playing with Flask framework and I have a form where I input a value into a text box. The submit button fetches json object from an API endpoint and creates a file with the name of the value. Which is then stored back in the server as a text file. (one of flask's directories).

I want to perform a validation whenever a user enters the value on to the form -- check if the file with the same name exists. If it exits, alert the user that the file exits and the user could cancel to abort fetching the API again 'or' refetch the API to refresh the content.

With python I can perform the validation by doing "os.path.exists('mydirectory/myfile.txt')" Which will return me a true or a false.

How do I propogate it back to the flask page as an alert to the user? Can someone give me pointers as to should I be using javascript on the client side? I dont want to re-render the page with flask, I want the user to stay on the same page and just have an alert pop up with appropriate action.

sidman
  • 213
  • 2
  • 4
  • 15

1 Answers1

1

A good way to implement for your requirement provided as answer to your question:

How do I propagate it back to the flask page as an alert to the user?

Answer: Create an api that take a filename as an argument which the user will input in the form, and then perform whatsoever validation check, you want to put, and response some boolean value for the same.

Can someone give me pointers as to should I be using javascript on the client side?

Answer: Now, on client side after the input field is filled and then user moves on next input field, using javascript event, may be onblur (make sure check field value is not empty), do an ajax call using api created above and whatever response received, you can add alert on the page using any DOM manipulation library (jQuery) or you can use Javascript itself.

I would suggest not to use onChange event or on(from jQuery since), this may lead to multiple ajax call for unintended user input.

IrrupTor
  • 117
  • 7