During my time building Microsoft Excel apps using VBA, I worked with events like _Change and _Click.
Transitioning to JavaScript and frameworks was a bit overwhelming due to the asynchronous nature of it all.
Moving on to Python and Flask has been a refreshing change for me.
However, when it comes to frontend development, I struggle with creating dynamic forms as I can't find the equivalent tags (such as document.getElementsBy in JavaScript) and events (like onChange, onClick) that I used in VBA.
Just yesterday, I faced a challenge where I needed to update SelectField options based on user input in a TextField. This required me to incorporate <script>
tag and write JavaScript snippets:
<script>
//Accessing elements:
let train_number = document.getElementsByClassName('train-number');
let stops_list = document.getElementsByClassName('stops-list');
//Adding event listener:
train_number[0].onkeyup = (e) => {
//Clear stops_list:
if (train_number[0].value == '') {
stops_list[0].options.length = 0;
}
//Alphanumeric characters check:
let charStr = String.fromCharCode(e.which || e.keyCode);
if (/[a-z0-9]/i.test(charStr)) {
//Clear stops_list:
stops_list[0].options.length = 0;
searched_train = train_number[0].value;
//Fetching stations data:
fetch('/train/' + searched_train).then((response) => {
response.json().then((data) => {
for (var i = 0; i < data.length; i++) {
//Creating new option element
var opt = document.createElement('option');
//Adding text node to option element (opt)
opt.appendChild( document.createTextNode(data[i]) );
//Setting value property of opt
opt.value = data[i];
//Adding opt to select box (sel)
stops_list[0].add(opt);
}
})
})
}
}
</script>
Now, here's my question: Can dynamic forms and views be created using only Flask? Or would it be more beneficial to switch to another framework? If a framework change is recommended, which one do you suggest that is simple, lightweight, user-friendly, easy to learn, and preferably synchronous?