So last time I thought I was done with the dependent select box, but as with most things in programming, I was mistaken and had to learn more. Recently though I've felt a bit of competence as far as troubleshooting goes.
I tried to make a select box where the choices was an empty list and populate it through JSON data with Javascript. The problem was when the form was submitted it always said it was not a valid choice although the html contained the options I desired. Turns out Flask had to know the choices available and then I could write Javascript to only display which options match the above select box. The select boxes can be populated with Javascript but will only be accepted as valid if they are already available in the Flask app.
I also ran in to a lot of trouble with the error of NoneType not being iterable. I searched and searched what was a none type and couldn't find anything. My lambda functions seemed ok and they were supposed to iterate through a list. I was very frustrated for a long time. Even my stack overflow question was down voted. No-one helped me there either, which is surprising to me because I've gotten good information from other people's questions. But I finally figured out I was looking in the completely wrong place. The None type had to do with the select box, not the actual iterable lambda function I was using to populate the choices in the select box. So although I'm not sure what the function was trying to iterate, I do know how to avoid it and make it work.
I also encountered trouble when this piece of code did not work:
SelectField("Select", choices=[("0", "Choose")] + list((lambda to populate rest of list)), coerce=int)
** The lambda function returning tuples of (int, string) pairs **
I was a little proud of finding this error totally on my own. I can't remember the exact error but the problem was the following. The list from the lambda, and what I wanted, returned an (int, string) pair. However the part that would become the first position of the list was a tuple of ("0", "Choose") which is a string, string pair. Then the coerce=int was trying to do so with the string of "0" and not the int of 0. All I had to do was remove the quotes to make it an int. Admittedly a very simple fix but I feel I've been having trouble with type compatibility lately. Especially trying to take form data with Flask, enter it into a MySQL database and have the form communicate with the database so it can enter information of the correct type into the correct column. I also think my comprehension of how it is all working together has greatly improved working on this app.
I've also learned many other things to make this app work how I want. I was able to integrate Google maps API distance matrix for Python and Javascript to find distance between employees and stores. I suppose that will be for another post. hat was a very fun skill to learn and the reason I made the app is completely dependent on that API functioning. My company uses Bing to calculate distances and travel time. I don't know if they offer an API. I will have to research that if they don't accept using Google with my app.
25 words or less:
Pay attention to why errors are being thrown, they can teach you so much about how code works.
No comments:
Post a Comment