Things have been busy and I have not been able to be at my computer lately to work on programming. Looking at my calendar though it feels longer than it actually is as it was only last week I made a fulfilling breakthrough. Although I can't say I still completely understand the date field I feel comfortable using it.
The first trouble I had is that in the date field from Flask forms. I knew how to display that part of the form but when I would enter a date, manually, into the field, upon submission the page would display "Not a valid date" above the field. I actually just commented that code out for a few weeks and worked on other areas of the site. That proved very successful as I basically completed the app in that time and only had the elusive date field to tackle. By that time I was already confident in the app and the date shouldn't be too difficult to handle right?
As it stood the form was not recognizing the data in the field as a valid date. I thought maybe having a date picker would somehow enter correct information. So I searched and found jQuery ui. That would allow me to attach a date picker to an element so that when clicked it would show a date picker. It worked to an extent in that the page would display a date picker widget when clicking the date field but still said it was not valid data.
Many Google and StackOverflow searches later I came to the conclusion the backend was not parsing the data the way it should have been. When sending the data from the form, using the data in the app must be implemented using:
date.strftime(date_variable, "string format") - string format is for the date, i.e. '%Y-%m-%d'
So when sending the data from the web to the back end the data can be entered into an SQL query where the column is for dates.
So what I learned is the Flask date field must convert the entered string into a unix time object because you must enter the date format as a parameter when creating the date field. That data then must be sent to Python script and entered into the database. However date.strfime must be used so the format can be parsed by SQL as a date object and acceptably entered into the database. For a few tries there was a date entered but it was '0000-00-00'. Obviously not what I wanted.
So I suppose I do have an idea of why things were not functioning as I wanted them to. I was so happy to finally figure out the bane of my last few weeks and be so close to an MVP at last. One more roadblock though.
Right now I am having difficulty with multiple browsers. Most specifically Chrome and Safari (I haven't checked Firefox yet though I suppose I should). The trouble is the date picker widget created with jQuery ui. It functions just fine in Safari and technically works in Chrome. The problem arises in the fact that Chrome has its own date picker built in. Thus I get the error of not valid data when using the date picker. If I don't attach the jQuery date picker to the date field, Chrome's date picker will work fine and the data can be entered. The problem is using Safari then, the date picker cannot be used. Catch 22?
So I have made great strides, in my opinion, in understanding datetimes. But browser compatibility is a new challenge. I suppose that may never be solved even by the best developers.
25 words or less: Understanding how to parse datetimes is an invaluable tool and should not be overlooked. Keep trying, you'll get it.
Also I can't say my lack of posts is not entirely due to a recent obsession with Mad Men, but it certainly had a larger impact on my nonproductivity. Maybe someday I'll deviate some and write a post on my view of the show and understanding the ending.
A place where I hope to share my knowledge in my quest to learn programming and help you where I can. Published (ideally) every Wednesday, or whenever I can.
Sunday, September 18, 2016
Monday, September 5, 2016
Dependent Select Box (Part 3)
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.
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.
Subscribe to:
Posts (Atom)